TCP Connections

Supported by

 

  TsgcWebSocketServer

  TsgcWebSocketHTTPServer

  TsgcWebSocketClient

 

By default, sgcWebSocket uses WebSocket as the protocol, but you can use plain TCP protocol in client and server components.

 

Client Component

Disable WebSocket protocol.


Client.Specifications.RFC6455 := False;
 

 

Server Component

Handle event OnUnknownProtocol and set Transport as trpTCP and Accept the connection.


procedure OnUnknownProtocol(Connection: TsgcWSConnection;  var Accept: Boolean);
begin
  Connection.Transport := trpTCP;
  Accept := True;
end;

 

Then when a client connects to the server, this connection will be defined as TCP and will use plain TCP protocol instead of WebSockets. Plain TCP connections do not distinguish between text and binary messages, so all messages received are handled by the OnBinary event.

 

End of Message

If messages are large, they can sometimes be received fragmented. There is a method to detect the end of a message by specifying which bytes to look for. Example: in the STOMP protocol, all messages end with bytes 0 and 10.


procedure OnWSClientConnect(Connection: TsgcWSConnection);
begin
  Connection.TCPEndOfFrameScanBuffer := eofScanAllBytes;
  Connection.AddTCPEndOfFrame(0);
  Connection.AddTCPEndOfFrame(10);
end;