Skip to content

Latest commit

 

History

History
51 lines (35 loc) · 1.01 KB

File metadata and controls

51 lines (35 loc) · 1.01 KB

WebSocket (enable + broadcast)

The library can use ESPAsyncWebServer + AsyncWebSocket for bidirectional communication with web clients.

Start with a custom handler (recommended)

void onWsEvent(AsyncWebSocket* server,
               AsyncWebSocketClient* client,
               AwsEventType type,
               void* arg,
               uint8_t* data,
               size_t len) {
  // handle connect/disconnect/data...
}

server.init(onWsEvent);

With init(onWsEvent) a websocket is created on /ws.

Enable WebSocket at runtime

If you want to create a websocket at a different time:

server.enableWebSocket("/ws", onWsEvent);

Send messages to all clients

Text:

server.wsBroadcast("hello");

Binary:

uint8_t payload[] = {0x01, 0x02};
server.wsBroadcastBinary(payload, sizeof(payload));

Get the websocket pointer

AsyncWebSocket* ws = server.getWebSocket();

Useful if you want to manage websocket options directly (ping, cleanup clients, etc.).