#1 2015-05-25 19:58:01

douglasmmm
Member
Registered: 2015-04-24
Posts: 6

WebSockets: Event connect and disconnect

The side of a WebSockets server, is there any event or method to report when a client is connected and disconnected?
As an example to demonstrate in a StringList ...

Server code so far:

  private
    ModeloServicos   : TSQLModel;
    ServidorServicos : TSQLRestServerFullMemory;
    HTTPServer        : TSQLHttpServer;
...

procedure TobjMain.InicializarServicos;
var au : TSQLAuthUser;
begin
   ModeloServicos   := TSQLModel.Create([],_CONST_ROOT_NAME);
   ServidorServicos := TSQLRestServerFullMemory.Create(ModeloServicos,'test.json',false,true);
   ServidorServicos.ServiceDefine(TATWLicensingModule, [IATWLicModule], sicPerSession);

   HTTPServer := TSQLHttpServer.Create('8889',[ServidorServicos],'+',useBidirSocket);
   HTTPServer.WebSocketsEnable(ServidorServicos, _CONST_ENCRIPT).Settings.SetDefaults();
end;

Tanks

Offline

#2 2015-05-26 06:21:12

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: WebSockets: Event connect and disconnect

I've introduced TWebSocketProcessSettings.OnClientConnected and OnClientDisconnected events.
See http://synopse.info/fossil/info/e2e8eb68ef

But I'm not sure if it is very useful...
IMHO you should better handle it at TSQLRestServer level, using authentication and a mORMot session.

BTW Settings.SetDefaults is already done internally by default: no need to write it again.

Offline

#3 2015-05-26 17:34:19

douglasmmm
Member
Registered: 2015-04-24
Posts: 6

Re: WebSockets: Event connect and disconnect

I was with a version that did not have such an implementation.
Thanks for the answer ...

But do not quite understand your cue to use TSQLRestServer to bring me client connection data (IP, username, SessionID, etc), it is possible the server side (with WebSockets)?
I found nothing in the sample code ...


begin
   ModeloServicos   := TSQLModel.Create([],_CONST_ROOT_NAME);
   ServidorServicos := TSQLRestServerFullMemory.Create(ModeloServicos,'test.json',false,true);
   ServidorServicos.ServiceDefine(TATWLicensingModule, [IATWLicModule], sicPerSession);

   HTTPServer := TSQLHttpServer.Create('8889',[ServidorServicos],'+',useBidirSocket);
   // activa o WebSockets para o protocolo de TWebSocketProtocolBinary , com uma chave de encriptação
   HTTPServer.WebSocketsEnable(ServidorServicos, _CONST_ENCRIPT).Settings.SetFullLog;
//   f :=  CurrentServiceContext;
   HTTPServer.WebSocketsEnable(ServidorServicos, _CONST_ENCRIPT).Settings.OnClientConnected     := evntLogado;
   HTTPServer.WebSocketsEnable(ServidorServicos, _CONST_ENCRIPT).Settings.OnClientDisconnected := evntLogout;
end;

procedure TobjMain.evntLogado(Sender: TObject);
begin
  //Ideal bring the customer's name, IP address, session ID ....
  objLstBox.AddItem('Client connected',Self);
end;

procedure TobjMain.evntLogout(Sender: TObject);
begin
  //Ideal bring the customer's name, IP address, session ID ....
 objLstBox.AddItem('Client disconnected',Self);
end;

Last edited by douglasmmm (2015-05-26 18:58:10)

Offline

#4 2015-05-26 19:04:44

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: WebSockets: Event connect and disconnect

From TWebSocketProcessSettings.OnClientConnected and OnClientDisconnected events you would not be able to get those information.

But from TSQLRestServer, you could use the ServiceContext threadvar.
See http://synopse.info/files/html/Synopse% … l#TITL_107

Offline

Board footer

Powered by FluxBB