Un cliente pregunta si TMS Sparkle y sgcWebSockets pueden funcionar juntos, y la respuesta es sí: no hay problema en ejecutar sgcWebSockets y TMS Sparkle en el mismo servidor. Ambos pueden ejecutarse mediante el servidor HTTP.SYS; puedes ejecutar un único servidor HTTP.SYS y configurar endpoints para Sparkle y sgcWebSockets sin problemas. Básicamente, configuras en cada paquete qué endpoint se gestionará.
sgcWebSockets puede ejecutarse en un servidor HTTP.SYS mediante el HTTP API Server:
https://www.esegece.com/help/sgcWebSockets/#t=Components%2FTsgcWebSocketServer_HTTPAPI.htm
A continuación se muestran 2 ejemplos que ilustran cómo sgcWebSockets y TMS Sparkle pueden ejecutarse en el mismo servidor HTTP.SYS.
Ejemplo de 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.
Ejemplo de 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.
Ejemplos compilados
Sigue las instrucciones siguientes para ejecutar los ejemplos:
1. Ejecuta sgcWSServer como administrador. Abrirá un servidor WebSocket escuchando en el puerto 2001 y el endpoint "/ws".2. Ejecuta HelloWorldServer. Abrirá un servidor REST escuchando en el puerto 2001 y el endpoint "/rest".
3. Abre una conexión WebSocket a ws://127.0.0.1:2001/ws. Recibirás un mensaje del servidor tras conectarte y, si envías cualquier mensaje, el servidor lo devolverá automáticamente.
4. Abre una conexión HTTP a http://127.0.0.1:2001/rest. Verás una respuesta sencilla del servidor REST.
