WebSocket client library
Websocket.h
- Committer:
- samux
- Date:
- 2011-08-25
- Revision:
- 10:4f02275c34ee
- Parent:
- 9:9fa055ed54b4
- Child:
- 11:094c86973097
File content as of revision 10:4f02275c34ee:
/** * @author Samuel Mokrani * * @section LICENSE * * Copyright (c) 2011 mbed * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * * @section DESCRIPTION * Simple websocket client * */ #ifndef WEBSOCKET_H #define WEBSOCKET_H #include "mbed.h" #include "Wifly.h" #include "EthernetNetIf.h" #include "TCPSocket.h" #include "dnsresolve.h" /** Websocket client Class. * * Warning: you must use a wifi module (Wifly RN131-C or RN131-g) to use this class or an ethernet connection * * Example with a Wifi module (Wifly RN131-C or RNR131-g): * @code * #include "mbed.h" * #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:443/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"); * * } * @endcode * * * Example with an Ethernet Jack: * @code * #include "mbed.h" * #include "Websocket.h" * * Serial pc(USBTX, USBRX); * Websocket * ws; * * int main() * { * ws = new Websocket("ws://ip_domain:443/path"); * * while(!ws->connect()) * pc.printf("cannot connect webasocekt\r\n"); * * ws->Send("Hello World!"); * } */ class Websocket { public: /** * Constructor for a wifi module * * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) * @param wifi pointer to a wifi module (the communication will be establish by this module) */ Websocket(char * url, Wifly * wifi); /** * 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); /** * Connect to the websocket url * *@return true if the connection is established, false otherwise */ bool connect(); /** * Send a string according to the websocket format: \x00 str \xff * * @param str string to be sent */ void Send(char * str); /** * Read a websocket message * * @param message pointer to the string to be read (null if drop frame) * * @return true if a string has been read, false otherwise */ bool read(char * message); /** * To see if there is an active websocket connection * * @return true if there is an active websocket connection */ bool connected(); /** * Close the websocket connection * * @return true if the connection has been closed, false otherwise */ bool close(); private: void fillFields(bool wifi, char * url); void onTCPSocketEvent(TCPSocketEvent e); char * ip_domain; char * path; char * port; Wifly * wifi; bool eth_connected; char eth_rx[512]; bool response_server_eth; bool wifi_use; bool eth_use; EthernetNetIf eth; TCPSocket sock; IpAddr server_ip; }; #endif