Skip to main content
Version: v20 R4 BETA

WebSocketConnection

History
VersionChanges
v20Added

The WebSocketConnection class API allows you to handle WebSocket connections, once established using the WebSocketServer class.

info

For an overview and some examples of the WebSocket server implementation in 4D, please refer to the WebSocketServer class.

WebSocketConnection object

A WebSocketConnection object is automatically created when the WSHandler.onConnection callback function of the WebSocketServer object returns a connectionHandler object.

WebSocketConnection objects provide the following properties and functions:

.handler : Object    the accessor that gets the connectionHandler object used to initiate the connection
.id : Integer    the unique identifier of the connection
.send( message : Text )
.send( message : Blob )
.send( message : Object )
    sends a message to the client
.status : Text    the connection status (can be "Closing", "Closed" or "Connected")
.terminate( { code : Integer ; message : Text } )    forces the connection to close
.wss : 4D.WebSocketServer    the WebSocketServer parent object of the connection

.handler

.handler : Object

Description

The .handler property contains the accessor that gets the connectionHandler object used to initiate the connection.

.id

.id : Integer

Description

The .id property contains the unique identifier of the connection.

This property is read-only.

.send()

.send( message : Text )
.send( message : Blob )
.send( message : Object )

ParameterTypeDescription
messageText / Blob / Object->The message to send

Description

The .send() function sends a message to the client.

The following contents are sent depending on the message type:

TypeContent
TextText in UTF-8
BlobBinary data
ObjectText in JSON UTF-8 (same result as with JSON Stringify)

.status

.status : Text

Description

The .status property contains the connection status (can be "Closing", "Closed" or "Connected").

This property is read-only.

.terminate()

.terminate( { code : Integer ; message : Text } )

ParameterTypeDescription
codeInteger->Error code sent to the client (must be > 3000, otherwise the message is not sent)
messageText->Error message sent to the client

Description

The .terminate() function forces the connection to close.

A code and message can be sent to the client during the closure to indicate the reason of the termination.

.wss

.wss : 4D.WebSocketServer

Description

The .wss property contains the WebSocketServer parent object of the connection.

This property is read-only.