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.
Dependents: WebsocketClient Web_suck_et APS SO - ALARME CONTROLADO VIA SOCKETS F411-mbed-os-iot-project ... more
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(char * url, NetworkInterface * iface); 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 * Close the websocket connection 00103 * 00104 * @return true if the connection has been closed, false otherwise 00105 */ 00106 bool close(); 00107 00108 /* 00109 * Accessor: get path from the websocket url 00110 * 00111 * @return path 00112 */ 00113 char* getPath(); 00114 00115 private: 00116 void fillFields(char * url); 00117 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 00118 int sendOpcode(uint8_t opcode, char * msg); 00119 int sendLength(uint32_t len, char * msg); 00120 int sendMask(char * msg); 00121 int readChar(char * pC, bool block = true); 00122 00123 char scheme[8]; 00124 uint16_t port; 00125 char host[32]; 00126 char path[64]; 00127 00128 TCPSocket socket; 00129 00130 int read(char * buf, int len, int min_len = -1); 00131 int write(char * buf, int len); 00132 }; 00133 00134 #endif
Generated on Wed Jul 13 2022 15:45:22 by
1.7.2

HTML5 Websockets