Suga koubou / WebSocket_test

http://mbed.org/users/okini3939/notebook/node_websocket/

Dependencies:   EthernetNetIf mbed MbedJSONValue

Committer:
okini3939
Date:
Wed Nov 02 02:56:07 2011 +0000
Revision:
0:236a084b1d6b

    

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini39390:236a084b1d6b 1#include "mbed.h"
okini39390:236a084b1d6b 2#include "Websocket.h"
okini39390:236a084b1d6b 3#include "MbedJSONValue.h"
okini39390:236a084b1d6b 4#include "EthernetNetIf.h"
okini39390:236a084b1d6b 5
okini39390:236a084b1d6b 6#define BASE_URL "ws://host.domain.name:8080/"
okini39390:236a084b1d6b 7
okini39390:236a084b1d6b 8DigitalOut myled(LED1);
okini39390:236a084b1d6b 9Serial pc(USBTX, USBRX);
okini39390:236a084b1d6b 10
okini39390:236a084b1d6b 11EthernetNetIf *eth;
okini39390:236a084b1d6b 12Websocket *ws;
okini39390:236a084b1d6b 13
okini39390:236a084b1d6b 14int main() {
okini39390:236a084b1d6b 15 int i = 0;
okini39390:236a084b1d6b 16 char buf[100];
okini39390:236a084b1d6b 17 MbedJSONValue json;
okini39390:236a084b1d6b 18 Timer timer;
okini39390:236a084b1d6b 19
okini39390:236a084b1d6b 20 eth = new EthernetNetIf();
okini39390:236a084b1d6b 21 EthernetErr ethErr = eth->setup();
okini39390:236a084b1d6b 22 if (ethErr) {
okini39390:236a084b1d6b 23 pc.printf("\r\nERROR %d in setup.\r\n", ethErr);
okini39390:236a084b1d6b 24 }
okini39390:236a084b1d6b 25
okini39390:236a084b1d6b 26 ws = new Websocket(BASE_URL "test", eth);
okini39390:236a084b1d6b 27
okini39390:236a084b1d6b 28 pc.printf("begin\r\n");
okini39390:236a084b1d6b 29
okini39390:236a084b1d6b 30 json["hello"] = "mbed";
okini39390:236a084b1d6b 31 json["num"] = i;
okini39390:236a084b1d6b 32
okini39390:236a084b1d6b 33 while(! ws->connect()) {
okini39390:236a084b1d6b 34 pc.printf("cannot connect websocket, retrying...\r\n");
okini39390:236a084b1d6b 35 wait(2);
okini39390:236a084b1d6b 36 }
okini39390:236a084b1d6b 37
okini39390:236a084b1d6b 38 timer.start();
okini39390:236a084b1d6b 39 while(1) {
okini39390:236a084b1d6b 40 Net::poll();
okini39390:236a084b1d6b 41
okini39390:236a084b1d6b 42 if (timer.read_ms() >= 2000) {
okini39390:236a084b1d6b 43 json["num"] = i ++;
okini39390:236a084b1d6b 44 ws->send((char*)json.serialize().c_str());
okini39390:236a084b1d6b 45 timer.reset();
okini39390:236a084b1d6b 46 myled = myled ? 0 : 1;
okini39390:236a084b1d6b 47 }
okini39390:236a084b1d6b 48
okini39390:236a084b1d6b 49 if (ws->read(buf)) {
okini39390:236a084b1d6b 50 pc.printf("recv: %s\r\n", buf);
okini39390:236a084b1d6b 51 }
okini39390:236a084b1d6b 52
okini39390:236a084b1d6b 53 Net::poll();
okini39390:236a084b1d6b 54
okini39390:236a084b1d6b 55 if (! ws->connected()) {
okini39390:236a084b1d6b 56 pc.printf("disconnected\r\n");
okini39390:236a084b1d6b 57 break;
okini39390:236a084b1d6b 58 }
okini39390:236a084b1d6b 59 }
okini39390:236a084b1d6b 60}