Added WebSockets events and instructions to SimpleServer

This commit is contained in:
Salvador Díaz Fau 2018-11-25 18:59:41 +01:00
parent 18135e907a
commit a06523287c
2 changed files with 56 additions and 3 deletions

View File

@ -26,7 +26,6 @@ object SimpleServerFrm: TSimpleServerFrm
Align = alTop
BevelOuter = bvNone
TabOrder = 0
ExplicitWidth = 557
object AddressLbl: TLabel
Left = 14
Top = 11
@ -106,7 +105,6 @@ object SimpleServerFrm: TSimpleServerFrm
ReadOnly = True
ScrollBars = ssBoth
TabOrder = 1
ExplicitWidth = 557
end
object CEFServerComponent1: TCEFServerComponent
OnServerCreated = CEFServerComponent1ServerCreated
@ -114,6 +112,9 @@ object SimpleServerFrm: TSimpleServerFrm
OnClientConnected = CEFServerComponent1ClientConnected
OnClientDisconnected = CEFServerComponent1ClientDisconnected
OnHttpRequest = CEFServerComponent1HttpRequest
OnWebSocketRequest = CEFServerComponent1WebSocketRequest
OnWebSocketConnected = CEFServerComponent1WebSocketConnected
OnWebSocketMessage = CEFServerComponent1WebSocketMessage
Left = 456
Top = 304
end

View File

@ -80,6 +80,15 @@ type
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;
@ -100,12 +109,21 @@ implementation
// 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 it follow these steps :
// 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
@ -250,6 +268,40 @@ begin
end;
end;
procedure TSimpleServerFrm.CEFServerComponent1WebSocketConnected(
Sender: TObject; const server: ICefServer; connection_id: Integer);
begin
ConnectionLogMem.Lines.Add('Client connected : ' + inttostr(connection_id));
end;
procedure TSimpleServerFrm.CEFServerComponent1WebSocketMessage(
Sender: TObject; const server: ICefServer; connection_id: Integer;
const data: Pointer; data_size: NativeUInt);
var
TempStream : TStringStream;
begin
TempStream := nil;
try
if (data_size > 0) and (data <> nil) then
begin
TempStream := TStringStream.Create;
TempStream.WriteBuffer(data^, data_size);
ConnectionLogMem.Lines.Add('Client message received : ' + quotedstr(TempStream.DataString));
end;
finally
if (TempStream <> nil) then FreeAndNil(TempStream);
end;
end;
procedure TSimpleServerFrm.CEFServerComponent1WebSocketRequest(
Sender: TObject; const server: ICefServer; connection_id: Integer;
const client_address: ustring; const request: ICefRequest;
const callback: ICefCallback);
begin
if (callback <> nil) then callback.cont;
end;
procedure TSimpleServerFrm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if CEFServerComponent1.Initialized then