TsgcWSHTTPWebBrokerBridgeServer utilizza TIdHttpWebBrokerBridge come base del server ed è utile se si desidera utilizzare un unico server per connessioni DataSnap, HTTP e WebSocket.
TsgcWSHTTPWebBrokerBridgeServer eredita da TsgcWebSocketHTTPServer, pertanto è possibile fare riferimento a questo server.
Seguire i passaggi successivi per sostituire TIdHttpWebBrokerBridge con TsgcWSHTTPWebBrokerBridgeServer:
1. Creare una nuova istanza di TsgcWSHTTPWebBrokerBridgeServer.
2. Sostituire tutte le chiamate a TIdHttpWebBrokerBridge con TsgcWSHTTPWebBrokerBridgeServer.
3. Per gestire le connessioni WebSocket faccia semplicemente riferimento a TsgcWebSocketHTTPServer.
I componenti Datasnap si trovano solo nella cartella Source; non saranno trovati nelle cartelle compilate perché questi oggetti non sono inclusi nel pacchetto sgcWebSockets, quindi devono essere creati in fase di esecuzione.
Aggiungere semplicemente i file richiesti al progetto o impostare il percorso alla cartella Source del pacchetto sgcWebSockets. File richiesti:
Se il progetto utilizza IdHTTPWebBrokerBridge passi a sgcIdHTTPWebBrokerBridge (questo si applica solo per l'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;
Se il server si trova dietro TsgcWebSocketLoadBalancerServer, potrebbero verificarsi problemi con CORS; per evitare tali problemi, utilizzare il seguente codice
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;