Delphi WebSocket Client Component

The WebSocket protocol specification defines an API that allows a client application (commonly, a web browser) to establish two-way communication with a remote server. The protocol defines ws (WebSocket) and wss (WebSocket Secure) as URI schemes for unencrypted and encrypted connections, respectively. You can send messages to the server and receive responses without having to poll the server for a reply.

A WebSocket is a persistent connection between the client and the server, which operates over HTTP through a single TCP/IP socket connection. WebSocket was created to overcome the limitations of the unidirectional HTTP protocol, where any data sent from the server to the client must be first requested by the client. It provides a massive reduction in network traffic and latency compared to long-polling that was used to simulate a full-duplex connection by maintaining two connections.

WebSocket uses HTTP to establish an initial TCP connection, but keeps it alive after the HTTP response is received. The client sends an HTTP request that includes several required headers to ask the server to upgrade the existing HTTP connection to a WebSocket connection. If the server is able to establish such a connection, they use the existing TCP/IP connection as a WebSocket connection. Here's an example of a simple upgrade request from the client to the WebSocket server:

GET /chat HTTP/1.1
Host: example.com:8000
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
						

Creating a WebSocket Client in Delphi

SecureBridge offers a Delphi component for implementing a WebSocket client — TScWebSocketClient. The component also supports secure TLS/SSL connections to the server. To set up a TLS connection, use the SSLOptions property. The Connect method makes an HTTP request to the resource specified in the RequestUri property and if the server supports the WebSocket protocol, establishes a connection.

To send data to the server, use the Send method. You can retrieve data both in the synchronous mode, by using the Receive method for reading, and in the asynchronous mode, by handling the OnMessage event. The component also supports the HeartBeat mode to keep the connection alive, and the WatchDog mode to reconnect to the server automatically if the connection is lost. The code below is an example of a simple WebSocket client written in Delphi:

procedure TChatForm.ProcessMessage(Sender: TObject; const Data: TBytes;
  MessageType: TScWebSocketMessageType; EndOfMessage: boolean);
var
  Message: string;
begin
  if MessageType = mtText then begin
    Message := Encoding.Default.GetString(Data);
    ShowMessage(Message);
  end;
end;

procedure TChatForm.DoCloseChat(Sender: TObject);
begin
  if TScWebSocketClient(Sender).CloseStatus <> csNormalClosure then
    ShowMessage('Chat was closed with error: ' + TScWebSocketClient(Sender).CloseStatusDescription)
  else
    ShowMessage('Goodbye!');
end;

procedure TChatForm.btConnectClick(Sender: TObject);
var
  WebSocketClient: TScWebSocketClient;
begin
  WebSocketClient := TScWebSocketClient.Create('wss://chat.devart.com/connect');
  WebSocketClient.HeartBeatOptions.Enabled := True;
  WebSocketClient.Options.Credentials.UserName := 'Peter';
  WebSocketClient.Options.Credentials.Password := '12345';
  WebSocketClient.Options.UserAgent := 'devart_chat_client';
  WebSocketClient.Options.RequestHeaders['Content-Language'] := 'en-US';
  WebSocketClient.OnMessage := ProcessMessage;
  WebSocketClient.AfterDisconnect := DoCloseChat;
  WebSocketClient.Connect;
  WebSocketClient.Send('Hello, I am Peter');
end;

procedure TChatForm.btSendMessageClick(Sender: TObject);
begin
  WebSocketClient.Send('Message');
end;
						

Creating a WebSocket client in Delphi with SecureBridge is an easy and enjoyable process. You can download a fully-featured trial version to evaluate the solution for 60 days.

You may also find interesting:
- Delphi SSL Client
- Delphi HTTP/HTTPS Client