TsgcWSHTTPWebBrokerBridgeServer

TsgcWSHTTPWebBrokerBridgeServer 使用 TIdHttpWebBrokerBridge 作为服务器基础,适用于希望使用单一服务器同时处理 DataSnap、HTTP 和 WebSocket 连接的场景。

 

TsgcWSHTTPWebBrokerBridgeServer 继承自 TsgcWebSocketHTTPServer,因此您可以引用此服务器。

 

按照以下步骤将 TIdHttpWebBrokerBridge 替换为 TsgcWSHTTPWebBrokerBridgeServer

 

1. 创建 TsgcWSHTTPWebBrokerBridgeServer 的新实例。

 

2. 将所有对 TIdHttpWebBrokerBridge 的调用替换为 TsgcWSHTTPWebBrokerBridgeServer。

 

3. 如需处理 WebSocket 连接,请参考 TsgcWebSocketHTTPServer

 

配置

Datasnap 组件仅位于 Source 文件夹中,在编译后的文件夹中找不到,因为这些对象未包含在 sgcWebSockets 包中,所以必须在运行时创建。

只需将所需文件添加到您的项目中,或将路径设置到 sgcWebSockets 包的 Source 文件夹。所需文件:

 

 

如果项目使用了 IdHTTPWebBrokerBridge,请将其更改为 sgcIdHTTPWebBrokerBridge(仅适用于 Enterprise Edition)。

 

事件


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;

负载均衡器

如果服务器位于 TsgcWebSocketLoadBalancerServer 后面,可能会遇到 CORS 问题,请使用以下代码来避免这些问题

 


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;