Home

Awesome

By Blake Felt - blake.w.felt@gmail.com

ESP32 WebSocket

A component for WebSockets on ESP-IDF using lwip netconn. For an example, see https://github.com/Molorius/ESP32-Examples.

To add to a project, type: git submodule add https://github.com/Molorius/esp32-websocket.git components/websocket into the base directory of your project.

Some configuration options for the Server can be found in menuconfig in: Component config ---> WebSocket Server

This presently only has the WebSocket server code working, but client code will be added in the future (the groundwork is there).

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.

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.

Any suggestions or fixes are gladly appreciated.

Table of Contents

Enumerations

enum WEBSOCKET_TYPE_t

The different types of WebSocket events.

Values

Functions

int ws_server_start()

Starts the WebSocket Server. Use this function before attempting any sort of transmission or adding a client.

Returns

int ws_server_stop()

Stops the WebSocket Server. New clients can still be added and messages can be sent, but new messages will not be received.

Returns

int ws_server_add_client(struct netconn* conn,char* msg,uint16_t len,char* url,void *callback)

Adds a client to the WebSocket Server handler and performs the necessary handshake.

Parameters

Returns

int ws_server_add_client_protocol(struct netconn* conn,char* msg,uint16_t len,char* url,char* protocol,void *callback)

Adds a client to the WebSocket Server handler and performs the necessary handshake. Will also send the specified protocol.

Parameters

Returns

int ws_server_len_url(char* url)

Returns the number of clients connected to the specified URL.

Parameters

Returns

int ws_server_len_all()

Returns

int ws_server_remove_client(int num)

Removes the desired client.

Parameters

Returns

int ws_server_remove_clients(char* url)

Removes all clients connect to the desired URL.

Parameters

Returns

int ws_server_remove_all()

Removes all clients from server.

Returns

int ws_server_send_text_client(int num,char* msg,uint64_t len)

Sends the desired message to the client.

Parameters

Returns

int ws_server_send_text_clients(char* url,char* msg,uint64_t len)

Sends the message to clients connected to the desired URL.

Parameters

Returns

int ws_server_send_text_all(char* msg,uint64_t len)

Sends the message to all connected clients.

Parameters

Returns

int ws_server_send_text_client_from_callback(int num,char* msg,uint64_t len)

Sends the desired message to the client. Only use this inside the callback function.

Parameters

Returns

int ws_server_send_text_clients_from_callback(char* url,char* msg,uint64_t len)

Sends the message to clients connected to the desired URL. Only use this inside the callback function.

Parameters

Returns

int ws_server_send_text_all_from_callback(char* msg,uint64_t len)

Sends the message to all connected clients. Only use this inside the callback function.

Parameters

Returns

int ws_server_send_bin_client_from_callback(int num,char* msg,uint64_t len)

Sends the desired binary message to the client. Only use this inside the callback function.

Parameters

Returns

int ws_server_send_bin_clients_from_callback(char* url,char* msg,uint64_t len)

Sends the binary message to clients connected to the desired URL. Only use this inside the callback function.

Parameters

Returns

int ws_server_send_bin_all_from_callback(char* msg,uint64_t len)

Sends a binary message to all connected clients. Only use this inside the callback function.

Parameters

Returns