TsgcWSHTTPWebBrokerBridgeServer, sunucu temeli olarak TIdHttpWebBrokerBridge kullanır ve DataSnap, HTTP ve WebSocket bağlantıları için tek bir sunucu kullanmak istiyorsanız kullanışlıdır.
TsgcWSHTTPWebBrokerBridgeServer, TsgcWebSocketHTTPServer'dan devralır, böylece bu sunucuya başvurabilirsiniz.
TIdHttpWebBrokerBridge öğesini TsgcWSHTTPWebBrokerBridgeServer ile değiştirmek için sonraki adımları izleyin:
1. TsgcWSHTTPWebBrokerBridgeServer'ın yeni bir örneğini oluşturun.
2. TIdHttpWebBrokerBridge öğesine yapılan tüm çağrıları TsgcWSHTTPWebBrokerBridgeServer ile değiştirin.
3. WebSocket bağlantılarını işlemek için yalnızca TsgcWebSocketHTTPServer öğesine başvurun.
Datasnap bileşenleri yalnızca Source klasöründe bulunur, derlenmiş klasörlerde bulamazsınız çünkü bu nesneler sgcWebSockets paketine dahil değildir, bu nedenle çalışma zamanında oluşturmanız gerekir.
Yalnızca gerekli dosyaları projenize ekleyin veya yolunuzu sgcWebSockets paketinin Source klasörüne ayarlayın. Gerekli dosyalar:
Proje IdHTTPWebBrokerBridge kullanıyorsa sgcIdHTTPWebBrokerBridge'e geçin (bu yalnızca Enterprise Edition için geçerlidir).
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;
Sunucu TsgcWebSocketLoadBalancerServer arkasındaysa, CORS ile ilgili sorunlar yaşayabilirsiniz; bu sorunları önlemek için aşağıdaki kodu kullanın
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;