Client of WebSocket protocol
Fork of WebSocketClient by
Embed:
(wiki syntax)
Show/hide line numbers
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 #ifndef WEBSOCKET_H 00032 #define WEBSOCKET_H 00033 00034 #include "mbed.h" 00035 #include "rtos.h" 00036 00037 #include "TCPSocketConnection.h" 00038 #include "EventDetector.h" 00039 00040 extern void Websocket_Thread(void const *arg); 00041 00042 /** Websocket client Class. 00043 * 00044 * Example (ethernet network): 00045 * @code 00046 * #include "mbed.h" 00047 * #include "EthernetInterface.h" 00048 * #include "Websocket.h" 00049 * 00050 * int main() { 00051 * EthernetInterface eth; 00052 * eth.init(); //Use DHCP 00053 * eth.connect(); 00054 * printf("IP Address is %s\n\r", eth.getIPAddress()); 00055 * 00056 * Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw"); 00057 * ws.connect(); 00058 * 00059 * while (1) { 00060 * int res = ws.send("WebSocket Hello World!"); 00061 * 00062 * if (ws.read(recv)) { 00063 * printf("rcv: %s\r\n", recv); 00064 * } 00065 * 00066 * wait(0.1); 00067 * } 00068 * } 00069 * @endcode 00070 */ 00071 00072 class Websocket 00073 { 00074 public: 00075 00076 /* 00077 * Prepare message get from mailbox 00078 * 00079 * 00080 */ 00081 static void ReceiveMessage(char * dados){ haveMessage = true; captureMessage = dados; } 00082 00083 /* 00084 * Get status of connection 00085 * 00086 * 00087 */ 00088 static bool wsIsConnected(); 00089 00090 00091 /** 00092 * Thread to start Websocket 00093 * 00094 */ 00095 static void Websocket_Thread(void const *arg); 00096 00097 /** 00098 * Constructor 00099 * 00100 * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) 00101 */ 00102 Websocket(char const * url); 00103 00104 /** 00105 * Connect to the websocket url 00106 * 00107 *@return true if the connection is established, false otherwise 00108 */ 00109 bool connect(); 00110 00111 /** 00112 * Send a string according to the websocket format (see rfc 6455) 00113 * 00114 * @param str string to be sent 00115 * 00116 * @returns the number of bytes sent 00117 */ 00118 int send(char * str); 00119 00120 /** 00121 * Read a websocket message 00122 * 00123 * @param message pointer to the string to be read (null if drop frame) 00124 * 00125 * @return true if a websocket frame has been read 00126 */ 00127 bool read(char * message); 00128 00129 /** 00130 * To see if there is a websocket connection active 00131 * 00132 * @return true if there is a connection active 00133 */ 00134 bool is_connected(); 00135 00136 /** 00137 * Close the websocket connection 00138 * 00139 * @return true if the connection has been closed, false otherwise 00140 */ 00141 bool close(); 00142 00143 /* 00144 * Accessor: get path from the websocket url 00145 * 00146 * @return path 00147 */ 00148 char* getPath(); 00149 00150 private: 00151 00152 void fillFields(char const * url); 00153 int parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen); //Parse URL 00154 int sendOpcode(uint8_t opcode, char * msg); 00155 int sendLength(uint32_t len, char * msg); 00156 int sendMask(char * msg); 00157 int readChar(char * pC, bool block = true); 00158 00159 char scheme[8]; 00160 uint16_t port; 00161 char host[32]; 00162 char path[64]; 00163 00164 TCPSocketConnection socket; 00165 00166 int read(char * buf, int len, int min_len = -1); 00167 int write(char * buf, int len); 00168 00169 static char * captureMessage; 00170 static bool haveMessage; 00171 static bool isConect; 00172 00173 }; 00174 00175 #endif
Generated on Fri Jul 22 2022 00:51:42 by 1.7.2