Otherwise, it does not make much sense, I guess.
]]>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.
]]>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.
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.
]]>