WebSockets API¶
Yimmo WebSockets public API.
Server Settings¶
-
type ymo_ws_server_flags_t¶
Type for flags param passed in to
ymo_proto_ws_create()
.typedef uint8_t ymo_ws_server_flags_t;
-
YMO_WS_SERVER_DEFAULT¶
The receive callback will be invoked with data, as it is received from the client. Applications must buffer their own multi-message payloads and check for the FIN bit using
YMO_WS_FLAG_FIN
.#define YMO_WS_SERVER_DEFAULT 0x0
-
YMO_WS_SERVER_BUFFERED¶
The receive callback will be invoked with complete messages. The WS module will buffer multi-part messages for the application — up to
YMO_WS_MSG_LEN_MAX
bytes — and invoke the receive callback once the FIN bit is set.#define YMO_WS_SERVER_BUFFERED 0x01
-
YMO_WS_MSG_LEN_MAX¶
Max length of a buffered WS message.
Todo
Move this to runtime configuration!
# define YMO_WS_MSG_LEN_MAX 4096
Message Header¶
Note
See RFC6455: Framing for more info.
Byte 0¶
-
YMO_WS_FLAG_FIN¶
#define YMO_WS_FLAG_FIN 0x80
-
YMO_WS_OP_CONTINUATION¶
Indicates that the frame is a continuation frame.
#define YMO_WS_OP_CONTINUATION 0x00
-
YMO_WS_OP_TEXT¶
Indicates that the frame is a text frame.
#define YMO_WS_OP_TEXT 0x01
-
YMO_WS_OP_BINARY¶
Indicates that the frame is a binary frame.
#define YMO_WS_OP_BINARY 0x02
-
YMO_WS_OP_CLOSE¶
Indicates that the frame is a connection close.
#define YMO_WS_OP_CLOSE 0x08
-
YMO_WS_OP_PING¶
Indicates that the frame is a ping.
#define YMO_WS_OP_PING 0x09
-
YMO_WS_OP_PONG¶
Indicates that the frame is a pong.
#define YMO_WS_OP_PONG 0x0a
Byte 1¶
-
YMO_WS_FLAG_MASKED¶
#define YMO_WS_FLAG_MASKED 0x80
-
YMO_WS_MASK_LEN¶
#define YMO_WS_MASK_LEN 0x7f
Types¶
-
type ymo_ws_session_t¶
Opaque date type used to represent WebSocket connecctions
typedef struct ymo_ws_session ymo_ws_session_t;
-
type ymo_ws_connect_cb_t¶
WebSocket connect callback type.
- Session
a new websocket connection
- Returns
NULL
or avoid*
to custom data to associate with the session
typedef ymo_status_t (*ymo_ws_connect_cb_t)(ymo_ws_session_t* session);
-
type ymo_ws_recv_cb_t¶
WebSocket receive callback type — invoked for each packet of data.
- Session
the WebSocket session sending the message
- User_data
an optional pointer to user data associated with the session
- Flags
flags from the incoming Message Header
- Msg
frame payload
- Len
frame length
- Returns
YMO_OKAY
on success, errno.h value on failure
typedef ymo_status_t (*ymo_ws_recv_cb_t)( ymo_ws_session_t* session, void* user_data, uint8_t flags, const char* msg, size_t len);
-
type ymo_ws_close_cb_t¶
WebSocket close callback type.
- Session
the session for the closed websocket
- User_data
an optional pointer to user data associated with the session
typedef void (*ymo_ws_close_cb_t)(ymo_ws_session_t* session, void* user_data);
Session Data¶
Protocol Management¶
-
ymo_proto_t *ymo_proto_ws_create(ymo_ws_server_flags_t flags, ymo_ws_connect_cb_t connect_cb, ymo_ws_recv_cb_t recv_cb, ymo_ws_close_cb_t close_cb)¶
Create a WebSocket protocol object.
- Parameters
flags – see Server Settings
connect_cb – see
ymo_ws_connect_cb_t
recv_cb – see
ymo_ws_recv_cb_t
close_cb – see
ymo_ws_close_cb_t
- Returns
ymo_proto_t
pointer.
-
ymo_http_upgrade_handler_t *ymo_ws_http_upgrade_handler(ymo_proto_t *proto)¶
Given a websocket protocol object, return a
ymo_http_upgrade_handler_t
which handles WebSocket upgrade requests, which can be passed toymo_http_simple_init()
.
-
ymo_status_t ymo_ws_session_send(ymo_ws_session_t *session, uint8_t flags, ymo_bucket_t *payload)¶
Send a message to the given websocket session.
- Parameters
session – the websocket session
flags – flags for the outgoing Message Header
payload – a
ymo_bucket_t
encapsulating the data to be sent (NOTE: the payload can be a single bucket _or_ a chain of buckets).
- Returns
YMO_OKAY
on success;errno
value on failure.
Example:
ymo_bucket_t* my_msg = YMO_BUCKET_FROM_REF("Hello, world!", 13); return ymo_ws_session_send( session, YMO_WS_FLAG_FIN | YMO_WS_OP_TEXT, my_msg);