Functions#

This section provides a reference to the WebSocket Client API functions.

Functions#

sl_websocket_init(sl_websocket_client_t *handle, const sl_websocket_config_t *config)

Initialize the WebSocket client.

sl_websocket_connect(sl_websocket_client_t *handle)

Connect to a WebSocket server.

sl_websocket_send_frame(sl_websocket_client_t *handle, const sl_websocket_send_request_t *send_request)

Send a WebSocket frame.

sl_websocket_close(sl_websocket_client_t *handle)

Close the WebSocket connection.

sl_websocket_deinit(sl_websocket_client_t *handle)

Deinitialize the WebSocket client.

Extracts the WebSocket opcode from a given socket ID.

Function Documentation#

sl_websocket_init#

sl_websocket_error_t sl_websocket_init (sl_websocket_client_t * handle, const sl_websocket_config_t * config)

Initialize the WebSocket client.

Parameters
[out]handle

Pointer to the WebSocket client structure. Must not be NULL.

[in]config

Pointer to the WebSocket client configuration constant structure. Must not be NULL.

This function initializes the WebSocket client with the provided configuration. It allocates necessary resources and sets up the client for connection.

Returns

  • sl_websocket_error_t - Error code indicating the result of the operation.


Definition at line 39 of file components/service/sl_websocket_client/inc/sl_websocket_client.h

sl_websocket_connect#

sl_websocket_error_t sl_websocket_connect (sl_websocket_client_t * handle)

Connect to a WebSocket server.

Parameters
[in]handle

Pointer to the WebSocket client structure. Must not be NULL.

This function creates a socket, binds it, and connects to the specified WebSocket server.

  • The WebSocket handle should be initialized using sl_websocket_init before calling this function.

Returns

  • sl_websocket_error_t - Error code indicating the result of the operation.


Definition at line 56 of file components/service/sl_websocket_client/inc/sl_websocket_client.h

sl_websocket_send_frame#

sl_websocket_error_t sl_websocket_send_frame (sl_websocket_client_t * handle, const sl_websocket_send_request_t * send_request)

Send a WebSocket frame.

Parameters
[in]handle

Pointer to the WebSocket client structure. Must not be NULL.

[in]send_request

Pointer to the send request constant structure containing the frame details. Must not be NULL.

This function sends a WebSocket frame to the server. Masking is taken care of by the firmware.

Returns

  • sl_websocket_error_t - Error code indicating the result of the operation.

Note

  • Masking refers to applying a random 32-bit mask to the data sent to the server to ensure data integrity and security.

  • The following table lists the maximum individual chunk of data that can be sent over each supported protocol. The length of the payload is specified in the length field of the sl_websocket_send_request_t structure.

Protocol

Maximum data chunk (bytes)

WebSocket

1450 bytes

WebSocket over SSL

1362 bytes


Definition at line 86 of file components/service/sl_websocket_client/inc/sl_websocket_client.h

sl_websocket_close#

sl_websocket_error_t sl_websocket_close (sl_websocket_client_t * handle)

Close the WebSocket connection.

Parameters
[in]handle

Pointer to the WebSocket client structure. Must not be NULL.

This function closes the WebSocket connection and cleans up resources.

Returns

  • sl_websocket_error_t - Error code indicating the result of the operation.


Definition at line 104 of file components/service/sl_websocket_client/inc/sl_websocket_client.h

sl_websocket_deinit#

sl_websocket_error_t sl_websocket_deinit (sl_websocket_client_t * handle)

Deinitialize the WebSocket client.

Parameters
[in]handle

Pointer to the WebSocket client structure. Must not be NULL.

This function deinitializes the WebSocket client by freeing allocated resources and resetting the state. It should be called only after the WebSocket connection has been closed because it also attempts to close the socket as part of its cleanup process if the socket remains open after the sl_websocket_close is invoked. Therefore, calling this function is mandatory whenever a WebSocket connection is terminated, ensuring proper cleanup.

  • The WebSocket handle should be initialized using sl_websocket_init before calling this function.

Returns

  • sl_websocket_error_t - Error code indicating the result of the operation.


Definition at line 123 of file components/service/sl_websocket_client/inc/sl_websocket_client.h

sl_si91x_get_opcode_from_socket_id#

sl_websocket_opcode_t sl_si91x_get_opcode_from_socket_id (uint16_t socket_id)

Extracts the WebSocket opcode from a given socket ID.

Parameters
[in]socket_id

The 16-bit socket ID from which to extract the opcode.

This function determines the opcode of the WebSocket from the socket ID. The socket_id is obtained from the recv_data_callback from the sl_si91x_socket_metadata_t parameter.

Returns

  • sl_websocket_opcode_t - The extracted WebSocket opcode.


Definition at line 138 of file components/service/sl_websocket_client/inc/sl_websocket_client.h