Skip to content

File syn_websocket.h

FileList > net > syn_websocket.h

Go to the source code of this file

WebSocket protocol support on httpd.

  • #include "../common/syn_defs.h"
  • #include "../port/syn_port_socket.h"
  • #include "../pt/syn_pt.h"
  • #include "../sched/syn_task.h"
  • #include "syn_httpd.h"

Classes

Type Name
struct SYN_WebsocketSession
WebSocket session context.

Public Types

Type Name
enum SYN_WebsocketState
WebSocket connection states.

Public Functions

Type Name
SYN_Status syn_websocket_send (SYN_WebsocketSession * ws, uint8_t opcode, const void * data, size_t len)
Send a frame over WebSocket.
SYN_PT_Status syn_websocket_task (SYN_PT * pt, SYN_Task * task)
Background task for polling active WebSockets.
SYN_Status syn_websocket_upgrade (const SYN_HttpdRequest * req, SYN_HttpdResponse * resp, SYN_WebsocketSession * ws, void(*)(const uint8_t *payload, size_t len, uint8_t opcode, void *ctx) on_message, void * ctx)
Handle upgrading a connection in an httpd route handler to WebSocket.

Public Types Documentation

enum SYN_WebsocketState

WebSocket connection states.

enum SYN_WebsocketState {
    SYN_WS_STATE_CLOSED,
    SYN_WS_STATE_CONNECTED
};


Public Functions Documentation

function syn_websocket_send

Send a frame over WebSocket.

SYN_Status syn_websocket_send (
    SYN_WebsocketSession * ws,
    uint8_t opcode,
    const void * data,
    size_t len
) 

Formats a WebSocket packet header and writes the payload to the socket.

Parameters:

  • ws WebSocket session context.
  • opcode Frame type opcode (e.g., text, binary, ping).
  • data Payload data to send.
  • len Length of payload in bytes.

Returns:

SYN_OK on success, or socket error code.


function syn_websocket_task

Background task for polling active WebSockets.

SYN_PT_Status syn_websocket_task (
    SYN_PT * pt,
    SYN_Task * task
) 

Parameters:

  • pt Cooperative protothread state tracker.
  • task Scheduler task control block.

Returns:

PT_WAITING or PT_EXITED status.


function syn_websocket_upgrade

Handle upgrading a connection in an httpd route handler to WebSocket.

SYN_Status syn_websocket_upgrade (
    const SYN_HttpdRequest * req,
    SYN_HttpdResponse * resp,
    SYN_WebsocketSession * ws,
    void(*)(const uint8_t *payload, size_t len, uint8_t opcode, void *ctx) on_message,
    void * ctx
) 

Performs handshake, sends 101 status, sets upgraded = true on the response.

Parameters:

  • req Incoming HTTP upgrade request.
  • resp HTTP response interface to write upgrade headers to.
  • ws WebSocket session context to initialize.
  • on_message Handled frame callback function.
  • ctx User context pointer passed through to callback.

Returns:

SYN_OK on successful handshake, SYN_ERROR on negotiation failure.



The documentation for this class was generated from the following file src/syntropic/net/syn_websocket.h