|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | +ESP32 WebSocket |
| 5 | +================== |
| 6 | + |
| 7 | +A component for WebSockets on ESP-IDF using lwip netconn. |
| 8 | +For an example, see https://github.com/Molorius/ESP32-Examples. |
| 9 | + |
| 10 | +To add to a project, type: |
| 11 | +`git submodule add https://github.com/Molorius/esp32-websocket.git components/websocket` |
| 12 | +into the base directory of your project. |
| 13 | + |
| 14 | +Some configuration options for the Server can be found in menuconfig in: |
| 15 | +Component config ---> WebSocket Server |
| 16 | + |
| 17 | +This presently only has the WebSocket server code working, but client code will be added in the future (the groundwork is there). |
| 18 | + |
| 19 | +The code only allows one WebSocket server at a time, but this merely handles all incoming reads. New connections are added externally, so this can be used to hold various WebSocket connections. |
| 20 | + |
| 21 | +While this can theoretically handle very large messages, hardware constraints (RAM) limits the size of messages. I highly recommend not using more than 5000 bytes per message, but no constraint is in place for this. |
| 22 | + |
| 23 | +Any suggestions or fixes are gladly appreciated. |
| 24 | + |
| 25 | +Table of Contents |
| 26 | +================= |
| 27 | +* [Enumerations](#enumerations) |
| 28 | + * [WEBSOCKET_TYPE_t](#enum-websocket_type_t) |
| 29 | +* [Functions](#functions) |
| 30 | + * [ws_server_start](#int-ws_server_start) |
| 31 | + * [ws_server_stop](#int-ws_server_stop) |
| 32 | + * [ws_server_add_client](#int-ws_server_add_clientstruct-netconn-connchar-msguint16_t-lenchar-urlvoid-callback) |
| 33 | + * [ws_server_add_client_protocol](#int-ws_server_add_client_protocolstruct-netconn-connchar-msguint16_t-lenchar-urlchar-protocolvoid-callback) |
| 34 | + * [ws_server_len_url](#int-ws_server_len_urlchar-url) |
| 35 | + * [ws_server_len_all](#int-ws_server_len_all) |
| 36 | + * [ws_server_remove_client](#int-ws_server_remove_clientint-num) |
| 37 | + * [ws_server_remove_clients](#int-ws_server_remove_clientschar-url) |
| 38 | + * [ws_server_remove_all](#int-ws_server_remove_all) |
| 39 | + * [ws_server_send_text_client](#int-ws_server_send_text_clientint-numchar-msguint64_t-len) |
| 40 | + * [ws_server_send_text_clients](#int-ws_server_send_text_clientschar-urlchar-msguint64_t-len) |
| 41 | + * [ws_server_send_text_all](#int-ws_server_send_text_allchar-msguint64_t-len) |
| 42 | + * [ws_server_send_text_client_from_callback](#int-ws_server_send_text_client_from_callbackint-numchar-msguint64_t-len) |
| 43 | + * [ws_server_send_text_clients_from_callback](#int-ws_server_send_text_clients_from_callbackchar-urlchar-msguint64_t-len) |
| 44 | + * [ws_server_send_text_all_from_callback](#int-ws_server_send_text_all_from_callbackchar-msguint64_t-len) |
| 45 | + * [ws_server_send_bin_client_from_callback](#int-ws_server_send_bin_client_from_callbackint-numchar-msguint64_t-len) |
| 46 | + * [ws_server_send_bin_clients_from_callback](#int-ws_server_send_bin_clients_from_callbackchar-urlchar-msguint64_t-len) |
| 47 | + * [ws_server_send_bin_all_from_callback](#int-ws_server_send_bin_all_from_callbackchar-msguint64_t-len) |
| 48 | + |
| 49 | +Enumerations |
| 50 | +============ |
| 51 | + |
| 52 | +enum WEBSOCKET_TYPE_t |
| 53 | +--------------------- |
| 54 | + |
| 55 | +The different types of WebSocket events. |
| 56 | + |
| 57 | +*Values* |
| 58 | + * `WEBSOCKET_CONNECT`: A new client has successfully connected. |
| 59 | + * `WEBSOCKET_DISCONNECT_EXTERNAL`: The other side sent a disconnect message. |
| 60 | + * `WEBSOCKET_DISCONNECT_INTERNAL`: The esp32 server sent a disconnect message. |
| 61 | + * `WEBSOCKET_DISCONNECT_ERROR`: Disconnect due to a connection error. |
| 62 | + * `WEBSOCKET_TEXT`: Incoming text. |
| 63 | + * `WEBSOCKET_BIN`: Incoming binary. |
| 64 | + * `WEBSOCKET_PING`: The other side sent a ping message. |
| 65 | + * `WEBSOCKET_PONG`: The other side successfully replied to our ping. |
| 66 | + |
| 67 | +Functions |
| 68 | +========= |
| 69 | + |
| 70 | +int ws_server_start() |
| 71 | +--------------------- |
| 72 | + |
| 73 | +Starts the WebSocket Server. Use this function before attempting any |
| 74 | +sort of transmission or adding a client. |
| 75 | + |
| 76 | +*Returns* |
| 77 | + * 1: successful start |
| 78 | + * 0: server already running |
| 79 | + |
| 80 | +int ws_server_stop() |
| 81 | +-------------------- |
| 82 | + |
| 83 | +Stops the WebSocket Server. New clients can still be added and |
| 84 | +messages can be sent, but new messages will not be received. |
| 85 | + |
| 86 | +*Returns* |
| 87 | + * 1: successful stop |
| 88 | + * 0: server was not running before |
| 89 | + |
| 90 | +int ws_server_add_client(struct netconn* conn,char* msg,uint16_t len,char* url,void *callback) |
| 91 | +---------------------------------------------------------------------------------------------- |
| 92 | + |
| 93 | +Adds a client to the WebSocket Server handler and performs the necessary handshake. |
| 94 | + |
| 95 | +*Parameters* |
| 96 | + * `conn`: the lwip netconn connection. |
| 97 | + * `msg`: the entire incoming request message to join the server. Necessary for the handshake. |
| 98 | + * `len`: the length of `msg`. |
| 99 | + * `url`: the NULL-terminated url. Used to keep track of clients, not required. |
| 100 | + * `callback`: the callback that is used to run WebSocket events. This must be with parameters(uint8_t num,WEBSOCKET_TYPE_t type,char* msg,uint64_t len) where "num" is the client number, "type" is the event type, "msg" is the incoming message, and "len" is the message length. The callback itself is optional. |
| 101 | + |
| 102 | +*Returns* |
| 103 | + * -2: not enough information in `msg` to perform handshake. |
| 104 | + * -1: server full, or connection issue. |
| 105 | + * 0 or greater: connection number |
| 106 | + |
| 107 | +int ws_server_add_client_protocol(struct netconn* conn,char* msg,uint16_t len,char* url,char* protocol,void *callback) |
| 108 | +---------------------------------------------------------------------------------------------------------------------- |
| 109 | + |
| 110 | +Adds a client to the WebSocket Server handler and performs the necessary handshake. Will also send |
| 111 | +the specified protocol. |
| 112 | + |
| 113 | +*Parameters* |
| 114 | + * `conn`: the lwip netconn connection. |
| 115 | + * `msg`: the entire incoming request message to join the server. Necessary for the handshake. |
| 116 | + * `len`: the length of `msg`. |
| 117 | + * `url`: the NULL-terminated url. Used to keep track of clients, not required. |
| 118 | + * `protocol`: the NULL-terminated protocol. This will be sent to the client in the header. |
| 119 | + * `callback`: the callback that is used to run WebSocket events. This must be with parameters(uint8_t num,WEBSOCKET_TYPE_t type,char* msg,uint64_t len) where "num" is the client number, "type" is the event type, "msg" is the incoming message, and "len" is the message length. The callback itself is optional. |
| 120 | + |
| 121 | +*Returns* |
| 122 | + * -2: not enough information in `msg` to perform handshake. |
| 123 | + * -1: server full, or connection issue. |
| 124 | + * 0 or greater: connection number |
| 125 | + |
| 126 | +int ws_server_len_url(char* url) |
| 127 | +-------------------------------- |
| 128 | + |
| 129 | +Returns the number of clients connected to the specified URL. |
| 130 | + |
| 131 | +*Parameters* |
| 132 | + * `url`: the NULL-terminated string of the desired URL. |
| 133 | + |
| 134 | +*Returns* |
| 135 | + * The number of clients connected to the specified URL. |
| 136 | + |
| 137 | +int ws_server_len_all() |
| 138 | +----------------------- |
| 139 | + |
| 140 | +*Returns* |
| 141 | + * The number of connected clients. |
| 142 | + |
| 143 | +int ws_server_remove_client(int num) |
| 144 | +------------------------------------ |
| 145 | + |
| 146 | +Removes the desired client. |
| 147 | + |
| 148 | +*Parameters* |
| 149 | + * `num`: the client number |
| 150 | + |
| 151 | +*Returns* |
| 152 | + * 0: not a valid client number |
| 153 | + * 1: client disconnected |
| 154 | + |
| 155 | +int ws_server_remove_clients(char* url) |
| 156 | +--------------------------------------- |
| 157 | + |
| 158 | +Removes all clients connect to the desired URL. |
| 159 | + |
| 160 | +*Parameters* |
| 161 | + * `url`: the NULL-terminated URL. |
| 162 | + |
| 163 | +*Returns* |
| 164 | + * The number of clients that were disconnected. |
| 165 | + |
| 166 | +int ws_server_remove_all() |
| 167 | +-------------------------- |
| 168 | + |
| 169 | +Removes all clients from server. |
| 170 | + |
| 171 | +*Returns* |
| 172 | + * The number of clients that were disconnected. |
| 173 | + |
| 174 | +int ws_server_send_text_client(int num,char* msg,uint64_t len) |
| 175 | +-------------------------------------------------------------- |
| 176 | + |
| 177 | +Sends the desired message to the client. |
| 178 | + |
| 179 | +*Parameters* |
| 180 | + * `num`: the client's number. |
| 181 | + * `msg`: the desired message. |
| 182 | + * `len`: the length of the message. |
| 183 | + |
| 184 | +*Returns* |
| 185 | + * 0: message not sent properly |
| 186 | + * 1: message sent |
| 187 | + |
| 188 | +int ws_server_send_text_clients(char* url,char* msg,uint64_t len) |
| 189 | +----------------------------------------------------------------- |
| 190 | + |
| 191 | +Sends the message to clients connected to the desired URL. |
| 192 | + |
| 193 | +*Parameters* |
| 194 | + * `url`: the NULL-terminated URL. |
| 195 | + * `msg`: the desired message. |
| 196 | + * `len`: the length of the message. |
| 197 | + |
| 198 | +*Returns* |
| 199 | + * The number of clients that the message was sent to. |
| 200 | + |
| 201 | +int ws_server_send_text_all(char* msg,uint64_t len) |
| 202 | +--------------------------------------------------- |
| 203 | + |
| 204 | +Sends the message to all connected clients. |
| 205 | + |
| 206 | +*Parameters* |
| 207 | + * `msg`: the desired message |
| 208 | + * `len`: the length of the message |
| 209 | + |
| 210 | +*Returns* |
| 211 | + * The number of clients that the message was sent to. |
| 212 | + |
| 213 | +int ws_server_send_text_client_from_callback(int num,char* msg,uint64_t len) |
| 214 | +---------------------------------------------------------------------------- |
| 215 | + |
| 216 | +Sends the desired message to the client. Only use this inside the callback function. |
| 217 | + |
| 218 | +*Parameters* |
| 219 | + * `num`: the client's number. |
| 220 | + * `msg`: the desired message. |
| 221 | + * `len`: the length of the message. |
| 222 | + |
| 223 | +*Returns* |
| 224 | + * 0: message not sent properly |
| 225 | + * 1: message sent |
| 226 | + |
| 227 | +int ws_server_send_text_clients_from_callback(char* url,char* msg,uint64_t len) |
| 228 | +------------------------------------------------------------------------------- |
| 229 | + |
| 230 | +Sends the message to clients connected to the desired URL. Only use this inside the callback function. |
| 231 | + |
| 232 | +*Parameters* |
| 233 | + * `url`: the NULL-terminated URL. |
| 234 | + * `msg`: the desired message. |
| 235 | + * `len`: the length of the message. |
| 236 | + |
| 237 | +*Returns* |
| 238 | + * The number of clients that the message was sent to. |
| 239 | + |
| 240 | +int ws_server_send_text_all_from_callback(char* msg,uint64_t len) |
| 241 | +----------------------------------------------------------------- |
| 242 | + |
| 243 | +Sends the message to all connected clients. Only use this inside the callback function. |
| 244 | + |
| 245 | +*Parameters* |
| 246 | + * `msg`: the desired message |
| 247 | + * `len`: the length of the message |
| 248 | + |
| 249 | +*Returns* |
| 250 | + * The number of clients that the message was sent to. |
| 251 | + |
| 252 | + int ws_server_send_bin_client_from_callback(int num,char* msg,uint64_t len) |
| 253 | +---------------------------------------------------------------------------- |
| 254 | + |
| 255 | +Sends the desired binary message to the client. Only use this inside the callback function. |
| 256 | + |
| 257 | +*Parameters* |
| 258 | + * `num`: the client's number. |
| 259 | + * `msg`: the desired message. |
| 260 | + * `len`: the length of the message. |
| 261 | + |
| 262 | +*Returns* |
| 263 | + * 0: message not sent properly |
| 264 | + * 1: message sent |
| 265 | + |
| 266 | +int ws_server_send_bin_clients_from_callback(char* url,char* msg,uint64_t len) |
| 267 | +------------------------------------------------------------------------------- |
| 268 | + |
| 269 | +Sends the binary message to clients connected to the desired URL. Only use this inside the callback function. |
| 270 | + |
| 271 | +*Parameters* |
| 272 | + * `url`: the NULL-terminated URL. |
| 273 | + * `msg`: the desired message. |
| 274 | + * `len`: the length of the message. |
| 275 | + |
| 276 | +*Returns* |
| 277 | + * The number of clients that the message was sent to. |
| 278 | + |
| 279 | +int ws_server_send_bin_all_from_callback(char* msg,uint64_t len) |
| 280 | +----------------------------------------------------------------- |
| 281 | + |
| 282 | +Sends a binary message to all connected clients. Only use this inside the callback function. |
| 283 | + |
| 284 | +*Parameters* |
| 285 | + * `msg`: the desired message |
| 286 | + * `len`: the length of the message |
| 287 | + |
| 288 | +*Returns* |
| 289 | + * The number of clients that the message was sent to. |
0 commit comments