WebSocket client library
Diff: Websocket.h
- Revision:
- 22:f4aac491ea26
- Parent:
- 18:ef44ea603938
--- a/Websocket.h Tue Nov 15 19:37:40 2011 +0000 +++ b/Websocket.h Wed Feb 01 17:23:40 2012 +0000 @@ -35,9 +35,13 @@ #include "Wifly.h" #include <string> + +#ifdef TARGET_LPC1768 #include "EthernetNetIf.h" #include "TCPSocket.h" #include "dnsresolve.h" +#endif //target + /** Websocket client Class. * @@ -49,32 +53,39 @@ * #include "Wifly.h" * #include "Websocket.h" * - * Serial pc(USBTX, USBRX); - * Wifly * wifly; - * Websocket * ws; - * - * int main() - * { - * wifly = new Wifly(p9, p10, p20, "network", "password", true); - * ws = new Websocket("ws://ip_domain/path", wifly); - * - * if(wifly->join()) - * { - * if(ws->connect()) - * { - * pc.printf("ws connected\r\n"); - * while(1) - * { - * wait(0.1); - * ws->send("test"); - * } - * } - * else - * pc.printf("ws not connected\r\n"); - * } - * else - * pc.printf("join network failed\r\n"); - * + * DigitalOut l1(LED1); + * + * //Here, we create an instance, with pins 9 and 10 connecting to the + * //WiFly's TX and RX pins, and pin 21 to RESET. We are connecting to the + * //"mbed" network, password "password", and we are using WPA. + * Wifly wifly(p9, p10, p21, "mbed", "password", true); + * + * //Here, we create a Websocket instance in 'wo' (write-only) mode + * //on the 'samux' channel + * Websocket ws("ws://sockets.mbed.org/ws/samux/wo", &wifly); + * + * + * int main() { + * while (1) { + * + * //we connect the network + * while (!wifly.join()) { + * wifly.reset(); + * } + * + * //we connect to the websocket server + * while (!ws.connect()); + * + * while (1) { + * wait(0.5); + * + * //Send Hello world + * ws.send("Hello World! over Wifi"); + * + * // show that we are alive + * l1 = !l1; + * } + * } * } * @endcode * @@ -85,24 +96,26 @@ * #include "mbed.h" * #include "Websocket.h" * - * Serial pc(USBTX, USBRX); - * Websocket * ws; - * - * int main() - * { - * ws = new Websocket("ws://ip_domain/path"); - * - * if(ws->connect()) - * { - * pc.printf("ws connected\r\n"); - * while(1) - * { - * wait(0.1); - * ws->send("test"); - * } - * } - * else - * pc.printf("ws not connected\r\n"); + * Timer tmr; + * + * //Here, we create a Websocket instance in 'wo' (write-only) mode + * //on the 'samux' channel + * Websocket ws("ws://sockets.mbed.org/ws/samux/wo"); + * + * int main() { + * while (1) { + * + * while (!ws.connect()); + * + * tmr.start(); + * while (1) { + * if (tmr.read() > 0.5) { + * ws.send("Hello World! over Ethernet"); + * tmr.start(); + * } + * Net::poll(); + * } + * } * } * @endcode */ @@ -117,12 +130,14 @@ */ Websocket(char * url, Wifly * wifi); +#ifdef TARGET_LPC1768 /** * Constructor for an ethernet communication * * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) */ Websocket(char * url); +#endif //target /** * Connect to the websocket url @@ -132,7 +147,7 @@ bool connect(); /** - * Send a string according to the websocket format: 00 str ff + * Send a string according to the websocket format * * @param str string to be sent */ @@ -178,6 +193,10 @@ void fillFields(char * url); + void sendOpcode(uint8_t opcode); + void sendLength(uint32_t len); + void sendMask(); + void sendChar(uint8_t c); std::string ip_domain; std::string path; @@ -185,6 +204,7 @@ Wifly * wifi; +#ifdef TARGET_LPC1768 void onTCPSocketEvent(TCPSocketEvent e); bool eth_connected; bool eth_readable; @@ -196,9 +216,9 @@ EthernetNetIf * eth; TCPSocket * sock; IpAddr * server_ip; +#endif //target Type netif; - };