#1 2019-08-30 05:46:14

JuanK
Member
Registered: 2019-08-26
Posts: 6

How to get user info of connected users in a websocket server?

Hi,

I'm using Example 31 webSockets, projectChatServer ( simple SOA server using callbacks for a chat room) as base for my own chat program.  How can I get info on active conections to server? when connected to webserver every user set a UserName, an then send messages to all other users, I would like to send a message to a specific user, so I need to identify other usernames...

In the sample code :

procedure TChatService.Join(const pseudo: string;
  const callback: IChatCallback);
begin
  InterfaceArrayAdd(fConnected,callback);
end;

procedure TChatService.BlaBla(const pseudo,msg: string);
var i: integer;
begin
  for i := high(fConnected) downto 0 do // downwards for InterfaceArrayDelete()
    try
      fConnected[i].NotifyBlaBla(pseudo,msg);
    except
      InterfaceArrayDelete(fConnected,i); // unsubscribe the callback on failure
    end;
end;

I would like to do something like
msg='DestinationUser;RealMessage'
separate msg in 'DestinationUSer' and 'RealMessage' (easy of course) and  then do something like

for i := high(fConnected) downto 0 do
  if fconnected[i].UserName = 'DestinationUser' then
    fConnected[i].NotifyBlaBla(pseudo,'RealMesage');

Or something like

 i = fconnected.findUserByName('DestinationUser' )
Fconnected[i].NotifyBlaBla(pseudo,'RealMesage');

I have been looking in the forum and I guess it should be something like

var
    currSession: TAuthSession;
begin
  for i := high(fConnected) downto 0 do // downwards for InterfaceArrayDelete()
      begin
        currSession := (fConnected[i] as TAuthSession);
        currSession.UserName
        ...

But of course it doesn't work for fConnected...

As you can see I have no idea how to do it... sad Can you helpme please

(ps. Is there a way to search in the forum?, I had been reading a lot of post without luck)

Last edited by JuanK (2019-08-30 05:56:45)

Offline

#2 2019-08-30 06:31:03

pvn0
Member
From: Slovenia
Registered: 2018-02-12
Posts: 211

Re: How to get user info of connected users in a websocket server?

Assuming you are using default framework authentication then you need to access servicecontext thread var to get the session information, you would do this when the user joins the chat service and store the user name or id together with the callback interface, something like this : gist

Offline

#3 2019-08-30 07:03:38

JuanK
Member
Registered: 2019-08-26
Posts: 6

Re: How to get user info of connected users in a websocket server?

Thank you very much pvn0

That worked Great!!! smile

I follow your recommendation and defined fconnected as an array of records and store there the callback and the info I need to preserve.

Offline

#4 2019-08-30 07:20:58

pvn0
Member
From: Slovenia
Registered: 2018-02-12
Posts: 211

Re: How to get user info of connected users in a websocket server?

Glad to be of help, if you changed the array definition then remember to replace the InterfaceArrayAdd, InterfaceArrayDelete etc.. calls as well as those only work on interface arrays.

Offline

#5 2019-08-30 07:26:12

JuanK
Member
Registered: 2019-08-26
Posts: 6

Re: How to get user info of connected users in a websocket server?

Yes, I noticed that, also changed BlaBla as "procedure BlaBla(const Source:string; const Destination:string; msg: string);" to have Source User, Destination User and Message in different variables.  Thank you again, also for responding so quick..

Offline

Board footer

Powered by FluxBB