TsgcWebSocketClient_WinHTTP

TsgcWebSocketClient implements Client VCL WebSocket Component and can connect to a WebSocket Server, it's based on WinHTTP API and requires Windows 8 or higher. Follow the steps below to configure this component:

 

1. Drop a TsgcWebSocketClient_WinHTTP component in the form

 

2. Set Host and Port (default is 80) to connect to an available WebSocket Server. You can set URL property and Host, Port, Parameters... will be updated from URL. Example: wss://127.0.0.1:8080/ws/ will result:


oClient := TsgcWebSocketClient_WinHTTP.Create(nil);
oClient.Host := '127.0.0.1';
oClient.Port := 80;
oClient.TLS := True;
oClient.Options.Parameters := '/ws/';

 

3. You can select if you want TLS (secure connection) or not, disabled by default.

 

4. The following events can be used to customize the websocket client flow:

 

    OnConnect: when a WebSocket connection is established, this event is triggered

 

    OnDisconnect: when a WebSocket connection is dropped, this event is triggered

 

    OnError: whenever a WebSocket error occurs (like mal-formed handshake), this event is triggered

 

    OnMessage: every time the server sends a text message, this event is triggered

 

    OnBinary: every time the server sends a binary message, this event is triggered

 

    OnFragmented: when receives a fragment from a message (only fired when Options.FragmentedMessages = frgAll or frgOnlyFragmented).

 

    OnException: whenever an exception occurs, this event is triggered.

 

    OnBeforeConnect: before the client tries to connect to server, this event is called.

 

    OnBeforeWatchDog: if WatchDog is enabled, allows implementing a custom WatchDog setting Handled parameter to True (this means, won't attempts to connect to server). You can change the Server Connection properties too before try to reconnect, example: connect to a fallback server if first fails.

 

 

 

8. Create a procedure and set property Active = True.

 

Methods

 

    WriteData: sends a message to a WebSocket Server. Could be a String or TStream.

 

    Start: uses a secondary thread to connect to the server, this prevents your application from freezing while trying to connect.

 

    Stop: uses a secondary thread to disconnect from the server, this prevents your application from freezing while trying to disconnect.

 

    Connect: attempts to connect to the server and wait till the connection is successful or there is an error.

 

    Disconnect: attempts to disconnect from the server and wait till disconnection is successful or there is an error.

 

Properties

    Authentication: if enabled, WebSocket connection will try to authenticate passing a username and password.

      

       Implements 1 type of WebSocket Authentication

 

Basic: client open WebSocket connection passing username and password inside the header.

 

    Asynchronous: by default, requests are synchronous, execution of your application stops when you make new requests and resumes when you get a response. If you don't want that requests stop your application, enable this property.

 

    Host: IP or DNS name of the server.

 

     HeartBeat: if enabled attempts to keep alive a WebSocket connection sending a ping every x seconds.

 

Interval: number of seconds between each ping.

 

Timeout: max number of seconds between a ping and pong.

 

    ReadTimeout: max time in milliseconds to read messages.

 

    Port: Port used to connect to the host.

 

    NotifyEvents: defines which mode to notify websocket events.

 

neAsynchronous: this is the default mode, notify threaded events on asynchronous mode, adds events to a queue that are synchronized with the main thread asynchronously.

 

neSynchronous: if this mode is selected, notify threaded events on synchronous mode, needs to synchronize with the main thread to notify these events.

 

neNoSync: there is no synchronization with the main thread, if you need to access to controls that are not thread-safe, you need to implement your own synchronization methods.

 

    Options: allows customizing headers sent on the handshake.

 

Parameters: define parameters used on GET.

 

Origin: customize connection origin.

 

FragmentedMessages: allows handling fragmented messages

 

           frgOnlyBuffer: the message is buffered until all data is received, it raises OnBinary or OnMessage event (option by default)

           frgOnlyFragmented: every time a new fragment is received, it raises OnFragmented Event.

           frgAll: every time a new fragment is received, it raises OnFragmented Event with All data received from the first packet. When all data is received, it raises OnBinary or OnMessage event.

 

    Protocol: if present, displays the current protocol used

 

    Proxy: here you can define if you want to connect through an HTTP Proxy Server.

 

    WatchDog: if enabled, when an unexpected disconnection is detected, tries to reconnect to the server automatically.

 

Interval: seconds before reconnecting.

 

Attempts: maximum number of reconnection attempts; if zero, unlimited.

 

    TLS: enables a secure connection.

 

    TLSOptions: customize the TLS connection.

 

VerifyCertificate: if enabled, the Server Certificate will be validated and if the certificate is incorrect the connection will be closed. By default is disabled.

 

VerifyCertificateOptions: by default, all the certificate's properties are validated, if you want to ignore any of them, find below the options available.

 

    IgnoreCertCNInvalid: Allows an invalid common name in a certificate; that is, the server name specified by the application doesn't match the common name in the certificate.

    IgnoreCertDateInvalid: Allows an invalid certificate date, that is, an expired or not-yet-effective certificate.

    IgnoreCertWrongUsage: Allows the identity of a server to be established with a non-server certificate (for example, a client certificate).

    IgnoreUnknownCA: Allows an invalid certificate authority.