TsgcWSHTTPWebBrokerBridgeServer

TsgcWSHTTPWebBrokerBridgeServer hace uso de TIdHttpWebBrokerBridge como base del servidor y es útil si desea utilizar un único servidor para conexiones DataSnap, HTTP y WebSocket.

 

TsgcWSHTTPWebBrokerBridgeServer hereda de TsgcWebSocketHTTPServer, por lo que puede hacer referencia a este servidor.

 

Siga los pasos siguientes para reemplazar TIdHttpWebBrokerBridge por TsgcWSHTTPWebBrokerBridgeServer:

 

1. Cree una nueva instancia de TsgcWSHTTPWebBrokerBridgeServer.

 

2. Reemplace todas las llamadas a TIdHttpWebBrokerBridge por TsgcWSHTTPWebBrokerBridgeServer.

 

3. Para gestionar las conexiones WebSocket, consulte TsgcWebSocketHTTPServer.

 

Configuración

Los componentes Datasnap se encuentran únicamente en la carpeta Source; no los encontrará en las carpetas compiladas porque estos objetos no están incluidos en el paquete sgcWebSockets, por lo que debe crearlos en tiempo de ejecución.

Simplemente añada los archivos necesarios a su proyecto o establezca la ruta a la carpeta Source del paquete sgcWebSockets. Archivos necesarios:

 

 

Si el proyecto utiliza IdHTTPWebBrokerBridge, cámbielo por sgcIdHTTPWebBrokerBridge (esto solo aplica para la edición Enterprise).

 

Eventos


FServer := TsgcWSHTTPWebBrokerBridgeServer.Create(Self);
FServer.OnCommandRequest := OnCommandRequestEvent;
FServer.OnCommandGet := OnCommandGetevent;
  
procedure OnCommandRequestEvent(AThread: TIdContext; ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo; var aHandled: Boolean);
begin
  if ARequestInfo.Document = '/test.html' then
    aHandled := True;
end;
 
procedure OnCommandGetevent(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
begin
  if ARequestInfo.Document = '/test.html' then
  begin
    AResponseInfo.ResponseNo := 200;
    AResponseInfo.ContentText := 'hello all';
  end;
end;

Balanceador de carga

Si el servidor está detrás del TsgcWebSocketLoadBalancerServer, puede tener problemas con CORS; para evitar estos problemas, use el siguiente código

 


procedure TWebModule1.WebModuleBeforeDispatch(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  Response.SetCustomHeader('Access-Control-Allow-Origin','*');        
  if Trim(Request.GetFieldByName('Access-Control-Request-Headers')) <> '' then 
  begin 
    Response.SetCustomHeader('Access-Control-Allow-Headers', Request.GetFieldByName('Access-Control-Request-Headers'));        
    Handled := True;
  end;
  if FServerFunctionInvokerAction <> nil then
    FServerFunctionInvokerAction.Enabled := AllowServerFunctionInvoker;
end;