unit uSimpleServer; {$I ..\..\..\source\cef.inc} interface uses {$IFDEF DELPHI16_UP} Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Samples.Spin, Vcl.ExtCtrls, System.Math, {$ELSE} Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Spin, ExtCtrls, Math, {$ENDIF} uCEFInterfaces, uCEFServerComponent, uCEFTypes, uCEFMiscFunctions; type TSimpleServerFrm = class(TForm) CEFServerComponent1: TCEFServerComponent; ButtonPnl: TPanel; ConnectionLogMem: TMemo; AddressLbl: TLabel; AddressEdt: TEdit; PortLbl: TLabel; PortEdt: TSpinEdit; BacklogLbl: TLabel; BacklogEdt: TSpinEdit; StartBtn: TButton; StopBtn: TButton; procedure StartBtnClick(Sender: TObject); procedure StopBtnClick(Sender: TObject); procedure AddressEdtChange(Sender: TObject); procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); procedure FormCreate(Sender: TObject); procedure CEFServerComponent1ServerCreated(Sender: TObject; const server: ICefServer); procedure CEFServerComponent1ServerDestroyed(Sender: TObject; const server: ICefServer); procedure CEFServerComponent1ClientConnected(Sender: TObject; const server: ICefServer; connection_id: Integer); procedure CEFServerComponent1ClientDisconnected(Sender: TObject; const server: ICefServer; connection_id: Integer); procedure CEFServerComponent1HttpRequest(Sender: TObject; const server: ICefServer; connection_id: Integer; const client_address: ustring; const request: ICefRequest); procedure CEFServerComponent1WebSocketConnected(Sender: TObject; const server: ICefServer; connection_id: Integer); procedure CEFServerComponent1WebSocketMessage(Sender: TObject; const server: ICefServer; connection_id: Integer; const data: Pointer; data_size: NativeUInt); procedure CEFServerComponent1WebSocketRequest(Sender: TObject; const server: ICefServer; connection_id: Integer; const client_address: ustring; const request: ICefRequest; const callback: ICefCallback); protected FClosing : boolean; function BufferToString(const aBuffer : TBytes) : string; procedure ShowRequestInfo(const aRequest : ICefRequest); procedure ShowPostDataInfo(const aPostData : ICefPostData); public { Public declarations } end; var SimpleServerFrm: TSimpleServerFrm; implementation {$R *.dfm} // Server capacity is limited and is intended to handle only a small number of // simultaneous connections (e.g. for communicating between applications on localhost). // To test the HTTP server follow these steps : // 1- Build and run this demo. // 2- Click on the Start button. // 3- Open your web browser and visit this address http://127.0.0.1:8099 // 4- You should see some connection details in the server log and a "Hellow world" text in your web browser. // To test the websockets server follow these steps : // 1- Build and run this demo. // 2- Click on the "Start" button. // 3- Open your web browser and visit this address https://www.websocket.org/echo.html // 4- Type this in the "Location" field ws://127.0.0.1:8099 // 5- Click the "Connect" button. // 6- Click the "Send" button. // 7- You should see some connection details in the server log and the default text message "Rock it with HTML5 WebSocket" procedure TSimpleServerFrm.AddressEdtChange(Sender: TObject); begin if not(CEFServerComponent1.IsRunning) then StartBtn.Enabled := (length(trim(AddressEdt.Text)) > 0); end; procedure TSimpleServerFrm.StartBtnClick(Sender: TObject); begin if (length(trim(AddressEdt.Text)) > 0) then CEFServerComponent1.CreateServer(AddressEdt.Text, PortEdt.Value, BacklogEdt.Value); end; procedure TSimpleServerFrm.StopBtnClick(Sender: TObject); begin CEFServerComponent1.Shutdown; end; procedure TSimpleServerFrm.CEFServerComponent1ClientConnected( Sender: TObject; const server: ICefServer; connection_id: Integer); begin ConnectionLogMem.Lines.Add('Client connected : ' + inttostr(connection_id)); end; procedure TSimpleServerFrm.CEFServerComponent1ClientDisconnected( Sender: TObject; const server: ICefServer; connection_id: Integer); begin ConnectionLogMem.Lines.Add('Client disconnected : ' + inttostr(connection_id)); end; procedure TSimpleServerFrm.CEFServerComponent1HttpRequest(Sender: TObject; const server: ICefServer; connection_id: Integer; const client_address: ustring; const request: ICefRequest); var TempData : AnsiString; TempParts : TUrlParts; begin ConnectionLogMem.Lines.Add('---------------------------------------'); ConnectionLogMem.Lines.Add('HTTP request received from connection ' + inttostr(connection_id)); ConnectionLogMem.Lines.Add('Client address : ' + client_address); ShowRequestInfo(request); ConnectionLogMem.Lines.Add('---------------------------------------'); if (request <> nil) and CefParseUrl(Request.URL, TempParts) then begin if (TempParts.path = '') or (TempParts.path = '/') then begin TempData := UTF8Encode('