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 #include "TCPSocketConnection.h" 00037 00038 /** Websocket client Class. 00039 * 00040 * Example (ethernet network): 00041 * @code 00042 * #include "mbed.h" 00043 * #include "EthernetInterface.h" 00044 * #include "Websocket.h" 00045 * 00046 * int main() { 00047 * EthernetInterface eth; 00048 * eth.init(); //Use DHCP 00049 * eth.connect(); 00050 * printf("IP Address is %s\n\r", eth.getIPAddress()); 00051 * 00052 * Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw"); 00053 * ws.connect(); 00054 * 00055 * while (1) { 00056 * int res = ws.send("WebSocket Hello World!"); 00057 * 00058 * if (ws.read(recv)) { 00059 * printf("rcv: %s\r\n", recv); 00060 * } 00061 * 00062 * wait(0.1); 00063 * } 00064 * } 00065 * @endcode 00066 */ 00067 00068 class Websocket 00069 { 00070 public: 00071 /** 00072 * Constructor 00073 * 00074 * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) 00075 */ 00076 Websocket(char * url); 00077 00078 /** 00079 * Connect to the websocket url 00080 * 00081 *@return true if the connection is established, false otherwise 00082 */ 00083 bool connect(); 00084 00085 /** 00086 * Send a string according to the websocket format (see rfc 6455) 00087 * 00088 * @param str string to be sent 00089 * 00090 * @returns the number of bytes sent 00091 */ 00092 int send(char * str); 00093 00094 /** 00095 * Read a websocket message 00096 * 00097 * @param message pointer to the string to be read (null if drop frame) 00098 * 00099 * @return true if a websocket frame has been read 00100 */ 00101 bool read(char * message); 00102 00103 /** 00104 * To see if there is a websocket connection active 00105 * 00106 * @return true if there is a connection active 00107 */ 00108 bool is_connected(); 00109 00110 /** 00111 * Close the websocket connection 00112 * 00113 * @return true if the connection has been closed, false otherwise 00114 */ 00115 bool close(); 00116 00117 /* 00118 * Accessor: get path from the websocket url 00119 * 00120 * @return path 00121 */ 00122 char* getPath(); 00123 00124 private: 00125 00126 void fillFields(char * url); 00127 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 00128 int sendOpcode(uint8_t opcode, char * msg); 00129 int sendLength(uint32_t len, char * msg); 00130 int sendMask(char * msg); 00131 int readChar(char * pC, bool block = true); 00132 00133 char scheme[8]; 00134 uint16_t port; 00135 char host[32]; 00136 char path[64]; 00137 00138 TCPSocketConnection socket; 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 Wed Jul 13 2022 23:37:28 by
1.7.2
