- Home /
MLAPI: Is it possible to obtain all connected client ids in the NetworkManager?
Seeing if there's anyone more experienced to help. I am just wondering if there is a way to obtain perhaps the ClientId of all connected clients in the NetworkManager.
My situation is that I have a sql server and database which the game interacts with via php scripting. I have a table inside said database with the column 'online' which is either a 0 or 1 to denote if the user is connected.
I intend to update this table when the user clicks the quit button to update the 'online' column in his player's row to 0 for false.
Clearly this is not perfect and the database wont be updated if the user exits without clicking the quit button like alt+f4 for instance. Hence I am writing a script on the server side to take care of it and try to poll for connected clients and update them in the database as accordingly.
Wondering if anyone knows if the NetworkManager from MLAPI has any attribute like a list of connected client ids?
Note: I am aware of the NetworkManager.Singleton.LocalClientId but this returns a single ulong of the local client not all the connected clients. I would like a list of all the client ids if such an attribut exists.
Answer by Zendoth · Jun 14, 2021 at 06:06 PM
I have found the answer to this after some research and a little help from the mlapi discord admin TwoTen.
There exists an event called OnClientDisconnectCallback located within the NetworkManager. This event is automatically invoked by both the disconnected client and the server (important bit). Hence I can call for this event to call an additional function which handles my database queries. The event mentioned returns a ufloat which is the client ID unique to that client. On the server, no 2 client ID are the same starting from 1 which makes this perfect for my implementation.
I had a table in my db called client ID which is updated when the user connects to the server as a client and is already given a client ID. Hence a single user might have different client ID depending on when he logs in throughout the day.
For a user to login, it will query the db and see if this client ID is 0 or !0. If !0 it means someone is logged into that account. If the column was 0, After logging in, the client ID will be updated in the db.
As for my issue, when the event is invoked automatically on client disconnected, the server calls for an event that passed the unique client ID of the disconnected client to the db and I find the client ID cell of the ufloat passed through the event in my db and set it back to 0. I have tested this and it works quite well. If anyone has issues with implementation, I will be glad to help.