Delphi WebSocket Client
TsgcWebSocketClient — a full-featured WebSocket client component for Delphi and C++ Builder with SSL/TLS, proxy support, message compression, and automatic reconnection.
TsgcWebSocketClient — a full-featured WebSocket client component for Delphi and C++ Builder with SSL/TLS, proxy support, message compression, and automatic reconnection.
Drop-in component for connecting to any RFC 6455 compliant WebSocket server.
TsgcWebSocketClient provides a complete WebSocket client implementation. Set the Host, Port, and TLS properties, handle the OnMessage event, and activate the connection. The component manages the handshake, frame encoding, ping/pong, and graceful close automatically. It supports text and binary messages, sub-protocols, custom headers, cookies, and HTTP proxy traversal.
Connect to a WebSocket server, send and receive messages.
uses
sgcWebSocket_Client, sgcWebSocket_Types;
var
WSClient: TsgcWebSocketClient;
procedure TForm1.FormCreate(Sender: TObject);
begin
WSClient := TsgcWebSocketClient.Create(nil);
WSClient.Host := 'myserver.example.com';
WSClient.Port := 443;
WSClient.TLS := True;
// Enable automatic reconnection
WSClient.WatchDog.Enabled := True;
WSClient.WatchDog.Interval := 10; // seconds
// Enable compression
WSClient.Extensions.PerMessageDeflate.Enabled := True;
WSClient.OnConnect := OnConnect;
WSClient.OnMessage := OnMessage;
WSClient.OnDisconnect := OnDisconnect;
WSClient.Active := True;
end;
procedure TForm1.OnMessage(Connection: TsgcWSConnection;
const aText: string);
begin
Memo1.Lines.Add('Received: ' + aText);
end;
procedure TForm1.ButtonSendClick(Sender: TObject);
begin
WSClient.WriteData('Hello, WebSocket!');
end;