Websocket example

Dependencies:   C027 UbloxUSBModem WebSocketClient mbed

Fork of VodafoneUSBModemWebsocketTest by Donatien Garnier

main.cpp

Committer:
mazgch
Date:
2013-10-21
Revision:
8:05a5c32037c2
Parent:
7:745fa95221ba
Child:
10:495e03a3fc8f

File content as of revision 8:05a5c32037c2:

#include "mbed.h"
#include "C027.h"
#include "UbloxUSBGSMModem.h"
#include "UbloxUSBCDMAModem.h"

#include "Websocket.h"

void test(void const*)
{
    UbloxUSBGSMModem modem; // for LISA-C use the UbloxUSBCDMAModem instead
    Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw");
    char recv[128];

    int ret = modem.connect("internet"); // eventaully set another apn here
    if(ret) {
        printf("Could not connect\n");
        return;
    }

    bool c = ws.connect();
    printf("Connect result: %s\n", c?"OK":"Failed");

    for(int i = 0; i < 10000; i++) {
        if(!(i%100)) {
            int ret = ws.send("WebSocket Hello World over the Vodafone Network!");
            if(ret<0) {
                printf("Timeout\n");
                ws.close();
                c = ws.connect();
                printf("Connect result: %s\n", c?"OK":"Failed");
            }
        }

        if (ws.read(recv)) {
            printf("rcv: %s\r\n", recv);
        }

    }

    modem.disconnect();

    while(1) {
    }
}


int main()
{
    Thread testTask(test, NULL, osPriorityNormal, 1024 * 4);
    DigitalOut led(A0); // Connect a LED to A0 if you like to see some blinking
    while(1) {
        led=!led;
        Thread::wait(1000);
    }

    return 0;
}