WebSockets TMS Sparkle

· 컴포넌트

고객이 TMS SparklesgcWebSockets함께 실행할 수 있는지 물어봤는데, 답은 네입니다. 동일한 서버에서 sgcWebSockets와 TMS Sparkle를 실행하는 데 문제가 없어요. 두 제품 모두 HTTP.SYS 서버를 사용해 실행할 수 있어요. 단일 HTTP.SYS 서버를 실행하고 Sparkle와 sgcWebSockets가 처리할 엔드포인트를 문제없이 설정할 수 있어요. 기본적으로 각 패키지에서 어떤 엔드포인트를 처리할지 설정해요.

sgcWebSockets는 HTTP API 서버를 통해 HTTP.SYS 서버에서 실행할 수 있어요:

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

아래에서 sgcWebSockets와 TMS Sparkle를 동일한 HTTP.SYS 서버에서 실행하는 방법을 보여주는 샘플 2개를 확인할 수 있어요.

sgcWebSockets 샘플

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 샘플

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. 

컴파일된 샘플

샘플을 실행하려면 아래 지침을 따라 하세요:

1. sgcWSServer를 관리자로 실행하세요. 포트 2001과 엔드포인트 "/ws"에서 WebSocket 서버가 열려요.

2. HelloWorldServer를 실행하세요. 포트 2001과 엔드포인트 "/rest"에서 REST 서버가 열려요.

3. ws://127.0.0.1:2001/ws에 WebSocket 연결을 열어요. 연결 후 서버에서 메시지를 받을 수 있으며, 메시지를 보내면 서버가 자동으로 돌려보내줘요.

4. http://127.0.0.1:2001/rest에 HTTP 연결을 열어요. REST 서버의 간단한 응답이 표시돼요.