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, 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, 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.

Example

function OnOpenCB0(){
    lr.logMessage('In function:' + arguments.callee);
    for(var key in arguments){
        lr.logMessage( 'arguments[' + key + ']=' + arguments[key] );
    }
}

function OnErrorCB0(){
    lr.logMessage('In function:' + arguments.callee);
    for(var key in arguments){
        lr.logMessage( 'arguments[' + key + ']=' + arguments[key] );
    }
}

function Action()
{    
    web.websocketConnect({
       id:'0',
       uri:'ws://echo.websocket.org',
       origin:'https://www.websocket.org/echo.html',
       onOpenCB: 'OnOpenCB0',
       onErrorCB:'OnErrorCB0'
     });    

    return lr.pass
}