TsgcWebSocketHTTPServer

TsgcWebSocketHTTPServer implements Server WebSocket Component and can handle multiple threaded client connections as TsgcWebSocketServer, and allows to server HTML pages using a built-in HTTP Server, sharing the same port for WebSocket connections and HTTP requests.

 

Follow the next steps to configure this component:

 

1. Drop a TsgcWebSocketHTTPServer component in the form

 

2. Set Port (default is 80). If you are behind a firewall probably you will need to configure it.

 

3. Set Specifications allowed, by default all specifications are allowed.

 

    RFC6455: is standard and recommended WebSocket specification.

 

    Hixie76: it's a draft and it's only recommended to establish Hixie76 connections if you want to provide support to old browsers like Safari 4.2

 

4. The following events are available:

 

    OnConnect: every time a WebSocket connection is established, this event is fired.

 

    OnDisconnect: every time a WebSocket connection is dropped, this event is fired.

 

    OnError: every time there is a WebSocket error (like mal-formed handshake), this event is fired.

 

    OnMessage: every time a client sends a text message and it's received by server, this event is fired.

 

    OnBinary: every time a client sends a binary message and it's received by server, this event is fired.

 

    OnHandhake: this event is fired after handshake is evaluated on the server side.

 

   OnCommandGet: this event is fired when HTTP Server receives a GET, POST or HEAD command requesting a HTML page, an image... Example:

 

AResponseInfo.ContentText := '<HTML><HEADER>TEST</HEAD><BODY>Hello!</BODY></HTML>';

 

    OnCommandOther: this event is fired when HTTP Server receives a command different of GET, POST or HEAD.

 

    OnCreateSession: this event is fired when HTTP Server creates a new session.

 

    OnInvalidSession: this event is fired when an HTTP request is using an invalid/expiring session.

 

    OnSessionStart: this event is fired when HTTP Server starts a new session.

 

    OnCommandOther: this event is fired when HTTP Server closes a session.

 

    OnException: this event is fired when HTTP Server throws an exception.

 

    OnAuthentication: if authentication is enabled, this event if fired. You can check user and password passed by the client and enable/disable Authenticated Variable.

 

    OnUnknownProtocol: if WebSocket protocol is not detected (because the  client is using plain TCP protocol for example), in this event connection can be accepted or rejected.

 

    OnBeforeHeartBeat: if HeartBeat is enabled, allows to implement a custom HeartBeat setting Handled parameter to True (this means, standard websocket ping won't be sent).

 

    OnBeforeForwardHTTP: allows to forward a HTTP request to another HTTP server. Use forward property to enable this and set the destination URL.

 

    OnHTTPUploadBeforeSaveFile: the event is fired when a new file has been uploaded and before is saved to disk file, allows to modify the filename where will be saved.

 

    OnHTTPUploadAfterSaveFile: the event is fired after a new file has been uploaded and saved to disk file.

 

    OnHTTPUploadReadInput: the event is fired when the form post reads an input variable different from the file.

 

    OnSSLGetHandler: This event is raised before SSL handler is created, you can create here your own SSL Handler (needs to be inherited from TIdServerIOHandlerSSLBase or TIdIOHandlerSSLBase) and set the properties needed.

 

    OnSSLAfterCreateHandler: This event is called after the SSL Handler is created. Can be used to customize some of the properties of the IOHandler.

 

    OnSSLALPNSelect: When the connection is using ALPN this event is raised to set which protocol will be used.

 

    OnSSLVerifyPeer: When the property VerifyCertificate is set to True and the client is using a certificate, this event will be raised with the certificate data and the option to accept or not the connection.

 

 

 

* In some cases, you may get a high consume of cpu due to unsolicited connections, in these cases, just return an error 500 if it's a HTTP request or close connection for Unknown Protocol requests.

 

 

 

5. Create a procedure and set property Active = true.

 

 

Most common uses

 

 

Methods

     Broadcast: sends a message to all connected clients.

 

Message / Stream: message or stream to send to all clients.

 

Channel: if you specify a channel, the message will be sent only to subscribers.

 

Protocol: if defined, the message will be sent only to a specific protocol.

 

Exclude: if defined, list of connection guid excluded (separated by comma).

 

Include: if defined, list of connection guid included (separated by comma).

 

    WriteData: sends a message to a single or multiple clients. Every time a Client establishes a WebSocket connection, this connection is identified by a Guid, you can use this Guid to send a message to a client.

 

    Ping: sends a ping to all connected clients.

 

    DisconnectAll: disconnects all active connections.

 

 

Properties

    Connections: contains a list of all clients connections.

 

    Bindings: used to manage IP and Ports.

 

    DocumentRoot: here you can define a directory where you can put all html files (javascript, HTML, CSS...) if a client sends a request, the server automatically will search this file on this directory, if it finds, it will be served.

 

    Extensions: you can enable compression on messages sent (if client don't support compression, messages will be exchanged automatically without compression).

 

    MaxConnections: max connections allowed (if zero there is no limit).

 

    Count: Connections number count.

 

    AutoStartSession: if SessionState is active, when the server gets a new HTTP request, creates a new session.

 

    SessionState: if active, enables HTTP sessions.

 

    KeepAlive: if enabled, connection will stay alive after the response has been sent.

 

    ReadStartSSL: max. number of times an HTTPS connection tries to start.

 

    SessionList: read-only property used as a container for TIdHTTPSession instances created for the HTTP server.

 

    SessionTimeOut: timeout of sessions.

 

    HTTP2Options: by default HTTP/2 protocol is not enabled, it uses HTTP 1.1 to handle HTTP requests. Enabled this property if you want use HTTP/2 protocol if client supports it.

 

Enabled: if true, HTTP/2 protocol is supported. If client doesn't supports HTTP/2, HTTP 1.1 will be used as fallback.

 

FragmentedData: this property allows to configure how handle the fragments received.

 

 

 

Settings: Specifies the header values to send to the HTTP/2 server.

 

EnablePush: by default enabled, this setting can be used to avoid server push content to client.

 

HeaderTableSize: Allows the sender to inform the remote endpoint of the maximum size of the header compression table used to decode header blocks, in octets. The encoder can select any size equal to or less than this value by using signaling specific to the header compression format inside a header block. The initial value is 4,096 octets.

 

InitialWindowSize: Indicates the sender’s initial window size (in octets) for stream-level flow control. The initial value is 65,535 octets. This setting affects the window size of all streams.

 

MaxConcurrentStreams: Indicates the maximum number of concurrent streams that the sender will allow. This limit is directional: it applies to the number of streams that the sender permits the receiver to create. Initially, there is no limit to this value.

 

MaxFrameSize: Indicates the size of the largest frame payload that the sender is willing to receive, in octets. The initial value is 16,384 octets.

 

MaxHeaderListSize: This advisory setting informs a peer of the maximum size of header list that the sender is prepared to accept, in octets. The value is based on the uncompressed size of header fields, including the length of the name and value in octets plus an overhead of 32 octets for each header field.

 

Events: here you can configure if you want be notified when there is a new HTTP/2 connection or not.

 

OnConnect: if enabled when there is a new HTTP/2 connection, OnConnect event will be called (by default is disabled).

 

OnDisconnect: if enabled when there is a new HTTP/2 disconnection, OnDisconnect event will be called (by default is disabled).

 

    HTTPUploadFiles: by default when a client sends a file using a POST stream, the file is saved in memory. If you want to save these streams directly as files to avoid memory problems, you set the StreamType to pstFileStream and the files will be saved in the hard disk. Read more about Post Big Files.

 

MinSize: Minimum size in bytes of the stream to be saved as a file stream. By default is zero, which means all streams will be saved as FileStreams (if StreamType = pstFileStream).

 

RemoveBoundaries: the files uploaded using POST multipart/form-data, are encapsulated in boundaries, if this property is enabled, the files will be extracted from boundaries and saved in the hard disk.

 

SaveDirectory: the folder where the files will be saved. If empty, will be saved in the same folder where is the application.

 

StreamType: the type of the stream where the stream will be saved, by default memory.

 

pstMemoryStream: as memory stream.

pstFileStream: as file stream.