zabuton library
Dependencies: CushionSock EthernetNetIf MbedJSONValue mbed
Websocket.h
00001 /** 00002 * @author Samuel Mokrani 00003 * 00004 * @section LICENSE 00005 * 00006 * Copyright (c) 2011 mbed 00007 * 00008 * Permission is hereby granted, free of charge, to any person obtaining a copy 00009 * of this software and associated documentation files (the "Software"), to deal 00010 * in the Software without restriction, including without limitation the rights 00011 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00012 * copies of the Software, and to permit persons to whom the Software is 00013 * furnished to do so, subject to the following conditions: 00014 * 00015 * The above copyright notice and this permission notice shall be included in 00016 * all copies or substantial portions of the Software. 00017 * 00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00020 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00021 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00022 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00023 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00024 * THE SOFTWARE. 00025 * 00026 * @section DESCRIPTION 00027 * Simple websocket client 00028 * 00029 */ 00030 /* 00031 * modify by Suga 00032 */ 00033 00034 #ifndef WEBSOCKET_H 00035 #define WEBSOCKET_H 00036 00037 //#define WIFLY 00038 //#define ETH_SETUP 00039 00040 #include "mbed.h" 00041 #ifdef WIFLY 00042 #include "Wifly.h" 00043 #endif 00044 #include <string> 00045 00046 #include "EthernetNetIf.h" 00047 #include "TCPSocket.h" 00048 //#include "dnsresolve.h" 00049 #include "DNSRequest.h" 00050 00051 /** Websocket client Class. 00052 * 00053 * Warning: you must use a wifi module (Wifly RN131-C) or an ethernet network to use this class 00054 * 00055 * Example (wifi network): 00056 * @code 00057 * #include "mbed.h" 00058 * #include "Wifly.h" 00059 * #include "Websocket.h" 00060 * 00061 * Serial pc(USBTX, USBRX); 00062 * Wifly * wifly; 00063 * Websocket * ws; 00064 * 00065 * int main() 00066 * { 00067 * wifly = new Wifly(p9, p10, p20, "network", "password", true); 00068 * ws = new Websocket("ws://ip_domain/path", wifly); 00069 * 00070 * if(wifly->join()) 00071 * { 00072 * if(ws->connect()) 00073 * { 00074 * pc.printf("ws connected\r\n"); 00075 * while(1) 00076 * { 00077 * wait(0.1); 00078 * ws->send("test"); 00079 * } 00080 * } 00081 * else 00082 * pc.printf("ws not connected\r\n"); 00083 * } 00084 * else 00085 * pc.printf("join network failed\r\n"); 00086 * 00087 * } 00088 * @endcode 00089 * 00090 * 00091 * 00092 * Example (ethernet network): 00093 * @code 00094 * #include "mbed.h" 00095 * #include "Websocket.h" 00096 * 00097 * Serial pc(USBTX, USBRX); 00098 * Websocket * ws; 00099 * 00100 * int main() 00101 * { 00102 * ws = new Websocket("ws://ip_domain/path"); 00103 * 00104 * if(ws->connect()) 00105 * { 00106 * pc.printf("ws connected\r\n"); 00107 * while(1) 00108 * { 00109 * wait(0.1); 00110 * ws->send("test"); 00111 * } 00112 * } 00113 * else 00114 * pc.printf("ws not connected\r\n"); 00115 * } 00116 * @endcode 00117 */ 00118 class Websocket 00119 { 00120 public: 00121 /** 00122 * Constructor 00123 * 00124 * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) 00125 * @param wifi pointer to a wifi module (the communication will be establish by this module) 00126 */ 00127 #ifdef WIFLY 00128 Websocket(char * url, Wifly * wifi); 00129 #endif 00130 00131 /** 00132 * Constructor for an ethernet communication 00133 * 00134 * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) 00135 */ 00136 Websocket(char * url, EthernetNetIf *e); 00137 00138 /** 00139 * Connect to the websocket url 00140 * 00141 *@return true if the connection is established, false otherwise 00142 */ 00143 bool connect(); 00144 00145 /** 00146 * Send a string according to the websocket format: 00 str ff 00147 * 00148 * @param str string to be sent 00149 */ 00150 void send(char * str); 00151 00152 /** 00153 * Read a websocket message 00154 * 00155 * @param message pointer to the string to be read (null if drop frame) 00156 * 00157 * @return true if a string has been read, false otherwise 00158 */ 00159 bool read(char * message); 00160 00161 /** 00162 * To see if there is a websocket connection active 00163 * 00164 * @return true if there is a connection active 00165 */ 00166 bool connected(); 00167 00168 /** 00169 * Close the websocket connection 00170 * 00171 * @return true if the connection has been closed, false otherwise 00172 */ 00173 bool close(); 00174 00175 /** 00176 * Accessor: get path from the websocket url 00177 * 00178 * @return path 00179 */ 00180 std::string getPath(); 00181 00182 enum Type { 00183 WIF, 00184 ETH 00185 }; 00186 00187 00188 private: 00189 00190 00191 void fillFields(char * url); 00192 void isr_dns (DNSReply r); 00193 00194 std::string ip_domain; 00195 std::string path; 00196 std::string port; 00197 00198 #ifdef WIFLY 00199 Wifly * wifi; 00200 #endif 00201 00202 void onTCPSocketEvent(TCPSocketEvent e); 00203 bool eth_connected; 00204 bool eth_readable; 00205 bool eth_writeable; 00206 char eth_rx[512]; 00207 bool response_server_eth; 00208 bool new_msg; 00209 int dns_status; 00210 00211 EthernetNetIf * eth; 00212 TCPSocket * sock; 00213 IpAddr * server_ip; 00214 00215 Type netif; 00216 00217 00218 }; 00219 00220 #endif
Generated on Thu Jul 14 2022 08:11:09 by 1.7.2