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)
| ConnectionID | The connection ID. |
| AccumulatedHeadersStr | The accumulated header string. |
| AccumulatedHeadersLen | The 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)
| ConnectionID | The connection ID. |
| isbinary | An indicator whether the string is binary or text. |
| Data | The buffer received from the server. |
| Data Length | The 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)
| ConnectionID | The connection ID. |
| isClosedByClient | An indicator whether the connection was closed by the client. |
| Code | A 4-digit close code. |
| Reason | A 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)
| ConnectionID | The connection ID. |
| Message | The message of the error event (may be NULL). |
| Length | The 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);
}

