DataSnap with WebSocket and HTTP/2

From sgcWebSockets 4.4.3, you can replace your DataSnap server application by our WebSockets server and take advantage of new features like:

    • WebSocket Protocol
    • HTTP/2 Protocol
    • IOCP


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.
TsgcWSHTTP2WebBrokerBridgeServerWebSocket 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_WebBrokerBridgeWebSocket 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.

Sample Code

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).

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. 
×
Stay Informed

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.

sgcWebSockets 4.4.3
HTTP/2 Server Test Performance

Related Posts