Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of WebSocketClient by
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 00036 /** Websocket client Class. 00037 * 00038 * Example (ethernet network): 00039 * @code 00040 * #include "mbed.h" 00041 * #include "EthernetInterface.h" 00042 * #include "Websocket.h" 00043 * 00044 * int main() { 00045 * EthernetInterface eth; 00046 * eth.init(); //Use DHCP 00047 * eth.connect(); 00048 * printf("IP Address is %s\n\r", eth.getIPAddress()); 00049 * 00050 * Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw"); 00051 * ws.connect(); 00052 * 00053 * while (1) { 00054 * int res = ws.send("WebSocket Hello World!"); 00055 * 00056 * if (ws.read(recv)) { 00057 * printf("rcv: %s\r\n", recv); 00058 * } 00059 * 00060 * wait(0.1); 00061 * } 00062 * } 00063 * @endcode 00064 */ 00065 00066 class Websocket 00067 { 00068 public: 00069 /** 00070 * Constructor 00071 * 00072 * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) 00073 */ 00074 Websocket(NetworkStack *ns, char * url); 00075 00076 /** 00077 * Connect to the websocket url 00078 * 00079 *@return true if the connection is established, false otherwise 00080 */ 00081 bool connect(); 00082 00083 /** 00084 * Send a string according to the websocket format (see rfc 6455) 00085 * 00086 * @param str string to be sent 00087 * 00088 * @returns the number of bytes sent 00089 */ 00090 int send(char * str); 00091 00092 /** 00093 * Read a websocket message 00094 * 00095 * @param message pointer to the string to be read (null if drop frame) 00096 * 00097 * @return true if a websocket frame has been read 00098 */ 00099 bool read(char * message); 00100 00101 /** 00102 * To see if there is a websocket connection active 00103 * 00104 * @return true if there is a connection active 00105 */ 00106 bool is_connected(); 00107 00108 /** 00109 * Close the websocket connection 00110 * 00111 * @return true if the connection has been closed, false otherwise 00112 */ 00113 bool close(); 00114 00115 /* 00116 * Accessor: get path from the websocket url 00117 * 00118 * @return path 00119 */ 00120 char* getPath(); 00121 00122 private: 00123 00124 void fillFields(char * url); 00125 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 00126 int sendOpcode(uint8_t opcode, char * msg); 00127 int sendLength(uint32_t len, char * msg); 00128 int sendMask(char * msg); 00129 int readChar(char * pC, bool block = true); 00130 00131 char scheme[8]; 00132 uint16_t port; 00133 char host[32]; 00134 char path[64]; 00135 bool connected; 00136 00137 TCPSocket socket; 00138 NetworkStack *m_ns; 00139 00140 int read(char * buf, int len, int min_len = -1); 00141 int write(char * buf, int len); 00142 }; 00143 00144 #endif
Generated on Sun Jul 17 2022 09:02:19 by
1.7.2
