WebSockets TMS Sparkle

A customer ask if TMS Sparkle and sgcWebSockets can run together, and the answer is yes, there is no problem running sgcWebSockets and TMS Sparkle on the same server. Both can run using HTTP.SYS server, you can run a single HTTP.SYS server and configure endpoints to run with Sparkle and sgcWebSockets without problems. Basically you configure in each package which endpoint will handle.

sgcWebSockets can run in HTTP.SYS server through HTTP API Server:

https://www.esegece.com/help/sgcWebSockets/#t=Components%2FTsgcWebSocketServer_HTTPAPI.htm

Find below 2 samples which show how sgcWebSockets and TMS Sparkle can run on the same HTTP.SYS server.

sgcWebSockets Sample

program sgcWSServer;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  sgcWebSocket, sgcWebSocket_Classes,
  sgcWebSocket_Server_HTTPAPI;

type
  TsgcServerClass = class
    public
      procedure OnConnectEvent(Connection: TsgcWSConnection);
      procedure OnMessageEvent(Connection: TsgcWSConnection; const Text: String);
  end;


procedure TsgcServerClass.OnConnectEvent(Connection: TsgcWSConnection);
begin
   Connection.WriteData('Hello From Server.');
end;

procedure TsgcServerClass.OnMessageEvent(Connection: TsgcWSConnection; const
    Text: String);
begin
  Connection.WriteData(Text);
end;

var
  oServer: TsgcWebSocketServer_HTTPAPI;
  oConnection: TsgcServerClass;
begin
  try
    oServer := TsgcWebSocketServer_HTTPAPI.Create(nil);
    oConnection := TsgcServerClass.Create;
    Try
      oServer.Bindings.NewBinding('127.0.0.1', 2001, '/ws/');
      oServer.OnConnect := oConnection.OnConnectEvent;
      oServer.OnMessage := oConnection.OnMessageEvent;
      oServer.Active := True;
      WriteLn('sgcWebSockets Server started at ws://127.0.0.1:2001/ws');

      while oServer.Active do
        Sleep(10);
    Finally
      oConnection.Free;
      oServer.Free;
    End;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
 

TMS Sparkle Sample

program HelloWorldServer;

{$APPTYPE CONSOLE}

uses
  System.SysUtils,
  Sparkle.HttpServer.Context,
  Sparkle.HttpServer.Module,
  Sparkle.HttpSys.Server;

type
  THelloWorldModule = class(THttpServerModule)
    public procedure ProcessRequest(const C: THttpServerContext); override;
  end;

procedure THelloWorldModule.ProcessRequest(const C: THttpServerContext);
begin
  C.Response.StatusCode := 200;
  C.Response.ContentType := 'text/plain';
  C.Response.Close(TEncoding.UTF8.GetBytes('Hello, World!'));
end;

const
  ServerUrl = 'http://127.0.0.1:2001/rest';
var
  Server: THttpSysServer;
begin
  Server := THttpSysServer.Create;
  try
    Server.AddModule(THelloWorldModule.Create(ServerUrl));
    Server.Start;
    WriteLn('Hello World Server started at ' + ServerUrl);
    WriteLn('Press Enter to stop');
    ReadLn;
  finally
    Server.Free;
  end;
end. 

Compiled Samples

Follow the instructions below to run the samples:

1. Execute sgcWSServer as Administrator. Will open a WebSocket server, listening on port 2001 and endpoint "/ws"

2. Execute HelloWorldServer. Will open a REST server, listening on port 2001 and endpoing "/rest"

3. Open a websocket connection to ws://127.0.0.1:2001/ws. You will get a message from server after connect and if you send any message will be returned by server automatically.

4. Open a http connection to http://127.0.0.1:2001/rest. A simple response from REST server will be shown. 

File Name: WebSocketsTMSSparkle
File Size: 2.4 mb
Download File
×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

iOS Telegram Client
Binance Futures API

Related Posts