WebSocketClose Function

Action

Closes a WebSocket communication channel. The client sends a close message to the server and waits until the server actually closes the connection. After WebSocketClose has been called, the associated WebSocket handle is not valid any longer.

Include file

webapi.bdh

Syntax

WebSocketClose( in hWebSocket    : number,
                in nStatusCode   : number optional,
                in sCloseMessage : dstring optional ): boolean;
Parameter Description
hWebSocket A handle, which identifies the WebSocket. To retrieve this handle, use the WebSocketConnect() function.
nStatusCode Optional: Reason for closing the WebSocket connection. For valid values, refer to http://tools.ietf.org/html/rfc6455#section-7.4. Some well known values are defined as BDL constants:
  • WEBSOCKET_CLOSESTATUS_NORMAL_CLOSURE := 1000;
  • WEBSOCKET_CLOSESTATUS_GOING_AWAY := 1001;
  • WEBSOCKET_CLOSESTATUS_PROTOCOL_ERROR := 1002;
  • WEBSOCKET_CLOSESTATUS_DATATYPE_NOT_ACCEPTED := 1003;
  • WEBSOCKET_CLOSESTATUS_NO_STATUSCODE := 1005;
  • WEBSOCKET_CLOSESTATUS_CLOSED_ABNORMALLY := 1006;
  • WEBSOCKET_CLOSESTATUS_DATA_INCONSISTENT := 1007;
  • WEBSOCKET_CLOSESTATUS_POLICY_VIOLATION := 1008;
  • WEBSOCKET_CLOSESTATUS_MESSAGE_TOO_BIG := 1009;
  • WEBSOCKET_CLOSESTATUS_EXTENSION_NOT_SUPPORTED := 1010;
  • WEBSOCKET_CLOSESTATUS_UNEXPECTED_CONDITION := 1011;
  • WEBSOCKET_CLOSESTATUS_TLS_HANDSHAKE_FAILURE := 1015;
sCloseMessage Optional: Additional message for diagnostic purposes

Return value

  • true if successful

  • 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;