Skip to content

Instantly share code, notes, and snippets.

@flydev-fr
Last active July 12, 2023 08:17
Show Gist options
  • Save flydev-fr/939024853ad225593d26e1a488e1edae to your computer and use it in GitHub Desktop.
Save flydev-fr/939024853ad225593d26e1a488e1edae to your computer and use it in GitHub Desktop.
Issue with OnWebSocketsClosed on Delphi 11 with last mORMot2 release
(*
Issue on Delphi 11, FPC not tested:
- OnWebSocketsClosed event is not triggered on client side on last mORMot2 version.
- confirmed working at least on commit:
> May 12, 2023 - (fa3cd43) fa3cd43097e1e9a89edee94ad0282b17817668a6
Step to reproduce:
1. grab `rest-websockets` from the example folder and compile the server on last mORMot2 release
2. on client, assign a procedure on `OnWebSocketsClosed` event
3. compile the client using last mORMot2 release
4. run the server and the client, then close the server
*)
/// simple SOA client using callbacks for a chat room
program restws_chatclient;
{$I mormot.defines.inc}
{$ifdef OSWINDOWS}
{$APPTYPE CONSOLE}
{$endif OSWINDOWS}
uses
{$I mormot.uses.inc} // use FastMM4 on older versions of Delphi
SysUtils,
Classes,
mormot.core.text,
mormot.core.os,
mormot.core.interfaces,
mormot.orm.core,
mormot.soa.core,
mormot.rest.client,
mormot.rest.http.client,
restws_chatinterface in 'restws_chatinterface.pas';
type
TChatCallback = class(TInterfacedCallback, IChatCallback)
protected
procedure NotifyBlaBla(const pseudo, msg: string);
end;
{$I-} // for write/writeln below
procedure TChatCallback.NotifyBlaBla(const pseudo, msg: string);
begin
TextColor(ccLightBlue);
writeln(#13'@',pseudo,' ',msg);
TextColor(ccLightGray);
write('>');
end;
type
TestClientDisconnect = class
public procedure OnWebSocketsClosed(Sender: TObject);
end;
procedure Run;
var
Client: TRestHttpClientWebsockets;
pseudo, msg: string;
Service: IChatService;
callback: IChatCallback;
test: TestClientDisconnect;
begin
test := TestClientDisconnect.Create;
writeln('Connecting to the local Websockets server...');
Client := TRestHttpClientWebsockets.Create('mykingspark1.ddns.me', '8888', TSQLModel.Create([]));
try
Client.Model.Owner := Client;
Client.WebSocketsUpgrade(CHAT_TRANSMISSION_KEY);
//
Client.OnWebSocketsClosed := test.OnWebSocketsClosed;
//
if not Client.ServerTimeStampSynchronize then
raise EServiceException.Create(
'Error connecting to the server: please run Project31ChatServer.exe');
Client.ServiceDefine([IChatService], sicShared);
if not Client.Services.Resolve(IChatService, Service) then
raise EServiceException.Create('Service IChatService unavailable');
try
TextColor(ccWhite);
writeln('Please enter your name, then press [Enter] to join the chat');
writeln('Enter a void line to quit');
write('@');
TextColor(ccLightGray);
readln(pseudo);
if pseudo = '' then
exit;
callback := TChatCallback.Create(Client, IChatCallback);
Service.Join(pseudo, callback);
TextColor(ccWhite);
writeln('Please type a message, then press [Enter]');
writeln('Enter a void line to quit');
repeat
TextColor(ccLightGray);
write('>');
readln(msg);
if msg='' then
break;
Service.BlaBla(pseudo, msg);
until false;
finally
callback := nil; // will unsubscribe from the remote publisher
Service := nil; // release the service local instance BEFORE Client.Free
end;
finally
Client.Free;
test.Free;
end;
end;
{ TestClientDisconnect }
procedure TestClientDisconnect.OnWebSocketsClosed(Sender: TObject);
begin
Writeln('=> websockets closed');
end;
begin
try
Run;
except
on E: Exception do
ConsoleShowFatalException(E);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment