Event Handler best practices

Hi,

Form my main app I want to send some messages to the connected clients. I’ve implemented the eventhandler / event repository.

However most samples are built around events being triggered in the Implementation unit. I know there is a boradcast server but adding another server for some simple messages seems overhead.

What is the preferred way to send a message to all connected clients?

I have done this from the fServerDataModule but it does not seem right:

var

ev: IMy_Writer;
i: Integer;
AList: TStringList;
begin

AList := TStringList.Create();

ROSessionManager.GetAllSessions(AList);

for i := 0 to AList.Count-1 do
begin

ev := (ROEventRepository as IPlayerEventSink_Writer);
ev.ExcludeSender := False;
ev.MyProc(StringToGUID(AList[i]),MyParam);

end;

Second question is where to place the RegisterEventClient for this scenario. I’ve done that in the RORemoteDataModuleActivate now which works but the unregister in the Deactivate does not work.

Thanks,

Hi,

check the HTTP Chat sample:

procedure THTTPChatServerMainForm.bbWarnShutdownClick(Sender: TObject);
var reason : string;
begin
  reason := 'The system administrator will reboot the server in 5 minutes.'#13#13+
            'Please close your applications and save your work.';

  if InputQuery('Shutdown Reason', 'Reason', reason)
    then (EventRepository as IHTTPChatServerEvents_Writer).OnSystemShutdown(EmptyGUID, 5, reason);
end;

RegisterEventClient usually should be put to the Login method of the LoginService service

Hi Evgeny,

Thanks that solves the first problem.

However the RegisterEventClient does nto work for me when I put it in the loginservice.login method. What would be causing that?

Hi,

you can have LegacyEvents = True

check the same HTTP Chat sample:

function THTTPChatService.Login(const aUserID: Unicodestring): Unicodestring;
..
    RegisterEventClient(ClientID, EID_HTTPChatEvents);
    RegisterEventClient(ClientID, EID_HTTPChatServerEvents);

Aaaah ok now I get it. I was setting the EventRepository and LegacyEvents on my main remote data module and not my login module. Now it works, Thanks!