sgcWebSockets 4.4.3 sürümünden itibaren, DataSnap sunucu uygulamanızı WebSockets sunucumuzla değiştirebilir ve şu gibi yeni özelliklerden yararlanabilirsiniz:
- WebSocket Protokolü
- HTTP/2 Protokolü
- IOCP
Varsayılan Indy sunucusu TIdHTTPWebBrokerBridge yerine kullanabileceğiniz 3 farklı sunucu türü vardır.
| Sunucu | Temel Özellikler | Açıklama |
| TsgcWSHTTPWebBrokerBridgeServer | WebSocket Protokolü HTTP 1.* Protokolü XHR Protokolü IOCP |
Indy kütüphanesine dayanır, aynı bağlantı noktasında WebSocket ve HTTP protokollerini destekler. IOCP de etkinleştirilebilir. |
| TsgcWSHTTP2WebBrokerBridgeServer | WebSocket Protokolü HTTP 1.* Protokolü HTTP/2 Protokolü XHR Protokolü IOCP | Indy kütüphanesine dayanır, aynı bağlantı noktasında WebSocket ve HTTP/2 protokollerini destekler. IOCP de etkinleştirilebilir. |
| TsgcWSServer_HTTPAPI_WebBrokerBridge | WebSocket Protokolü HTTP 1.* Protokolü HTTP/2 Protokolü XHR Protokolü IOCP | HTTP.SYS Microsoft API'sine dayanır, aynı bağlantı noktasında WebSocket ve HTTP/2 protokollerini destekler. IOCP varsayılan olarak kullanılır. En iyi performans için önerilir. |
Bu sunuculardan herhangi birini kullanarak, aynı dinleme bağlantı noktasında HTTP ve WebSocket protokolünü kullanabilirsiniz. Bu sunuculardan herhangi birinde mevcut olan OnHTTPRequest olayını kullanarak özel http isteklerini yönlendirebilirsiniz.
Örnek Kod
Bu sunuculardan herhangi birini kullanmak için, varsayılan Indy WebBrokerBridge sunucusunu değiştirmeniz ve çalıştırmak için gerekli özellikleri ayarlamanız gerekir.
Varsayılan sunucuyu değiştiren ve TsgcWSHTTPWebBrokerBridgeServer (HTTP + WebSocket protokollerini destekler) kullanan aşağıdaki kodu inceleyin.
unit FormUnit1;
interface
uses
Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.AppEvnts, Vcl.StdCtrls, sgcWebSocket_Server_WebBrokerBridge, Web.HTTPApp,
sgcWebSocket_Classes, sgcWebSocket_Types;
type
TForm1 = class(TForm)
ButtonStart: TButton;
ButtonStop: TButton;
EditPort: TEdit;
Label1: TLabel;
ApplicationEvents1: TApplicationEvents;
ButtonOpenBrowser: TButton;
Button1: TButton;
chkSSL: TCheckBox;
procedure FormCreate(Sender: TObject);
procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
procedure Button1Click(Sender: TObject);
procedure ButtonStartClick(Sender: TObject);
procedure ButtonStopClick(Sender: TObject);
procedure ButtonOpenBrowserClick(Sender: TObject);
private
FServer: TsgcWSHTTPWebBrokerBridgeServer;
procedure StartServer;
private
procedure OnWebSocketMessage(aConnection: TsgcWSConnection; const aText:
string);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
WinApi.Windows, Winapi.ShellApi, Datasnap.DSSession;
procedure TForm1.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
begin
ButtonStart.Enabled := not FServer.Active;
ButtonStop.Enabled := FServer.Active;
EditPort.Enabled := not FServer.Active;
end;
procedure TForm1.ButtonOpenBrowserClick(Sender: TObject);
var
vURL: string;
vProtocol: string;
begin
StartServer;
vProtocol := 'http';
if chkSSL.Checked then
vProtocol := vProtocol + 's';
vURL := Format(vProtocol + '://127.0.0.1:%s', [EditPort.Text]);
ShellExecute(0,
nil,
PChar(vURL), nil, nil, SW_SHOWNOACTIVATE);
end;
procedure TForm1.ButtonStartClick(Sender: TObject);
begin
StartServer;
end;
procedure TerminateThreads;
begin
if TDSSessionManager.Instance <> nil then
TDSSessionManager.Instance.TerminateAllSessions;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
vURL: string;
vProtocol: string;
begin
StartServer;
vProtocol := 'http';
if chkSSL.Checked then
vProtocol := vProtocol + 's';
vURL := Format(vProtocol + '://127.0.0.1:%s/sgcWebSockets.html', [EditPort.Text]);
ShellExecute(0,
nil,
PChar(vURL), nil, nil, SW_SHOWNOACTIVATE);
end;
procedure TForm1.ButtonStopClick(Sender: TObject);
begin
TerminateThreads;
FServer.Active := False;
FServer.Bindings.Clear;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FServer := TsgcWSHTTPWebBrokerBridgeServer.Create(Self);
FServer.OnMessage := OnWebSocketMessage;
end;
procedure TForm1.OnWebSocketMessage(aConnection: TsgcWSConnection; const aText:
string);
begin
aConnection.WriteData(aText);
end;
procedure TForm1.StartServer;
begin
if not FServer.Active then
begin
FServer.Bindings.Clear;
FServer.DefaultPort := StrToInt(EditPort.Text);
if chkSSL.Checked then
begin
FServer.SSLOptions.CertFile := 'sgc.pem';
FServer.SSLOptions.KeyFile := 'sgc.pem';
FServer.SSLOptions.RootCertFile := 'sgc.pem';
FServer.SSLOptions.Port := StrToInt(EditPort.Text);
FServer.SSLOptions.Version := tls1_2;
end;
FServer.SSL := chkSSL.Checked;
FServer.Active := True;
end;
end;
end.
