WE310F5  39.00.000
M2MB WEBSOCKET

This section describes the M2MB APIs to perform websocket client operations. More...

#define WEBSOCKETC_MAX_CLIENTS   (4)
 
#define WEBSOCKET_MAX_SUBPROTOCOLS   (4)
 
enum  M2MB_WEBSOCKET_STATE_E {
  M2MB_WEBSOCKET_STATE_INITIAL_E = 0,
  M2MB_WEBSOCKET_STATE_CONNECTING_E = 1,
  M2MB_WEBSOCKET_STATE_OPEN_E = 2,
  M2MB_WEBSOCKET_STATE_CLOSING_E = 3,
  M2MB_WEBSOCKET_STATE_CLOSED_E = 4
}
 The state of the websocket. More...
 
enum  M2MB_WEBSOCKET_OPTION_E {
  M2MB_WEBSOCKET_OPTION_STATE = 1,
  M2MB_WEBSOCKET_OPTION_ERRNO = 2,
  M2MB_WEBSOCKET_OPTION_SOCKET_ERRNO = 3,
  M2MB_WEBSOCKET_OPTION_SSL_ERRNO = 4,
  M2MB_WEBSOCKET_OPTION_NUM_RX_BYTES = 5,
  M2MB_WEBSOCKET_OPTION_NUM_TX_BYTES = 6,
  M2MB_WEBSOCKET_OPTION_NUM_RX_PINGS = 7,
  M2MB_WEBSOCKET_OPTION_NUM_TX_PINGS = 8,
  M2MB_WEBSOCKET_OPTION_NUM_RX_PONGS = 9,
  M2MB_WEBSOCKET_OPTION_NUM_TX_PONGS = 10
}
 Websocket options that can be read using m2mb_websocketc_get_opt API. More...
 
enum  M2MB_WEBSOCKET_DATA_TYPE_E {
  M2MB_WEBSOCKET_DATA_TYPE_TEXT_E = 1,
  M2MB_WEBSOCKET_DATA_TYPE_BINARY_E = 2
}
 Type of data sent or received on websocket. More...
 
enum  M2MB_WEBSOCKET_EVENT_E {
  M2MB_WEBSOCKET_EVENT_CONNECT_E = 1,
  M2MB_WEBSOCKET_EVENT_MESSAGE_E = 2,
  M2MB_WEBSOCKET_EVENT_PONG_E = 3,
  M2MB_WEBSOCKET_EVENT_CLOSE_E = 4
}
 Websocket event type. Provided to M2MB_Event_CB_t callback. More...
 
enum  M2MB_WEBSOCKET_CLOSE_STATUS_E {
  M2MB_WEBSOCKET_CLOSE_STATUS_NORMAL = 1000,
  M2MB_WEBSOCKET_CLOSE_STATUS_GOING_AWAY = 1001,
  M2MB_WEBSOCKET_CLOSE_STATUS_PROTOCOL_ERROR = 1002,
  M2MB_WEBSOCKET_CLOSE_STATUS_UNSUPPORTED_MESSAGE_TYPE = 1003,
  M2MB_WEBSOCKET_CLOSE_STATUS_NO_STATUS_RECEIVED = 1005,
  M2MB_WEBSOCKET_CLOSE_STATUS_ABNORMAL_CLOSURE = 1006,
  M2MB_WEBSOCKET_CLOSE_STATUS_DATA_DOES_NOT_MATCH_MESSAGE_TYPE = 1007,
  M2MB_WEBSOCKET_CLOSE_STATUS_POLICY_VIOLATION = 1008,
  M2MB_WEBSOCKET_CLOSE_STATUS_MESSAGE_TOO_BIG = 1009,
  M2MB_WEBSOCKET_CLOSE_STATUS_EXPECTED_EXTENSION = 1010,
  M2MB_WEBSOCKET_CLOSE_STATUS_UNEXPECTED_CONDITION = 1011
}
 Connection close status. More...
 
enum  M2MB_WEBSOCKET_CONF_ID_E {
  M2MB_WEBSOCKET_CONF_ECHOMODE,
  M2MB_WEBSOCKET_CONF_HTTPHEADERS_ADD,
  M2MB_WEBSOCKET_CONF_HTTPHEADERS_CLEAR
}
 Client configuration parameters. For use with m2mb_websocketc_new. More...
 
typedef UINT32 M2MB_WEBSOCKET_HANDLE_T
 Handle to websocket context. More...
 
typedef struct M2MB_WEBSOCKET_CTX M2MB_WEBSOCKET_CTX_T
 websocket client context used with every websocket API. More...
 
typedef struct M2MB_WEBSOCKET_EVENT_INFO M2MB_WEBSOCKET_EVENT_INFO_T
 Websocket event. Received by M2MB_WEBSOCKETC_EVENT_CB_T callback registered with m2mb_websocketc_new API. More...
 
typedef void(* M2MB_WEBSOCKETC_EVENT_CB_T) (M2MB_WEBSOCKET_HANDLE_T handle, void *arg, M2MB_WEBSOCKET_EVENT_E event, M2MB_WEBSOCKET_EVENT_INFO_T *event_Info)
 Callback to receive new websocket messages. More...
 
typedef UINT32 M2MB_SSL_OBJ_HDL_T
 
typedef struct M2MB_WEBSOCKET_OPEN_PARAMS M2MB_WEBSOCKET_OPEN_PARAMS_T
 Client configuration parameters. For use with m2mb_websocketc_new. More...
 
typedef union M2MB_WEBSOCKET_CONF_PARAMS M2MB_WEBSOCKET_CFG_PARAMS_T
 Client configuration parameters. For use with m2mb_websocket_cfg. More...
 
M2MB_STATUS_T m2mb_websocket_open (M2MB_WEBSOCKET_CTX_T **pCtx, M2MB_WEBSOCKET_OPEN_PARAMS_T *pParams)
 Create a client websocket context. More...
 
M2MB_STATUS_T m2mb_websocket_close (M2MB_WEBSOCKET_CTX_T *pCtx)
 Closes the websocket connection. More...
 
M2MB_STATUS_T m2mb_websocket_connect (M2MB_WEBSOCKET_CTX_T *pCtx, INT8 *p_server, UINT16 port, char *pResource_path)
 Establishes a websocket connection. More...
 
M2MB_STATUS_T m2mb_websocket_cfg (M2MB_WEBSOCKET_CTX_T *pCtx, M2MB_WEBSOCKET_CONF_ID_E confid, M2MB_WEBSOCKET_CFG_PARAMS_T *cfg)
 Enables or disables echo mode. More...
 
int m2mb_websocket_send (M2MB_WEBSOCKET_CTX_T *pCtx, char *pMsg, UINT32 msglen, M2MB_WEBSOCKET_DATA_TYPE_E datatype, UINT8 end_of_message)
 Sends application data. More...
 
M2MB_STATUS_T m2mb_websocket_ping (M2MB_WEBSOCKET_CTX_T *pCtx, char *pPing_data, UINT32 ping_data_length)
 Send a websocket ping. More...
 
M2MB_STATUS_T m2mb_websocket_pong (M2MB_WEBSOCKET_CTX_T *pCtx, char *pPong_data, UINT32 pong_data_length)
 Send a websocket pong. More...
 
M2MB_STATUS_T m2mb_websocket_get_opt (M2MB_WEBSOCKET_CTX_T *pCtx, M2MB_WEBSOCKET_OPTION_E opt, void *opt_Value, UINT32 *opt_Length)
 Get properties of the websocket. More...
 

Detailed Description

This section describes the M2MB APIs to perform websocket client operations.

Macro Definition Documentation

◆ WEBSOCKET_MAX_SUBPROTOCOLS

#define WEBSOCKET_MAX_SUBPROTOCOLS   (4)

max number of subprotocols supported.

Definition at line 52 of file m2mb_websocket.h.

◆ WEBSOCKETC_MAX_CLIENTS

#define WEBSOCKETC_MAX_CLIENTS   (4)

max number of clients supported.

Definition at line 50 of file m2mb_websocket.h.

Typedef Documentation

◆ M2MB_SSL_OBJ_HDL_T

Handle to an SSL object. This is obtained from a call to qapi_Net_SSL_Obj_New(). The handle is freed with a call to qapi_Net_SSL_Obj_Free().

Definition at line 183 of file m2mb_websocket.h.

◆ M2MB_WEBSOCKET_CFG_PARAMS_T

Client configuration parameters. For use with m2mb_websocket_cfg.

◆ M2MB_WEBSOCKET_CTX_T

websocket client context used with every websocket API.

◆ M2MB_WEBSOCKET_EVENT_INFO_T

Websocket event. Received by M2MB_WEBSOCKETC_EVENT_CB_T callback registered with m2mb_websocketc_new API.

◆ M2MB_WEBSOCKET_HANDLE_T

Handle to websocket context.

Definition at line 120 of file m2mb_websocket.h.

◆ M2MB_WEBSOCKET_OPEN_PARAMS_T

Client configuration parameters. For use with m2mb_websocketc_new.

◆ M2MB_WEBSOCKETC_EVENT_CB_T

typedef void(* M2MB_WEBSOCKETC_EVENT_CB_T) (M2MB_WEBSOCKET_HANDLE_T handle, void *arg, M2MB_WEBSOCKET_EVENT_E event, M2MB_WEBSOCKET_EVENT_INFO_T *event_Info)

Callback to receive new websocket messages.

Definition at line 165 of file m2mb_websocket.h.

Enumeration Type Documentation

◆ M2MB_WEBSOCKET_CLOSE_STATUS_E

Connection close status.

Enumerator
M2MB_WEBSOCKET_CLOSE_STATUS_NORMAL 

normal closure.

M2MB_WEBSOCKET_CLOSE_STATUS_GOING_AWAY 

end point is going away.

M2MB_WEBSOCKET_CLOSE_STATUS_PROTOCOL_ERROR 

protocol error.

M2MB_WEBSOCKET_CLOSE_STATUS_UNSUPPORTED_MESSAGE_TYPE 

unsupported message type.

M2MB_WEBSOCKET_CLOSE_STATUS_NO_STATUS_RECEIVED 

No close status specified.

M2MB_WEBSOCKET_CLOSE_STATUS_ABNORMAL_CLOSURE 

Peer closed connection without sending close frame.

M2MB_WEBSOCKET_CLOSE_STATUS_DATA_DOES_NOT_MATCH_MESSAGE_TYPE 

data does not match message type (e.g., non-UTF-8 in text message).

M2MB_WEBSOCKET_CLOSE_STATUS_POLICY_VIOLATION 

policy violation.

M2MB_WEBSOCKET_CLOSE_STATUS_MESSAGE_TOO_BIG 

message too big to process.

M2MB_WEBSOCKET_CLOSE_STATUS_EXPECTED_EXTENSION 

expected server to negotiate extension.

M2MB_WEBSOCKET_CLOSE_STATUS_UNEXPECTED_CONDITION 

unexpected condition.

Definition at line 103 of file m2mb_websocket.h.

◆ M2MB_WEBSOCKET_CONF_ID_E

Client configuration parameters. For use with m2mb_websocketc_new.

Enumerator
M2MB_WEBSOCKET_CONF_ECHOMODE 
M2MB_WEBSOCKET_CONF_HTTPHEADERS_ADD 
M2MB_WEBSOCKET_CONF_HTTPHEADERS_CLEAR 

Definition at line 216 of file m2mb_websocket.h.

◆ M2MB_WEBSOCKET_DATA_TYPE_E

Type of data sent or received on websocket.

Enumerator
M2MB_WEBSOCKET_DATA_TYPE_TEXT_E 

UTF-8 text data.

M2MB_WEBSOCKET_DATA_TYPE_BINARY_E 

Binary data.

Definition at line 85 of file m2mb_websocket.h.

◆ M2MB_WEBSOCKET_EVENT_E

Websocket event type. Provided to M2MB_Event_CB_t callback.

Enumerator
M2MB_WEBSOCKET_EVENT_CONNECT_E 

websocket connection established.

M2MB_WEBSOCKET_EVENT_MESSAGE_E 

message or partial message received.

M2MB_WEBSOCKET_EVENT_PONG_E 

pong received.

M2MB_WEBSOCKET_EVENT_CLOSE_E 

websocket connection closed or failed to connect.

Definition at line 93 of file m2mb_websocket.h.

◆ M2MB_WEBSOCKET_OPTION_E

Websocket options that can be read using m2mb_websocketc_get_opt API.

Enumerator
M2MB_WEBSOCKET_OPTION_STATE 

Current state of websocket, e.g. OPEN, CLOSED.

M2MB_WEBSOCKET_OPTION_ERRNO 

Last RX/TX or handshake error.

M2MB_WEBSOCKET_OPTION_SOCKET_ERRNO 

Last socket error (if errno is M2MB_WEBSOCKET_STATUS_SOCKET_ERROR).

M2MB_WEBSOCKET_OPTION_SSL_ERRNO 

Last SSL error (if errno is M2MB_WEBSOCKET_STATUS_SSL_ERROR).

M2MB_WEBSOCKET_OPTION_NUM_RX_BYTES 

Total number of bytes received after connection established.

M2MB_WEBSOCKET_OPTION_NUM_TX_BYTES 

Total number of bytes sent after connection established.

M2MB_WEBSOCKET_OPTION_NUM_RX_PINGS 

Total number of pings received.

M2MB_WEBSOCKET_OPTION_NUM_TX_PINGS 

Total number of pings sent.

M2MB_WEBSOCKET_OPTION_NUM_RX_PONGS 

Total number of pongs received.

M2MB_WEBSOCKET_OPTION_NUM_TX_PONGS 

Total number of pongs sent.

Definition at line 69 of file m2mb_websocket.h.

◆ M2MB_WEBSOCKET_STATE_E

The state of the websocket.

Enumerator
M2MB_WEBSOCKET_STATE_INITIAL_E 

Websocket handshake has not started.

M2MB_WEBSOCKET_STATE_CONNECTING_E 

Websocket handshake in progress.

M2MB_WEBSOCKET_STATE_OPEN_E 

Websocket handshake complete.

M2MB_WEBSOCKET_STATE_CLOSING_E 

Closing handshake started.

M2MB_WEBSOCKET_STATE_CLOSED_E 

Websocket closed.

Definition at line 58 of file m2mb_websocket.h.

Function Documentation

◆ m2mb_websocket_cfg()

M2MB_STATUS_T m2mb_websocket_cfg ( M2MB_WEBSOCKET_CTX_T pCtx,
M2MB_WEBSOCKET_CONF_ID_E  confid,
M2MB_WEBSOCKET_CFG_PARAMS_T cfg 
)

Enables or disables echo mode.

Enables or disables echo mode for particular websocket.

Parameters
[in]pClientCtxPointer to store websocket client context of type M2MB_WEBSOCKETC_CTX_T.
[in]echo_modeIf it is 1, it enables echo mode, otherwise disables it.
Returns

M2MB_OK in case of success M2MB_ERROR in case of error
Example
void m2m_s2w_websocket_event_handler(M2MB_WEBSOCKET_HDL_T websocket_handle,
void * arg, M2MB_WEBSOCKET_EVENT_E event, M2MB_WEBSOCKET_EVENT_INFO_T *event_info)
{
}
void main(void)
{
//module should have acquire ip address to create websocket client.
INT8 *ServerAddr = "192.168.16.120";
UINT32 Server_port = 8082;
INT8* Resource_path = "/";
UINT8 echo_mode = TRUE;
char *pWebsockCtx = malloc(sizeof(M2MB_WEBSOCKETC_CTX_T));
M2MB_STATUS_T status;
if(pWebsockCtx == NULL)
return M2MB_ERROR;
status = m2mb_websocketc_new( pWebsockCtx, &config,
(VOID *)m2m_s2w_websocket_event_handler);
status = m2mb_websocketc_connect( pWebsockCtx,
ServerAddr, Server_port, Resource_path );
status = m2mb_websocketc_echo( pWebsockCtx, echo_mode );
if(M2MB_ERROR == m2mb_websocketc_close( pWebsockCtx))
return M2MB_ERROR;
else
return M2MB_OK;
}

◆ m2mb_websocket_close()

M2MB_STATUS_T m2mb_websocket_close ( M2MB_WEBSOCKET_CTX_T pCtx)

Closes the websocket connection.

Closes the websocket connection and notify the sender. This also closes the underlying TCP socket and SSL connection.

Parameters
[in]pClientCtxPointer to store websocket client context of type M2MB_WEBSOCKETC_CTX_T.
Returns

M2MB_OK in case of success M2MB_ERROR in case of error
Example
void m2m_s2w_websocket_event_handler(M2MB_WEBSOCKET_HDL_T websocket_handle,
void * arg, M2MB_WEBSOCKET_EVENT_E event, M2MB_WEBSOCKET_EVENT_INFO_T *event_info)
{
}
void main(void)
{
//module should have acquired IP address to create websocket client.
INT8 *ServerAddr = "192.168.16.120";
UINT32 Server_port = 8082;
INT8* Resource_path = "/";
char *pWebsockCtx = malloc(sizeof(M2MB_WEBSOCKETC_CTX_T));
M2MB_STATUS_T status;
if(pWebsockCtx == NULL)
return M2MB_ERROR;
status = m2mb_websocketc_new( pWebsockCtx, &config,
(VOID *)m2m_s2w_websocket_event_handler);
status = m2mb_websocketc_connect( pWebsockCtx,
ServerAddr, Server_port, Resource_path );
if(M2MB_ERROR == m2mb_websocketc_close( pWebsockCtx))
return M2MB_ERROR;
else
return M2MB_OK;
}

◆ m2mb_websocket_connect()

M2MB_STATUS_T m2mb_websocket_connect ( M2MB_WEBSOCKET_CTX_T pCtx,
INT8 p_server,
UINT16  port,
char *  pResource_path 
)

Establishes a websocket connection.

Establishes a websocket connection to the server.

Parameters
[in]pClientCtxPointer to store websocket client context of type M2MB_WEBSOCKETC_CTX_T.
[in]p_serverPointer to the Server hostname or IP address.
[in]portServer port.
[in]pResource_pathServer pResource_path.
Returns

M2MB_OK in case of success M2MB_ERROR in case of error
Example
void m2m_s2w_websocket_event_handler(M2MB_WEBSOCKET_HDL_T websocket_handle,
void * arg, M2MB_WEBSOCKET_EVENT_E event, M2MB_WEBSOCKET_EVENT_INFO_T *event_info)
{
}
void main(void)
{
//module should have acquired IP address to create websocket client.
INT8 *ServerAddr = "192.168.16.120";
UINT32 Server_port = 8082;
INT8* Resource_path = "/";
char *pWebsockCtx = malloc(sizeof(M2MB_WEBSOCKETC_CTX_T));
M2MB_STATUS_T status;
if(pWebsockCtx == NULL)
return M2MB_ERROR;
status = m2mb_websocketc_new( pWebsockCtx, &config,
(VOID *)m2m_s2w_websocket_event_handler);
status = m2mb_websocketc_connect( pWebsockCtx,
ServerAddr, Server_port, Resource_path );
}

◆ m2mb_websocket_get_opt()

M2MB_STATUS_T m2mb_websocket_get_opt ( M2MB_WEBSOCKET_CTX_T pCtx,
M2MB_WEBSOCKET_OPTION_E  opt,
void *  opt_Value,
UINT32 opt_Length 
)

Get properties of the websocket.

Get properties of the websocket. For example, this can be used to get the negotiated sub-protocol.

Parameters
[in]pClientCtxPointer to store websocket client context of type M2MB_WEBSOCKETC_CTX_T.
[in]optOption name to get of type M2MB_WEBSOCKET_OPTION_E.
[in]opt_ValuePointer to copy option value.
[in]opt_LengthPointer to indicate option length.
Returns

M2MB_OK in case of success M2MB_ERROR in case of error
Example
void m2m_s2w_websocket_event_handler(M2MB_WEBSOCKET_HDL_T websocket_handle,
void * arg, M2MB_WEBSOCKET_EVENT_E event, M2MB_WEBSOCKET_EVENT_INFO_T *event_info)
{
}
void main(void)
{
//module should have acquired IP address to create websocket client.
INT8 *ServerAddr = "192.168.16.120";
UINT32 Server_port = 8082;
INT8* Resource_path = "/";
INT32 opt_Value;
UINT32 opt_Length = 0;
char *pWebsockCtx = malloc(sizeof(M2MB_WEBSOCKETC_CTX_T));
M2MB_STATUS_T status;
if(pWebsockCtx == NULL)
return M2MB_ERROR;
status = m2mb_websocketc_new( pWebsockCtx, &config,
(VOID *)m2m_s2w_websocket_event_handler);
status = m2mb_websocketc_connect( pWebsockCtx,
ServerAddr, Server_port, Resource_path );
status = m2mb_websocketc_get_opt(pWebsockCtx,
M2MB_WEBSOCKET_OPTION_STATE , &opt_Value, &opt_Length);
}

◆ m2mb_websocket_open()

M2MB_STATUS_T m2mb_websocket_open ( M2MB_WEBSOCKET_CTX_T **  pCtx,
M2MB_WEBSOCKET_OPEN_PARAMS_T pParams 
)

Create a client websocket context.

Creates a web socket client context.

Parameters
[in]pClientCtxPointer to store websocket client context of type M2MB_WEBSOCKETC_CTX_T.
[in]pConfigPointer to the user configuration of type M2MB_WEBSOCKET_CLIENT_CONFIG_T.
[in]pUsercbUser call back of type M2MB_WEBSOCKETC_EVENT_CB_T.
[in]sslhandleUser SSL handle.
Returns

M2MB_OK in case of success M2MB_ERROR in case of error
Example
void m2m_s2w_websocket_event_handler(M2MB_WEBSOCKET_HDL_T websocket_handle,
void * arg, M2MB_WEBSOCKET_EVENT_E event, M2MB_WEBSOCKET_EVENT_INFO_T *event_info)
{
}
void main(void)
{
//module should have acquired IP address to create websocket client.
char *pWebsockCtx = malloc(sizeof(M2MB_WEBSOCKETC_CTX_T));
M2MB_STATUS_T status;
if(pWebsockCtx == NULL)
return M2MB_ERROR;
status = m2mb_websocketc_new( pWebsockCtx, &config,
(VOID *)m2m_s2w_websocket_event_handler);
}

◆ m2mb_websocket_ping()

M2MB_STATUS_T m2mb_websocket_ping ( M2MB_WEBSOCKET_CTX_T pCtx,
char *  pPing_data,
UINT32  ping_data_length 
)

Send a websocket ping.

Send a websocket ping. The resulting pong will be receive by the registered pong callback.

Parameters
[in]pClientCtxPointer to store websocket client context of type M2MB_WEBSOCKETC_CTX_T.
[in]pPing_dataOptional data to include in the ping.
[in]ping_data_lengthLength of data.
Returns

M2MB_OK in case of success M2MB_ERROR in case of error
Example
void m2m_s2w_websocket_event_handler(M2MB_WEBSOCKET_HDL_T websocket_handle,
void * arg, M2MB_WEBSOCKET_EVENT_E event, M2MB_WEBSOCKET_EVENT_INFO_T *event_info)
{
}
void websocket_event_handler(M2MB_WEBSOCKET_HDL_T websocket_handle,
void * arg, M2MB_WEBSOCKET_EVENT_E event,
{
M2MB_WEBSOCKETC_CTX_T *pClientCtx = (M2MB_WEBSOCKETC_CTX_T*) arg;
switch (event)
{
{
//websocket pong received with %d byte payload\n",event_info->data_Length);
}
break;
}
}
void main(void)
{
//module should have acquired IP address to create websocket client.
INT8 *ServerAddr = "192.168.16.120";
UINT32 Server_port = 8082;
INT8* Resource_path = "/";
char *pWebsockCtx = malloc(sizeof(M2MB_WEBSOCKETC_CTX_T));
M2MB_STATUS_T status;
if(pWebsockCtx == NULL)
return M2MB_ERROR;
status = m2mb_websocketc_new( pWebsockCtx, &config,
(VOID *)websocket_event_handler);
status = m2mb_websocketc_connect( pWebsockCtx,
ServerAddr, Server_port, Resource_path );
status = m2mb_websocketc_ping( pWebsockCtx, "optional ping data",
strlen("optional ping data"));
}

◆ m2mb_websocket_pong()

M2MB_STATUS_T m2mb_websocket_pong ( M2MB_WEBSOCKET_CTX_T pCtx,
char *  pPong_data,
UINT32  pong_data_length 
)

Send a websocket pong.

Send a websocket pong. The application may choose to send a pong unidirectionally without a ping for keep alive.

Parameters
[in]pClientCtxPointer to store websocket client context of type M2MB_WEBSOCKETC_CTX_T.
[in]pPong_dataOptional data to include in the pong.
[in]pong_data_lengthLength of data.
Returns

M2MB_OK in case of success M2MB_ERROR in case of error
Example
void websocket_event_handler(M2MB_WEBSOCKET_HDL_T websocket_handle,
void * arg, M2MB_WEBSOCKET_EVENT_E event,
{
M2MB_WEBSOCKETC_CTX_T *pClientCtx = (M2MB_WEBSOCKETC_CTX_T*) arg;
switch (event)
{
{
//websocket pong received with %d byte payload\n",event_info->data_Length);
}
break;
}
}
void main(void)
{
//module should have acquire ip address to create websocket client.
INT8 *ServerAddr = "192.168.16.120";
UINT32 Server_port = 8082;
INT8* Resource_path = "/";
char *pWebsockCtx = malloc(sizeof(M2MB_WEBSOCKETC_CTX_T));
M2MB_STATUS_T status;
if(pWebsockCtx == NULL)
return M2MB_ERROR;
status = m2mb_websocketc_new( pWebsockCtx, &config,
(VOID *)websocket_event_handler);
status = m2mb_websocketc_connect( pWebsockCtx,
ServerAddr, Server_port, Resource_path );
status = m2mb_websocketc_pong( pWebsockCtx, "optional pong data",
strlen("optional pong data"));
}

◆ m2mb_websocket_send()

int m2mb_websocket_send ( M2MB_WEBSOCKET_CTX_T pCtx,
char *  pMsg,
UINT32  msglen,
M2MB_WEBSOCKET_DATA_TYPE_E  datatype,
UINT8  end_of_message 
)

Sends application data.

Sends application data.

Parameters
[in]pClientCtxPointer to store websocket client context of type M2MB_WEBSOCKETC_CTX_T.
[in]pMsgData Data to send.
[in]msglenData_Length Length of data.
[in]datatype1 for text data, and 2 for binary data.
[in]end_of_messageend_Of_Message If data contains an entire message, this should be set to 1. Messages can be fragmented. If end_Of_Message is 0, this sends the data as a fragment of a message. The last fragment of the message should be sent with end_Of_Message set to 1.
Returns

M2MB_OK in case of success M2MB_ERROR in case of error
Example
void m2m_s2w_websocket_event_handler(M2MB_WEBSOCKET_HDL_T websocket_handle,
void * arg, M2MB_WEBSOCKET_EVENT_E event, M2MB_WEBSOCKET_EVENT_INFO_T *event_info)
{
}
void main(void)
{
//module should have acquired IP address to create websocket client.
INT8 *ServerAddr = "192.168.16.120";
UINT32 Server_port = 8082;
INT8* Resource_path = "/";
char *pWebsockCtx = malloc(sizeof(M2MB_WEBSOCKETC_CTX_T));
M2MB_STATUS_T status;
if(pWebsockCtx == NULL)
return M2MB_ERROR;
status = m2mb_websocketc_new( pWebsockCtx, &config,
(VOID *)m2m_s2w_websocket_event_handler);
status = m2mb_websocketc_connect( pWebsockCtx,
ServerAddr, Server_port, Resource_path );
if((ret = m2mb_websocketc_send(pWebsockCtx, "data to send",strlen("data to send"),
1, 1)) == M2MB_ERROR )
{
return M2MB_ERROR;
}
else
{
return M2MB_OK;
}
if(M2MB_ERROR == m2mb_websocketc_close( pWebsockCtx))
return M2MB_ERROR;
else
return M2MB_OK;
}