#1 2017-04-26 19:48:36

EMartin
Member
From: Buenos Aires - Argentina
Registered: 2013-01-09
Posts: 332

Documentation on ServiceNotificationMethodViaMessages

@ab, can you clarify in the documentation the use of this method ?

In section 16.7.2.6. Interacting with UI/VCL says:

Client.ServiceNotificationMethodViaMessages(MainForm.Handle,WM_SERVICENOTIFICATION);

and Client is a TSQLHttpClientWebsockets and not TSQLRestClientURI (my silly confusion).

Thanks.


Esteban

Offline

#2 2017-04-27 05:19:18

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

Re: Documentation on ServiceNotificationMethodViaMessages

TSQLHttpClientWebsockets is a TSQLRestClientURI, I guess.

Offline

#3 2017-04-27 11:01:16

EMartin
Member
From: Buenos Aires - Argentina
Registered: 2013-01-09
Posts: 332

Re: Documentation on ServiceNotificationMethodViaMessages

Not exactly, I créate a client with TSQLRestClientURI for communications with the server, but for websocket communications I créate with TSQLHttpClientWebsockets the callbacks and with this instance I call this method, then with TSQLRestClientURI I call the another method (execute). I do not have the code at hand.


Esteban

Offline

#4 2017-04-27 14:20:00

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

Re: Documentation on ServiceNotificationMethodViaMessages

All this is a bit confused to me...
There is no problem to have a websockets callback and another connection at the same time.
The callbacks will be received in the websockets client thread, and the other connection will work in its calling thread.

And you can't just create a TSQLRestClientURI instance: it is an abstract class.
You can store into a TSQLRestClientURI variable another inherited class instance, with its own communication layer.

Offline

#5 2017-04-27 15:36:54

EMartin
Member
From: Buenos Aires - Argentina
Registered: 2013-01-09
Posts: 332

Re: Documentation on ServiceNotificationMethodViaMessages

Sorry for my bad expressions. I describe you the usage of this method with my code for showing the ambiguous description in the documentation (for me).

I have my class declaration:

...
TARISClient = class(TObject)
protected
  ...
  fWebSocketClient: TSQLHttpClientWebsockets; // for websocket callbacks
  fCallbackService: IARISCallbackService; // callback interface
  fClient: TSQLRestClientURI; // mORMot client
  fLastErrorMessage: RawUTF8;
  ...
public
  ...
  CallbackClient: IARISCallbackClient;

  constructor Create;
  ...
  function CallbackCreate(const aHost, aPort: string; out aCallback: IARISCallbackClient): Boolean;
  ...
  /// Cliente websocket
  property WSClient: TSQLHttpClientWebsockets read fWebSocketClient;
  ...
end;

class implementation:

constructor TARISClient.Create;
  ...
  fClient := TSQLHttpClient.Create(...);
  ...
end;
...
function TARISClient.CallbackCreate(const aHost, aPort: string; out aCallback: IARISCallbackClient): Boolean;
begin
  // se crea el websocket cliente
  fWebSocketClient := TSQLHttpClientWebsockets.Create(...));
  fWebSocketClient.Model.Owner := fWebSocketClient;
  // se usa el websocket con protocolo binario
  fWebSocketClient.WebSocketsUpgrade(ARIS_WS_TRANSMISSION_KEY);
  if not fWebSocketClient.ServerTimeStampSynchronize then
  begin
    fLastErrorMessage := FormatUTF8(SErrorConnectingToTheServer, [lHost, lPort])  + ' / ' + fWebSocketClient.LastErrorMessage;;
    Exit;
  end;
  // se cargan los servicios definidos para los websockets
  fWebSocketClient.ServiceDefine([IARISCallbackService], sicShared);
  if not fWebSocketClient.Services.Resolve(IARISCallbackService, fCallbackService) then
  begin
    fLastErrorMessage := SServiceIARISCallbackServiceUnava + ' / ' + fWebSocketClient.LastErrorMessage;;
    Exit;
  end;
  // se crea el callback cliente que será invocado cuando se reciba una notificación websocket desde el servidor
  aCallback := TARISCallbackClient.Create(fWebSocketClient, IARISCallbackClient);
  // si está asignado el evento para cuando se recibe un callback se lo asigna al callback cliente creado para que
  // luego propague la notificación
  if Assigned(fOnCallback) then
    TARISCallbackClient(ObjectFromInterface(aCallback)).OnCallback := fOnCallback; // allow it callback propagation when used in a TForm
  Result := True;
end;
...

then I use the client and the callbacks in visual application:

class declaration:

const
  WM_SERVICENOTIFICATION = WM_USER+123;
type
...
TfrmARISClient = class(TForm)
protected
  ...
  fARISClient: TARISClient;
  fCallback: IARISCallbackClient;
  ...
  procedure ServiceNotification(var Msg: TMessage); message WM_SERVICENOTIFICATION;
  procedure DoOnCallback(const aJSONCallback: RawUTF8);
  ...
public
  ...
end;
...

class implementation and usage:

...
procedure TfrmARISClient.FormCreate(Sender: TObject);
begin
  ...
  // se crea el cliente de ARIS
  fARISClient := TARISClient.Create(...);
  if not fARISClient.Client.ServerTimeStampSynchronize then
    raise Exception.Create('No se pudo conectar con ARIS');
  ...
  // se suscribe al callback para recibir notificaciones
  fARISClient.CallbackCreate(...,...,fCallback);
  // evento que procesa los callbacks
  TARISCallbackClient(ObjectFromInterface(fCallback)).OnCallback := DoOnCallback;
  // procesar callbacks en la hebra principal
  fARISClient.WSClient.ServiceNotificationMethodViaMessages(Handle,WM_SERVICENOTIFICATION); //--> *** LINE 1 ***
  ...  
end;
...
procedure TfrmARISClient.ServiceNotification(var Msg: TMessage);
begin
  TSQLRestClientURI.ServiceNotificationMethodExecute(Msg); //--> *** LINE 2 ***
end;
...
procedure TfrmARISClient.DoOnCallback(const aJSONCallback: RawUTF8);
begin
  // process callback in main thread (show progress bar, update labels, etc.)
end;

*** LINE 1 ***: use of the client websocket, not of the fARISClient (TSQLRestClientURI/TSQLHttpClient).
*** LINE 2 ***: use of the class procedure of the TSQLRestClientURI, I know I can use fARISClient instead.

in the documentation only show Client.ServiceNotificationMethodViaMessages(MainForm.Handle,WM_SERVICENOTIFICATION) and, for me, is not clear the client type, it may be only me who does not understand.

I hope you understand, anyway this works like a charm.

Regards.


Esteban

Offline

#6 2017-04-27 15:46:21

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

Re: Documentation on ServiceNotificationMethodViaMessages

Client is the WebSockets client, which receives the asynchronous callback, even if it is trigerred by a method from another client (or from pure server-side event).

Otherwise, it does not make much sense, I guess.

Offline

#7 2017-04-27 15:58:23

EMartin
Member
From: Buenos Aires - Argentina
Registered: 2013-01-09
Posts: 332

Re: Documentation on ServiceNotificationMethodViaMessages

ok


Esteban

Offline

Board footer

Powered by FluxBB