2023-01-06 16:18:27 +01:00
|
|
|
// *************************************************************************** }
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2024-01-02 17:04:27 +01:00
|
|
|
// Copyright (c) 2010-2024 Daniele Teti and the DMVCFramework Team
|
2023-01-06 16:18:27 +01:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Original Code has been donated by radek@communicator.pl
|
|
|
|
// (https://github.com/danieleteti/delphimvcframework/issues/613#issuecomment-1368555870)
|
|
|
|
//
|
|
|
|
// Follows the original comments:
|
|
|
|
// Delphi EventSource Client (SSE)
|
|
|
|
//
|
|
|
|
// radek@communicator.pl
|
|
|
|
//
|
|
|
|
// With reference to the specification the only "data"
|
|
|
|
// field of SSE Message if required
|
2023-01-07 23:31:39 +01:00
|
|
|
//
|
|
|
|
// !!!!!! Please note !!!!!!
|
2023-01-06 16:18:27 +01:00
|
|
|
//
|
2023-01-07 23:31:39 +01:00
|
|
|
// Event OnSSEEvent is raised from the thread
|
|
|
|
// make sure your handler is thread safe !
|
|
|
|
//
|
|
|
|
// Use OnQueryExtraHeaders to add custom headers such as
|
|
|
|
// cookies (Set-Cookie) or
|
|
|
|
// Authentication: Bearer XXX.YYY.ZZZ
|
|
|
|
//
|
2023-01-06 16:18:27 +01:00
|
|
|
// ***************************************************************************
|
|
|
|
unit MVCFramework.SSEClient;
|
2023-01-08 12:35:21 +01:00
|
|
|
|
2023-01-06 16:18:27 +01:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2023-01-07 23:31:39 +01:00
|
|
|
System.Net.HttpClient, System.Net.HttpClientComponent, System.SysUtils, System.Net.URLClient, System.Classes, System.Threading;
|
2023-01-06 16:18:27 +01:00
|
|
|
|
|
|
|
type
|
2023-01-08 12:35:21 +01:00
|
|
|
TMVCSSEClientDefaults = class sealed
|
|
|
|
public const
|
|
|
|
/// <summary>
|
|
|
|
/// Accept certificate errors i.e. self signed
|
|
|
|
/// </summary>
|
|
|
|
SSE_CLIENT_IGNORE_CERTIFICATE_ERRORS = true;
|
|
|
|
end;
|
|
|
|
|
2023-01-07 23:31:39 +01:00
|
|
|
TOnSSEEvent = procedure(Sender: TObject; const MessageID: integer; const event, data: string) of object;
|
|
|
|
TOnQueryExtraHeaders = procedure(Sender: TObject; headers: TURLHeaders) of object;
|
2023-01-06 16:18:27 +01:00
|
|
|
|
|
|
|
TMVCSSEClient = class(TObject)
|
|
|
|
private
|
|
|
|
fWorkingTask: ITask;
|
|
|
|
fLastEventId: integer;
|
|
|
|
fReconnectTimeout: integer;
|
2023-01-07 23:31:39 +01:00
|
|
|
fEventStream: TStringStream;
|
2023-01-08 12:35:21 +01:00
|
|
|
fSSEClient: TNetHTTPClient;
|
|
|
|
fSSERequest: TNetHTTPRequest;
|
2023-01-06 16:18:27 +01:00
|
|
|
fURL: string;
|
|
|
|
fOnSSEEvent: TOnSSEEvent;
|
|
|
|
fOnQueryExtraHeaders: TOnQueryExtraHeaders;
|
2023-01-07 23:31:39 +01:00
|
|
|
fTerminated: boolean;
|
|
|
|
procedure ReceiveData(const Sender: TObject; AContentLength, AReadCount: Int64; var AAbort: Boolean);
|
2023-01-08 12:35:21 +01:00
|
|
|
procedure ValidateServerCertificate(const Sender: TObject; const ARequest: TURLRequest; const Certificate: TCertificate;
|
|
|
|
var Accepted: Boolean);
|
2023-01-06 16:18:27 +01:00
|
|
|
protected
|
|
|
|
procedure ExtractMessage(const ASSEMessage: string); virtual;
|
|
|
|
public
|
2023-01-08 12:35:21 +01:00
|
|
|
constructor Create(const AURL: string; const AIgnoreCertificateErrors: boolean = TMVCSSEClientDefaults.SSE_CLIENT_IGNORE_CERTIFICATE_ERRORS);
|
2023-01-06 16:18:27 +01:00
|
|
|
destructor Destroy; override;
|
2023-01-07 23:31:39 +01:00
|
|
|
property OnSSEEvent: TOnSSEEvent read fOnSSEEvent write fOnSSEEvent;
|
|
|
|
property OnQueryExtraHeaders: TOnQueryExtraHeaders read fOnQueryExtraHeaders write fOnQueryExtraHeaders;
|
2023-01-06 16:18:27 +01:00
|
|
|
procedure Start;
|
|
|
|
procedure Stop;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.DateUtils;
|
|
|
|
|
|
|
|
const
|
|
|
|
DefaultReconnectTimeout = 10000;
|
|
|
|
CRLF = #13#10;
|
|
|
|
|
2023-01-08 12:35:21 +01:00
|
|
|
constructor TMVCSSEClient.Create(const AURL: string; const AIgnoreCertificateErrors: boolean = TMVCSSEClientDefaults.SSE_CLIENT_IGNORE_CERTIFICATE_ERRORS);
|
2023-01-06 16:18:27 +01:00
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
fTerminated := False;
|
|
|
|
fURL := AURL;
|
2023-01-08 12:35:21 +01:00
|
|
|
fSSEClient := TNetHTTPClient.Create(nil);
|
|
|
|
fSSERequest := TNetHTTPRequest.Create(nil);
|
2023-01-07 23:31:39 +01:00
|
|
|
fSSERequest.Accept := 'text/event-stream';
|
|
|
|
fSSERequest.OnReceiveData := ReceiveData;
|
2023-01-08 12:35:21 +01:00
|
|
|
fSSERequest.Client := fSSEClient;
|
|
|
|
if AIgnoreCertificateErrors then
|
|
|
|
fSSEClient.OnValidateServerCertificate := ValidateServerCertificate;
|
2023-01-07 23:31:39 +01:00
|
|
|
fEventStream := TStringStream.Create('', TEncoding.UTF8); ;
|
2023-01-06 16:18:27 +01:00
|
|
|
fLastEventId := -1;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TMVCSSEClient.Destroy;
|
|
|
|
begin
|
|
|
|
Stop;
|
2023-01-07 23:31:39 +01:00
|
|
|
fSSERequest.Free;
|
2023-01-08 12:35:21 +01:00
|
|
|
fSSEClient.Free;
|
2023-01-06 16:18:27 +01:00
|
|
|
fEventStream.Free;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCSSEClient.ExtractMessage(const ASSEMessage: string);
|
|
|
|
var
|
|
|
|
SSEMessage: TStrings;
|
|
|
|
event, data: string;
|
|
|
|
begin
|
|
|
|
SSEMessage := TStringList.Create;
|
|
|
|
SSEMessage.NameValueSeparator := ':';
|
|
|
|
try
|
|
|
|
SSEMessage.Text := ASSEMessage;
|
|
|
|
|
|
|
|
if SSEMessage.IndexOfName('id')>-1 then
|
2024-09-25 21:27:53 +02:00
|
|
|
fLastEventId := SSEMessage.Values['id'].Trim.ToInteger;
|
2023-01-06 16:18:27 +01:00
|
|
|
if SSEMessage.IndexOfName('event')>-1 then
|
2024-09-25 21:27:53 +02:00
|
|
|
event := SSEMessage.Values['event'].Trim;
|
2023-01-06 16:18:27 +01:00
|
|
|
if SSEMessage.IndexOfName('data')>-1 then
|
2024-09-25 21:27:53 +02:00
|
|
|
data := SSEMessage.Values['data'].Trim;
|
2023-01-06 16:18:27 +01:00
|
|
|
if SSEMessage.IndexOfName('retry')>-1 then
|
2024-09-25 21:27:53 +02:00
|
|
|
fReconnectTimeout := StrToIntDef(SSEMessage.Values['retry'].Trim, DefaultReconnectTimeout);
|
2023-01-06 16:18:27 +01:00
|
|
|
|
|
|
|
fOnSSEEvent(Self, fLastEventId, event, data);
|
|
|
|
finally
|
|
|
|
SSEMessage.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2023-01-07 23:31:39 +01:00
|
|
|
procedure TMVCSSEClient.ReceiveData(const Sender: TObject; AContentLength, AReadCount: Int64; var AAbort: Boolean);
|
2023-01-06 16:18:27 +01:00
|
|
|
var
|
2023-01-07 23:31:39 +01:00
|
|
|
lData, lSSEItem: string;
|
2023-01-06 16:18:27 +01:00
|
|
|
lSSEItems: TArray<string>;
|
|
|
|
begin
|
2023-01-07 23:31:39 +01:00
|
|
|
AAbort := (fTerminated);
|
|
|
|
if not AAbort then
|
|
|
|
begin
|
2023-01-06 16:18:27 +01:00
|
|
|
|
2023-01-07 23:31:39 +01:00
|
|
|
lData := fEventStream.DataString.Trim;
|
|
|
|
//==============================================================================
|
|
|
|
// PARSE THE FOLLOWING:
|
|
|
|
//
|
|
|
|
// id: 1
|
|
|
|
// event: sampleEvent
|
|
|
|
// data: testData1
|
|
|
|
// retry: 10000
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// id: 2
|
|
|
|
// event: sampleEvent
|
|
|
|
// data: testData2
|
|
|
|
// retry: 10000
|
|
|
|
//==============================================================================
|
|
|
|
|
|
|
|
lSSEItems := lData.Split([CRLF+CRLF]);
|
|
|
|
for lSSEItem in lSSEItems do
|
|
|
|
ExtractMessage(lSSEItem);
|
|
|
|
|
|
|
|
fEventStream.Clear;
|
|
|
|
end;
|
2023-01-06 16:18:27 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCSSEClient.Start;
|
|
|
|
var
|
|
|
|
lNextRetry: TDateTime;
|
|
|
|
begin
|
|
|
|
fReconnectTimeout := DefaultReconnectTimeout;
|
|
|
|
if not Assigned(fOnSSEEvent) then
|
|
|
|
raise Exception.Create('No event handler defined for OnSSEEvent');
|
|
|
|
|
2023-01-07 23:31:39 +01:00
|
|
|
if Assigned(fOnQueryExtraHeaders) then
|
|
|
|
fOnQueryExtraHeaders(Self, fSSERequest.CustHeaders);
|
|
|
|
|
|
|
|
fTerminated := false;
|
2023-01-06 16:18:27 +01:00
|
|
|
|
|
|
|
fWorkingTask := TTask.Run(
|
|
|
|
procedure
|
|
|
|
begin
|
2023-01-07 23:31:39 +01:00
|
|
|
while (not fTerminated) do
|
2023-01-06 16:18:27 +01:00
|
|
|
begin
|
|
|
|
try
|
2023-01-07 23:31:39 +01:00
|
|
|
fSSERequest.CustHeaders.Add('Last-Event-ID', fLastEventId.ToString);
|
|
|
|
fSSERequest.Get(FURL, fEventStream);
|
2023-01-06 16:18:27 +01:00
|
|
|
except
|
2023-01-17 08:52:26 +01:00
|
|
|
on E: Exception do
|
2023-01-06 16:18:27 +01:00
|
|
|
begin
|
2023-01-17 08:52:26 +01:00
|
|
|
if not (E is ENetHTTPResponseException) then
|
2023-01-07 23:31:39 +01:00
|
|
|
begin
|
|
|
|
//connection to server lost, use fReconnectTimeout
|
|
|
|
//non blocking Sleep
|
|
|
|
lNextRetry := IncMilliSecond(Now, fReconnectTimeout);
|
|
|
|
while Now < lNextRetry do
|
|
|
|
begin
|
|
|
|
if (fTerminated) then
|
|
|
|
Break;
|
2023-01-17 08:52:26 +01:00
|
|
|
Sleep(100);//prevent from draining processor
|
2023-01-07 23:31:39 +01:00
|
|
|
end;
|
|
|
|
end
|
|
|
|
else
|
2023-01-17 08:52:26 +01:00
|
|
|
begin
|
|
|
|
//request cancelled
|
|
|
|
if (fTerminated) then
|
|
|
|
Break;
|
|
|
|
end;
|
2023-01-06 16:18:27 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCSSEClient.Stop;
|
|
|
|
begin
|
|
|
|
fTerminated := True;
|
2023-01-08 12:35:21 +01:00
|
|
|
fSSERequest.Cancel;
|
2023-01-06 16:18:27 +01:00
|
|
|
if Assigned(fWorkingTask) then
|
|
|
|
begin
|
2023-01-07 23:31:39 +01:00
|
|
|
TTask.WaitForAll([fWorkingTask]);
|
2023-01-06 16:18:27 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2023-01-08 12:35:21 +01:00
|
|
|
procedure TMVCSSEClient.ValidateServerCertificate(const Sender: TObject; const ARequest: TURLRequest; const Certificate: TCertificate;
|
|
|
|
var Accepted: Boolean);
|
|
|
|
begin
|
|
|
|
Accepted := true;
|
|
|
|
end;
|
|
|
|
|
2023-01-06 16:18:27 +01:00
|
|
|
end.
|