Od sgcWebSockets 4.4.3 możesz zastąpić swoją aplikację serwera DataSnap naszym serwerem WebSockets i skorzystać z nowych funkcji, takich jak:
- protokół WebSocket
- protokół HTTP/2
- IOCP
Istnieją 3 różne typy serwerów, których możesz użyć jako zamiennika domyślnego serwera Indy TIdHTTPWebBrokerBridge.
| Serwer | Główne funkcje | Opis |
| TsgcWSHTTPWebBrokerBridgeServer | protokół WebSocket protokół HTTP 1.* protokół XHR IOCP |
Oparty na bibliotece Indy, obsługuje protokoły WebSocket i HTTP na tym samym porcie. Można też włączyć IOCP. |
| TsgcWSHTTP2WebBrokerBridgeServer | protokół WebSocket protokół HTTP 1.* protokół HTTP/2 protokół XHR IOCP | Oparty na bibliotece Indy, obsługuje protokoły WebSocket i HTTP/2 na tym samym porcie. Można też włączyć IOCP. |
| TsgcWSServer_HTTPAPI_WebBrokerBridge | protokół WebSocket protokół HTTP 1.* protokół HTTP/2 protokół XHR IOCP | Oparty na Microsoft API HTTP.SYS, obsługuje protokoły WebSocket i HTTP/2 na tym samym porcie. IOCP jest używany domyślnie. Zalecany dla najlepszej wydajności. |
Korzystając z któregokolwiek z tych serwerów, możesz używać protokołu HTTP i WebSocket na tym samym porcie nasłuchu. Możesz obsługiwać niestandardowe żądania http za pomocą zdarzenia OnHTTPRequest dostępnego w każdym z tych serwerów.
Przykładowy kod
Aby skorzystać z któregokolwiek z tych serwerów, musisz zastąpić domyślny serwer Indy WebBrokerBridge i ustawić wymagane właściwości, aby go uruchomić.
Sprawdź poniższy kod, który zastępuje domyślny serwer i korzysta z TsgcWSHTTPWebBrokerBridgeServer (obsługuje protokoły HTTP + WebSocket).
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.
