qsdasd

Dependencies:   WebSocketClient

Committer:
sam_grove
Date:
Fri Oct 25 00:07:04 2013 +0000
Revision:
3:9bd22e5386cd
Parent:
1:1c1802ec42a2
Child:
6:2fae6e37c5ca
Updated example and dependencies

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:1c1802ec42a2 1 #include "mbed.h"
samux 1:1c1802ec42a2 2 #include "EthernetInterface.h"
samux 1:1c1802ec42a2 3 #include "Websocket.h"
sam_grove 3:9bd22e5386cd 4
sam_grove 3:9bd22e5386cd 5 Ticker flash;
sam_grove 3:9bd22e5386cd 6 DigitalOut led(LED1);
sam_grove 3:9bd22e5386cd 7 void flashLED(void){led = !led;}
samux 1:1c1802ec42a2 8
samux 1:1c1802ec42a2 9
sam_grove 3:9bd22e5386cd 10 int main()
sam_grove 3:9bd22e5386cd 11 {
sam_grove 3:9bd22e5386cd 12 flash.attach(&flashLED, 1.0f);
sam_grove 3:9bd22e5386cd 13
sam_grove 3:9bd22e5386cd 14 EthernetInterface ethernet;
sam_grove 3:9bd22e5386cd 15 ethernet.init(); // connect with DHCP
sam_grove 3:9bd22e5386cd 16 int ret_val = ethernet.connect();
sam_grove 3:9bd22e5386cd 17
sam_grove 3:9bd22e5386cd 18 if (0 == ret_val) {
sam_grove 3:9bd22e5386cd 19 printf("IP Address: %s\n", ethernet.getIPAddress());
sam_grove 3:9bd22e5386cd 20 } else {
sam_grove 3:9bd22e5386cd 21 error("ethernet failed to connect: %d.\n", ret_val);
sam_grove 3:9bd22e5386cd 22 }
sam_grove 3:9bd22e5386cd 23
sam_grove 3:9bd22e5386cd 24 // view @ http://sockets.mbed.org/demo/viewer
sam_grove 3:9bd22e5386cd 25 Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw");
samux 1:1c1802ec42a2 26 ws.connect();
sam_grove 3:9bd22e5386cd 27 char str[100];
sam_grove 3:9bd22e5386cd 28
sam_grove 3:9bd22e5386cd 29 for(int i=0; i<0x7fffffff; ++i) {
sam_grove 3:9bd22e5386cd 30 // string with a message
sam_grove 3:9bd22e5386cd 31 sprintf(str, "%d WebSocket Hello World over Ethernet", i);
sam_grove 3:9bd22e5386cd 32 ws.send(str);
sam_grove 3:9bd22e5386cd 33
sam_grove 3:9bd22e5386cd 34 // clear the buffer and wait a sec...
sam_grove 3:9bd22e5386cd 35 memset(str, 0, 100);
sam_grove 3:9bd22e5386cd 36 wait(0.5f);
sam_grove 3:9bd22e5386cd 37
sam_grove 3:9bd22e5386cd 38 // websocket server should echo whatever we sent it
sam_grove 3:9bd22e5386cd 39 if (ws.read(str)) {
sam_grove 3:9bd22e5386cd 40 printf("rcv'd: %s\n", str);
sam_grove 3:9bd22e5386cd 41 }
samux 1:1c1802ec42a2 42 }
sam_grove 3:9bd22e5386cd 43 ws.close();
sam_grove 3:9bd22e5386cd 44 ethernet.disconnect();
sam_grove 3:9bd22e5386cd 45
sam_grove 3:9bd22e5386cd 46 while(true);
samux 1:1c1802ec42a2 47 }