Client of WebSocket protocol
Fork of WebSocketClient by
Websocket.h@8:c9da00db9d33, 2017-02-24 (annotated)
- Committer:
- mauricioaschmitz
- Date:
- Fri Feb 24 20:06:49 2017 +0000
- Revision:
- 8:c9da00db9d33
- Parent:
- 6:86e89a0369b9
Added project prtgmdWebSocketClient
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 4:466f90b7849a | 1 | /** |
samux | 4:466f90b7849a | 2 | * @author Samuel Mokrani |
samux | 4:466f90b7849a | 3 | * |
samux | 4:466f90b7849a | 4 | * @section LICENSE |
samux | 4:466f90b7849a | 5 | * |
samux | 4:466f90b7849a | 6 | * Copyright (c) 2011 mbed |
samux | 4:466f90b7849a | 7 | * |
samux | 4:466f90b7849a | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
samux | 4:466f90b7849a | 9 | * of this software and associated documentation files (the "Software"), to deal |
samux | 4:466f90b7849a | 10 | * in the Software without restriction, including without limitation the rights |
samux | 4:466f90b7849a | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
samux | 4:466f90b7849a | 12 | * copies of the Software, and to permit persons to whom the Software is |
samux | 4:466f90b7849a | 13 | * furnished to do so, subject to the following conditions: |
samux | 4:466f90b7849a | 14 | * |
samux | 4:466f90b7849a | 15 | * The above copyright notice and this permission notice shall be included in |
samux | 4:466f90b7849a | 16 | * all copies or substantial portions of the Software. |
samux | 4:466f90b7849a | 17 | * |
samux | 4:466f90b7849a | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
samux | 4:466f90b7849a | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
samux | 4:466f90b7849a | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
samux | 4:466f90b7849a | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
samux | 4:466f90b7849a | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samux | 4:466f90b7849a | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
samux | 4:466f90b7849a | 24 | * THE SOFTWARE. |
samux | 4:466f90b7849a | 25 | * |
samux | 4:466f90b7849a | 26 | * @section DESCRIPTION |
samux | 4:466f90b7849a | 27 | * Simple websocket client |
samux | 4:466f90b7849a | 28 | * |
samux | 4:466f90b7849a | 29 | */ |
samux | 4:466f90b7849a | 30 | |
samux | 4:466f90b7849a | 31 | #ifndef WEBSOCKET_H |
samux | 4:466f90b7849a | 32 | #define WEBSOCKET_H |
samux | 4:466f90b7849a | 33 | |
samux | 4:466f90b7849a | 34 | #include "mbed.h" |
mauricioaschmitz | 8:c9da00db9d33 | 35 | #include "rtos.h" |
samux | 4:466f90b7849a | 36 | |
samux | 4:466f90b7849a | 37 | #include "TCPSocketConnection.h" |
mauricioaschmitz | 8:c9da00db9d33 | 38 | #include "EventDetector.h" |
mauricioaschmitz | 8:c9da00db9d33 | 39 | |
mauricioaschmitz | 8:c9da00db9d33 | 40 | extern void Websocket_Thread(void const *arg); |
samux | 4:466f90b7849a | 41 | |
samux | 4:466f90b7849a | 42 | /** Websocket client Class. |
samux | 4:466f90b7849a | 43 | * |
samux | 4:466f90b7849a | 44 | * Example (ethernet network): |
samux | 4:466f90b7849a | 45 | * @code |
samux | 4:466f90b7849a | 46 | * #include "mbed.h" |
samux | 4:466f90b7849a | 47 | * #include "EthernetInterface.h" |
samux | 4:466f90b7849a | 48 | * #include "Websocket.h" |
samux | 4:466f90b7849a | 49 | * |
samux | 4:466f90b7849a | 50 | * int main() { |
samux | 4:466f90b7849a | 51 | * EthernetInterface eth; |
samux | 4:466f90b7849a | 52 | * eth.init(); //Use DHCP |
samux | 4:466f90b7849a | 53 | * eth.connect(); |
samux | 4:466f90b7849a | 54 | * printf("IP Address is %s\n\r", eth.getIPAddress()); |
samux | 4:466f90b7849a | 55 | * |
samux | 4:466f90b7849a | 56 | * Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw"); |
samux | 4:466f90b7849a | 57 | * ws.connect(); |
samux | 4:466f90b7849a | 58 | * |
samux | 4:466f90b7849a | 59 | * while (1) { |
samux | 4:466f90b7849a | 60 | * int res = ws.send("WebSocket Hello World!"); |
samux | 4:466f90b7849a | 61 | * |
samux | 4:466f90b7849a | 62 | * if (ws.read(recv)) { |
samux | 4:466f90b7849a | 63 | * printf("rcv: %s\r\n", recv); |
samux | 4:466f90b7849a | 64 | * } |
samux | 4:466f90b7849a | 65 | * |
samux | 4:466f90b7849a | 66 | * wait(0.1); |
samux | 4:466f90b7849a | 67 | * } |
samux | 4:466f90b7849a | 68 | * } |
samux | 4:466f90b7849a | 69 | * @endcode |
samux | 4:466f90b7849a | 70 | */ |
samux | 4:466f90b7849a | 71 | |
samux | 4:466f90b7849a | 72 | class Websocket |
samux | 4:466f90b7849a | 73 | { |
samux | 4:466f90b7849a | 74 | public: |
mauricioaschmitz | 8:c9da00db9d33 | 75 | |
mauricioaschmitz | 8:c9da00db9d33 | 76 | /* |
mauricioaschmitz | 8:c9da00db9d33 | 77 | * Prepare message get from mailbox |
mauricioaschmitz | 8:c9da00db9d33 | 78 | * |
mauricioaschmitz | 8:c9da00db9d33 | 79 | * |
mauricioaschmitz | 8:c9da00db9d33 | 80 | */ |
mauricioaschmitz | 8:c9da00db9d33 | 81 | static void ReceiveMessage(char * dados){ haveMessage = true; captureMessage = dados; } |
mauricioaschmitz | 8:c9da00db9d33 | 82 | |
mauricioaschmitz | 8:c9da00db9d33 | 83 | /* |
mauricioaschmitz | 8:c9da00db9d33 | 84 | * Get status of connection |
mauricioaschmitz | 8:c9da00db9d33 | 85 | * |
mauricioaschmitz | 8:c9da00db9d33 | 86 | * |
mauricioaschmitz | 8:c9da00db9d33 | 87 | */ |
mauricioaschmitz | 8:c9da00db9d33 | 88 | static bool wsIsConnected(); |
mauricioaschmitz | 8:c9da00db9d33 | 89 | |
mauricioaschmitz | 8:c9da00db9d33 | 90 | |
mauricioaschmitz | 8:c9da00db9d33 | 91 | /** |
mauricioaschmitz | 8:c9da00db9d33 | 92 | * Thread to start Websocket |
mauricioaschmitz | 8:c9da00db9d33 | 93 | * |
mauricioaschmitz | 8:c9da00db9d33 | 94 | */ |
mauricioaschmitz | 8:c9da00db9d33 | 95 | static void Websocket_Thread(void const *arg); |
mauricioaschmitz | 8:c9da00db9d33 | 96 | |
samux | 4:466f90b7849a | 97 | /** |
samux | 4:466f90b7849a | 98 | * Constructor |
samux | 4:466f90b7849a | 99 | * |
samux | 4:466f90b7849a | 100 | * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) |
samux | 4:466f90b7849a | 101 | */ |
mauricioaschmitz | 8:c9da00db9d33 | 102 | Websocket(char const * url); |
samux | 4:466f90b7849a | 103 | |
samux | 4:466f90b7849a | 104 | /** |
samux | 4:466f90b7849a | 105 | * Connect to the websocket url |
samux | 4:466f90b7849a | 106 | * |
samux | 4:466f90b7849a | 107 | *@return true if the connection is established, false otherwise |
samux | 4:466f90b7849a | 108 | */ |
samux | 4:466f90b7849a | 109 | bool connect(); |
samux | 4:466f90b7849a | 110 | |
samux | 4:466f90b7849a | 111 | /** |
samux | 4:466f90b7849a | 112 | * Send a string according to the websocket format (see rfc 6455) |
samux | 4:466f90b7849a | 113 | * |
samux | 4:466f90b7849a | 114 | * @param str string to be sent |
samux | 4:466f90b7849a | 115 | * |
samux | 4:466f90b7849a | 116 | * @returns the number of bytes sent |
samux | 4:466f90b7849a | 117 | */ |
samux | 4:466f90b7849a | 118 | int send(char * str); |
samux | 4:466f90b7849a | 119 | |
samux | 4:466f90b7849a | 120 | /** |
samux | 4:466f90b7849a | 121 | * Read a websocket message |
samux | 4:466f90b7849a | 122 | * |
samux | 4:466f90b7849a | 123 | * @param message pointer to the string to be read (null if drop frame) |
samux | 4:466f90b7849a | 124 | * |
samux | 4:466f90b7849a | 125 | * @return true if a websocket frame has been read |
samux | 4:466f90b7849a | 126 | */ |
samux | 4:466f90b7849a | 127 | bool read(char * message); |
samux | 4:466f90b7849a | 128 | |
samux | 4:466f90b7849a | 129 | /** |
samux | 4:466f90b7849a | 130 | * To see if there is a websocket connection active |
samux | 4:466f90b7849a | 131 | * |
samux | 4:466f90b7849a | 132 | * @return true if there is a connection active |
samux | 4:466f90b7849a | 133 | */ |
samux | 4:466f90b7849a | 134 | bool is_connected(); |
samux | 4:466f90b7849a | 135 | |
samux | 4:466f90b7849a | 136 | /** |
samux | 4:466f90b7849a | 137 | * Close the websocket connection |
samux | 4:466f90b7849a | 138 | * |
samux | 4:466f90b7849a | 139 | * @return true if the connection has been closed, false otherwise |
samux | 4:466f90b7849a | 140 | */ |
samux | 4:466f90b7849a | 141 | bool close(); |
samux | 4:466f90b7849a | 142 | |
samux | 4:466f90b7849a | 143 | /* |
samux | 4:466f90b7849a | 144 | * Accessor: get path from the websocket url |
samux | 4:466f90b7849a | 145 | * |
samux | 4:466f90b7849a | 146 | * @return path |
samux | 4:466f90b7849a | 147 | */ |
donatien | 6:86e89a0369b9 | 148 | char* getPath(); |
samux | 4:466f90b7849a | 149 | |
samux | 4:466f90b7849a | 150 | private: |
samux | 4:466f90b7849a | 151 | |
mauricioaschmitz | 8:c9da00db9d33 | 152 | void fillFields(char const * url); |
donatien | 6:86e89a0369b9 | 153 | 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 |
samux | 4:466f90b7849a | 154 | int sendOpcode(uint8_t opcode, char * msg); |
samux | 4:466f90b7849a | 155 | int sendLength(uint32_t len, char * msg); |
samux | 4:466f90b7849a | 156 | int sendMask(char * msg); |
samux | 4:466f90b7849a | 157 | int readChar(char * pC, bool block = true); |
donatien | 6:86e89a0369b9 | 158 | |
donatien | 6:86e89a0369b9 | 159 | char scheme[8]; |
donatien | 6:86e89a0369b9 | 160 | uint16_t port; |
donatien | 6:86e89a0369b9 | 161 | char host[32]; |
donatien | 6:86e89a0369b9 | 162 | char path[64]; |
samux | 4:466f90b7849a | 163 | |
samux | 4:466f90b7849a | 164 | TCPSocketConnection socket; |
samux | 4:466f90b7849a | 165 | |
samux | 4:466f90b7849a | 166 | int read(char * buf, int len, int min_len = -1); |
samux | 4:466f90b7849a | 167 | int write(char * buf, int len); |
mauricioaschmitz | 8:c9da00db9d33 | 168 | |
mauricioaschmitz | 8:c9da00db9d33 | 169 | static char * captureMessage; |
mauricioaschmitz | 8:c9da00db9d33 | 170 | static bool haveMessage; |
mauricioaschmitz | 8:c9da00db9d33 | 171 | static bool isConect; |
mauricioaschmitz | 8:c9da00db9d33 | 172 | |
samux | 4:466f90b7849a | 173 | }; |
samux | 4:466f90b7849a | 174 | |
samux | 4:466f90b7849a | 175 | #endif |