Websocket Hello World over an ethernet network
Dependencies: EthernetNetIf mbed DNSResolver
main.cpp@8:66a7e251b8c5, 2012-02-01 (annotated)
- Committer:
- samux
- Date:
- Wed Feb 01 17:24:22 2012 +0000
- Revision:
- 8:66a7e251b8c5
- Parent:
- 7:ecad5b72fa16
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 6:aa2b63dc24f3 | 1 | #include "mbed.h" |
samux | 6:aa2b63dc24f3 | 2 | #include "Websocket.h" |
samux | 6:aa2b63dc24f3 | 3 | |
samux | 6:aa2b63dc24f3 | 4 | Serial pc(USBTX, USBRX); |
samux | 6:aa2b63dc24f3 | 5 | Timer tmr; |
samux | 6:aa2b63dc24f3 | 6 | |
samux | 6:aa2b63dc24f3 | 7 | //Here, we create a Websocket instance in 'rw' (write) mode |
samux | 8:66a7e251b8c5 | 8 | //on the 'samux' channel |
samux | 8:66a7e251b8c5 | 9 | Websocket ws("ws://sockets.mbed.org/ws/samux/rw"); |
samux | 6:aa2b63dc24f3 | 10 | |
samux | 6:aa2b63dc24f3 | 11 | int main() { |
samux | 8:66a7e251b8c5 | 12 | char recv[128]; |
samux | 6:aa2b63dc24f3 | 13 | while (1) { |
samux | 8:66a7e251b8c5 | 14 | |
samux | 8:66a7e251b8c5 | 15 | while (!ws.connect()) |
samux | 6:aa2b63dc24f3 | 16 | pc.printf("cannot connect websocket, retrying\r\n"); |
samux | 8:66a7e251b8c5 | 17 | |
samux | 6:aa2b63dc24f3 | 18 | tmr.start(); |
samux | 6:aa2b63dc24f3 | 19 | while (1) { |
samux | 8:66a7e251b8c5 | 20 | if (tmr.read() > 0.5) { |
samux | 8:66a7e251b8c5 | 21 | ws.send("Hello World! over Ethernet"); |
samux | 8:66a7e251b8c5 | 22 | if (ws.read(recv)) { |
samux | 8:66a7e251b8c5 | 23 | pc.printf("recv: %s\r\n", recv); |
samux | 8:66a7e251b8c5 | 24 | } |
samux | 6:aa2b63dc24f3 | 25 | tmr.start(); |
samux | 6:aa2b63dc24f3 | 26 | } |
samux | 6:aa2b63dc24f3 | 27 | Net::poll(); |
samux | 6:aa2b63dc24f3 | 28 | } |
samux | 6:aa2b63dc24f3 | 29 | } |
samux | 0:dac316268a0c | 30 | } |