include/ws.h file

wsServer constants and functions.

Classes

struct ws_events
events Web Socket events types.

Functions

auto get_handshake_accept(char* wsKey, unsigned char** dest) -> int
Gets the field Sec-WebSocket-Accept on response, by an previously informed key.
auto get_handshake_response(char* hsrequest, char** hsresponse) -> int
Gets the complete response to accomplish a succesfully handshake.
auto ws_getaddress(ws_cli_conn_t* client) -> char*
Gets the IP address relative to a client connection opened by the server.
auto ws_sendframe(ws_cli_conn_t* cli, const char* msg, uint64_t size, int type) -> int
Creates and send an WebSocket frame with some payload data.
auto ws_sendframe_txt(ws_cli_conn_t* cli, const char* msg) -> int
Sends a WebSocket text frame.
auto ws_sendframe_bin(ws_cli_conn_t* cli, const char* msg, uint64_t size) -> int
Sends a WebSocket binary frame.
auto ws_get_state(ws_cli_conn_t* cli) -> int
For a given client, gets the current state for the connection, or -1 if invalid.
auto ws_close_client(ws_cli_conn_t* cli) -> int
Close the client connection for the given client with normal close code (1000) and no reason string.
auto ws_socket(struct ws_events* evs, uint16_t port, int thread_loop) -> int
Main loop for the server.

Defines

#define WS_CLSE_INVUTF8
Inconsistent message (invalid utf-8)

Global configurations

#define MAX_CLIENTS
Max clients connected simultaneously.

Key and message configurations.

#define MESSAGE_LENGTH
Message buffer length.
#define MAX_FRAME_LENGTH
Maximum frame/message length.
#define WS_KEY_LEN
WebSocket key length.
#define WS_MS_LEN
Magic string length.
#define WS_KEYMS_LEN
Accept message response length.
#define MAGIC_STRING
Magic string.

Handshake constants.

#define WS_HS_REQ
Alias for 'Sec-WebSocket-Key'.
#define WS_HS_ACCLEN
Handshake accept message length.
#define WS_HS_ACCEPT
Handshake accept message.
#define DEBUG(...)
Debug.

Frame types.

#define WS_FIN
Frame FIN.
#define WS_FIN_SHIFT
Frame FIN shift.
#define WS_FR_OP_CONT
Continuation frame.
#define WS_FR_OP_TXT
Text frame.
#define WS_FR_OP_BIN
Binary frame.
#define WS_FR_OP_CLSE
Close frame.
#define WS_FR_OP_PING
Ping frame.
#define WS_FR_OP_PONG
Pong frame.
#define WS_FR_OP_UNSUPPORTED
Unsupported frame.

Close codes

#define WS_CLSE_NORMAL
Normal close.
#define WS_CLSE_PROTERR
Protocol error.

Connection states

#define WS_STATE_CONNECTING
Connection not established yet.
#define WS_STATE_OPEN
Communicating.
#define WS_STATE_CLOSING
Closing state.
#define WS_STATE_CLOSED
Closed.

Timeout util

#define MS_TO_NS(x)
Nanoseconds macro converter.
#define TIMEOUT_MS
Timeout in milliseconds.

Function documentation

int get_handshake_accept(char* wsKey, unsigned char** dest)

Gets the field Sec-WebSocket-Accept on response, by an previously informed key.

Parameters
wsKey Sec-WebSocket-Key
dest source to be stored the value.
Returns Returns 0 if success and a negative number otherwise.

int get_handshake_response(char* hsrequest, char** hsresponse)

Gets the complete response to accomplish a succesfully handshake.

Parameters
hsrequest Client request.
hsresponse Server response.
Returns Returns 0 if success and a negative number otherwise.

char* ws_getaddress(ws_cli_conn_t* client)

Gets the IP address relative to a client connection opened by the server.

Parameters
client Client connection.
Returns Pointer the ip address, or NULL if fails.

int ws_sendframe(ws_cli_conn_t* cli, const char* msg, uint64_t size, int type)

Creates and send an WebSocket frame with some payload data.

Parameters
cli
msg Message to be send.
size Binary message size.
type Frame type.
Returns Returns the number of bytes written, -1 if error.

This routine is intended to be used to create a websocket frame for a given type e sending to the client. For higher level routines, please check ws_sendframe_txt and ws_sendframe_bin.

int ws_sendframe_txt(ws_cli_conn_t* cli, const char* msg)

Sends a WebSocket text frame.

Parameters
cli
msg Message to be send, null terminated.
Returns Returns the number of bytes written, -1 if error.

int ws_sendframe_bin(ws_cli_conn_t* cli, const char* msg, uint64_t size)

Sends a WebSocket binary frame.

Parameters
cli
msg Message to be send.
size Binary message size.
Returns Returns the number of bytes written, -1 if error.

int ws_get_state(ws_cli_conn_t* cli)

For a given client, gets the current state for the connection, or -1 if invalid.

Returns Returns the connection state or -1 if invalid client.

int ws_close_client(ws_cli_conn_t* cli)

Close the client connection for the given client with normal close code (1000) and no reason string.

Returns Returns 0 on success, -1 otherwise.

int ws_socket(struct ws_events* evs, uint16_t port, int thread_loop)

Main loop for the server.

Parameters
evs Events structure.
port Server port.
thread_loop If any value other than zero, runs the accept loop in another thread and immediately returns. If 0, runs in the same thread and blocks execution.
Returns If thread_loop != 0, returns 0. Otherwise, never returns.