WebSocketConnect Function

Action

Connects to a WebSocket server and establishes an asynchronous bidirectional communication channel.

Include file

webapi.bdh

Syntax

WebSocketConnect( in sUrl               : string,
                  in csResponseCallback : callback <ASYNC_CALLBACK_FUNCTION> optional ): 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

csResponseCallback The asynchronous response callback function. It has to be passed with the callback keyword (for example: callback(FCallback). The function has to be declared as follows:

function WebSocketConnect(csResponseCallback : string) <ASYNC_CALLBACK_FUNCTION>.

The function has to be of type ASYNC_CALLBACK_FUNCTION. csResponseCallback of the callback contains the content of the current response message part.

Return value

  • a handle, which identifies the WebSocket, if the connection was established successfully
  • false otherwise

Example

 transaction TMain
  var
    hWebSocket : number;
  begin
    hWebSocket := WebSocketConnect("http://echo.websocket.org", callback(FWebSocketMessageReceived));
    
    WebSocketSendTextMessage(hWebSocket, "Rock it with HTML5 WebSocket");
 
    Print("Waiting...");
    Wait(4.1);
    
    WebSocketSendTextMessage(hWebSocket, "Hello World!");

    Print("Waiting...");
    Wait(3.6);
    
    WebSocketSendTextMessage(hWebSocket, "Bye");
    WebSocketClose(hWebSocket);
  end TMain;