WebSocketReceiveMessage Function

Action

Receives a WebSocket message from the server. You must establish a synchronous WebSocket connection (with the function WebSocketConnectSync) beforehand. Do not use the function WebSocketConnect in this case, as this will establish an asynchronous WebSocket connection.

Include file

webapi.bdh

Syntax

WebSocketReceiveMessage( in hWebSocket    : number,
                         out sData        : dstring optional,
                         out nMessageKind : number optional,
                         in fTimeout      : double optional ): boolean

Parameter Description
hWebSocket A handle, which identifies the WebSocket. To retrieve this handle, use the WebSocketConnectSync() function.
sData Will contain the WebSocket message received from the server.

If nMessageKind is of type text, sData will contain the text converted in the current codepage.

If nMessageKind is of type binary, sData will contain the bytes received from the server.

nMessageKind Will contain the kind of WebSocket message received from the server. Can be one of the following:
  • WEBSOCKET_MESSAGEKIND_TEXT := 1; // text message
  • WEBSOCKET_MESSAGEKIND_BINARY := 2; // binary message
fTimeout Defines the timeout (in seconds) for waiting for a WebSocket message from the server. If no message is received within this timespan, the function returns false. The default value is 0 (infinite).

Return value

  • true if successful

  • false otherwise

Example

 transaction TMain
  var
    hWebSocket0 : number;
    sMessage : string;
    nMessageKind : number;
  begin
    
    // SYNCHRONOUS: Messages from server via WebSocketReceiveMessage
    
    hWebSocket0 := WebSocketConnectSync("http://lnz-testsite:90/websocketechoexample/websocketecho");
      
    ThinkTime(2.1);
    WebSocketSendTextMessage(hWebSocket0, "Rock it with HTML5 WebSocket");
    
    WebSocketReceiveMessage(hWebSocket0, sMessage, nMessageKind);
    
    Print("Received message: '" + sMessage + "'");
    if (nMessageKind = WEBSOCKET_MESSAGEKIND_TEXT) then
      Print("Message Kind: TEXT");
    elseif (nMessageKind = WEBSOCKET_MESSAGEKIND_BINARY) then
      Print("Message Kind: BINARY");
    end;
    

    ThinkTime(3.8);
    WebSocketSendTextMessage(hWebSocket0, "Hello World!");

    WebSocketReceiveMessage(hWebSocket0, sMessage, nMessageKind);

    Print("Received message: '" + sMessage + "'");
    if (nMessageKind = WEBSOCKET_MESSAGEKIND_TEXT) then
      Print("Message Kind: TEXT");
    elseif (nMessageKind = WEBSOCKET_MESSAGEKIND_BINARY) then
      Print("Message Kind: BINARY");
    end;

    
    ThinkTime(2.8);
    WebSocketSendTextMessage(hWebSocket0, "Bye");

    WebSocketReceiveMessage(hWebSocket0, sMessage, nMessageKind);
    
    Print("Received message: '" + sMessage + "'");
    if (nMessageKind = WEBSOCKET_MESSAGEKIND_TEXT) then
      Print("Message Kind: TEXT");
    elseif (nMessageKind = WEBSOCKET_MESSAGEKIND_BINARY) then
      Print("Message Kind: BINARY");
    end;

    
    // NO MESSAGE FROM SERVER => Timeout, expect error to be raised for the following WebSocketReceiveMessage() (after 4.0 seconds of waiting)
    WebSocketReceiveMessage(hWebSocket0, sMessage, nMessageKind, 4.0); // specify 4.0 seconds timeout, default is infinite


    ThinkTime(1.0);
    WebSocketClose(hWebSocket0);
  end TMain;