WebSocket Callback Functions

The following callback APIs are called by the web_websocket functions. For a complete listing, see Miscellaneous Functions.

onOpenCB

void onOpenCB(const char* connectionID, const char * AccumulatedHeadersStr, int AccumulatedHeadersLen)

ConnectionIDThe connection ID.
AccumulatedHeadersStrThe accumulated header string.
AccumulatedHeadersLenThe length of the header string.

This callback is triggered when the WebSocket connection's readyState changes to OPEN. This indicates that the connection is ready.

onMessageCB

void onMessageCB(const char* connectionID, int isbinary, char *data, int length)

ConnectionIDThe connection ID.
isbinaryAn indicator whether the string is binary or text.
DataThe buffer received from the server.
Data LengthThe length of the buffer.

This callback is triggered when a message is received from the server.

onCloseCB

void onCloseCB(const char* connectionID, int isClosedByClient, int code, const char* reason, int length)

ConnectionIDThe connection ID.
isClosedByClientAn indicator whether the connection was closed by the client.
CodeA 4-digit close code.
ReasonA string indicating the reason of the closure.
Length

The length of the reason message (0 for NULL).

This callback is triggered when the connection to the server is closed.

onErrorCB

void onErrorCB(const char* connectionID, const char * message, int length)

ConnectionIDThe connection ID.
MessageThe message of the error event (may be NULL).
LengthThe length of the error message (0 for NULL).

This callback is triggered when an error is received from the server.

Examples

Example - OnOpenCB

void OnOpenCB0 (const char* connectionID,
const char * AccumulatedHeadersStr,
int AccumulatedHeadersLen)
{
// lr_output_message("WebSocket ID = %s connected", connectionID);
// lr_save_param_regexp (AccumulatedHeadersStr,
//			  AccumulatedHeadersLen,
//			  "RegExp=Sec-WebSocket-Accept: (.+)\\r\\n",
//			  "ResultParam=Accept",
//			   LAST);
// lr_output_message("Sec-WebSocket-Accept = [%s]",
// lr_eval_string("{Accept}"));
}

Example - OnMessageCB

void OnMessageCB0 (const char* connectionID,
int isbinary,
const char * data,
int length)
{
//if (isbinary) {
//lr_output_message("WebSocket ID = %s. [%d] bytes binary message received.", connectionID, length);
//}
//else {
//lr_output_message("WebSocket ID = %s. [%d] bytes text message received.", connectionID, length);
//}
}

Example - OnErrorCB

			
void OnErrorCB0 (const char* connectionID,
const char * message,
int length)
{
//lr_output_message("WebSocket ID = %s error occurred. Error message = %s", connectionID, message);
}

Example - OnCloseCB

void OnCloseCB0 (const char* connectionID,
int isClosedByClient,
int code,
const char* reason,
int length)
{
//lr_output_message("WebSocket ID = %s closed. CloseCode= %d, CloseReason=%s", connectionID, code, reason);
}