From sgcWebSockets 4.4.3, you can replace your DataSnap server application by our WebSockets server and take advantage of new features like:
There are 3 different types of server that you can use as a replacement of TIdHTTPWebBrokerBridge default Indy server.
Server | Main Features | Description |
TsgcWSHTTPWebBrokerBridgeServer | WebSocket Protocol HTTP 1.* Protocol XHR Protocol IOCP |
Based on Indy library, supports WebSocket and HTTP protocols on the same port. IOCP can be enabled too. |
TsgcWSHTTP2WebBrokerBridgeServer | WebSocket Protocol HTTP 1.* Protocol HTTP/2 Protocol XHR Protocol IOCP | Based on Indy library, supports WebSocket and HTTP/2 protocols on the same port. IOCP can be enabled too. |
TsgcWSServer_HTTPAPI_WebBrokerBridge | WebSocket Protocol HTTP 1.* Protocol HTTP/2 Protocol XHR Protocol IOCP | Based on HTTP.SYS Microfost API, supports WebSocket and HTTP/2 protocols on the same port. IOCP is used by default. Recommended for best performance. |
Using any of these servers, you can use HTTP and WebSocket protocol using the same listening port. You can dispatch custom http requests using OnHTTPRequest event available on any of these servers.
In order to use any of these servers, you must replace the default Indy WebBrokerBridge server and set the required properties in order to run it.
Check the following code which replace the default server and make use of TsgcWSHTTPWebBrokerBridgeServer (supports HTTP + WebSocket protocols).
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133unit FormUnit1;interfaceusesWinapi.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;typeTForm1 = 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);privateFServer: TsgcWSHTTPWebBrokerBridgeServer;procedure StartServer;privateprocedure OnWebSocketMessage(aConnection: TsgcWSConnection; const aText:string);end;varForm1: TForm1;implementation{$R *.dfm}usesWinApi.Windows, Winapi.ShellApi, Datasnap.DSSession;procedure TForm1.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);beginButtonStart.Enabled := not FServer.Active;ButtonStop.Enabled := FServer.Active;EditPort.Enabled := not FServer.Active;end;procedure TForm1.ButtonOpenBrowserClick(Sender: TObject);varvURL: string;vProtocol: string;beginStartServer;vProtocol := 'http';if chkSSL.Checked thenvProtocol := 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);beginStartServer;end;procedure TerminateThreads;beginif TDSSessionManager.Instance <> nil thenTDSSessionManager.Instance.TerminateAllSessions;end;procedure TForm1.Button1Click(Sender: TObject);varvURL: string;vProtocol: string;beginStartServer;vProtocol := 'http';if chkSSL.Checked thenvProtocol := 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);beginTerminateThreads;FServer.Active := False;FServer.Bindings.Clear;end;procedure TForm1.FormCreate(Sender: TObject);beginFServer := TsgcWSHTTPWebBrokerBridgeServer.Create(Self);FServer.OnMessage := OnWebSocketMessage;end;procedure TForm1.OnWebSocketMessage(aConnection: TsgcWSConnection; const aText:string);beginaConnection.WriteData(aText);end;procedure TForm1.StartServer;beginif not FServer.Active thenbeginFServer.Bindings.Clear;FServer.DefaultPort := StrToInt(EditPort.Text);if chkSSL.Checked thenbeginFServer.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.
When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.