Skip to content

TWebSocketClient

The TWebSocketClient is a non-visual component enabling to perform web socket communication with a websocket server.

Set the hostname and port of the websocket server via WebSocketClient.HostName and Port. Start connecting to the websocket server via calling the method WebSocketClient1.Connect. When a successful connection is made, the WebSocketClient.OnConnect is triggered. Call WebSocketClient.Disconnect to disconnect form the server. When a disconnect is called programmatically or for another reason the connection to the websocket server is lost, the OnDisconnect event is triggered.

Sending & retrieving data

Data is sent as a string and retrieved as JavaScript object.

To send a command call:

WebSocketClient.Send(AMessage: string); overload;

WebSocketClient.Send(ABuffer: TJSArrayBuffer); overload;

When data is received from the websocket server, the event OnDataReceived is triggered. This returns the data as a JavaScript object. When the data is a string, the JavaScript obejct can be converted easily to a string by calling TJSObject.toString;

procedure TForm1.WebSocketClient1DataReceived(Sender: TObject; Origin:
string;
  Data: TJSObject);
begin
  WebListBox1.Items.Add(Data.toString);
end;

Properties for TWebSocketClient

Property Description
HostName Sets the name of the web socket server
PathName Sets the (optional) path name for the socket server
Port TCP/IP port to use for the web socket communication

Methods for TWebSocketClient

Method Description
Send(AMessage: string); Sends data to the socket server as string
Send(ABuffer: TJSArrayBuffer) Sends data to the socket server as JavaScript byte array buffer

Events for TWebSocketClient

Event Description
OnConnect Event triggered when the web socket client could successfully connect to the server
OnDataReceived Event triggered when data is received from tlhe web socket server
OnDisconnect Event triggered when the web socket client was disconnected from the web socket server