WebSocketConnectSync Function

Action

Connects to a WebSocket server and establishes a synchronous bidirectional communication channel.

Include file

webapi.bdh

Syntax

WebSocketConnectSync( in sUrl : string ): number
Parameter Description
sUrl The URL of the WebSocket server. A WebSocket URL in JavaScript usually looks like this: ws://echo.websocket.org

In Silk Performer, you have to specify http instead of ws, for example: http://echo.websocket.org

Return value

  • a handle, which identifies the WebSocket, if the connection was established successfully
  • 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;