Author Topic: How to get a list of all clients connected to server (in all channels)?  (Read 2100 times)

GantZ_Yaka

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 21
    • View Profile
Is there a command to get from the server once all clients IDs,  without being connected to the channels, without requesting a list of active channels, and not going through a cycle of players in each channel?
« Last Edit: February 17, 2014, 11:28:10 PM by GantZ_Yaka »

GantZ_Yaka

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 21
    • View Profile
Re: How to get a list of all clients connected to server (in all channels)?
« Reply #1 on: February 18, 2014, 02:06:33 AM »
I myself have found a solution to the problem.
If you want to get id of all players on the server without joining the channel you have to add your own package to TNPacket.cs and TNGameServer.cs.

I add to TNPacket.cs :
  1. public enum Packet
  2. {
  3.    Empty,
  4.    ......,
  5.    RequestClients,    //my new packet
  6.    .....,
  7. }
And add to TNGameServer.cs:
find case Packet.RequestChannelList: and place here new case

  1. case Packet.RequestClients:
  2. {
  3.         BinaryWriter writer = BeginSend(Packet.RequestClients);
  4.         int count = mPlayers.size;                             
  5.         writer.Write(count);                           
  6.         for (int i=0; i < mPlayers.size; ++i)
  7.         {
  8.                 writer.Write(mPlayers[i].id);
  9.         }
  10.         EndSend(true, player);
  11.         break;
  12. }


after all, in the script where you want to get the players's id, add these:
  1. float RefreshOffserRate =2.5f;  //delay not to send requests all the time
  2. float NextRefreshUpdate = 0.0f;
  3. public System.Collections.Generic.List<int> clients = new System.Collections.Generic.List<int>();
  4.  
  5. void Update () {
  6.                 if (Time.time > NextRefreshUpdate) {   
  7.                                 TNManager.client.BeginSend(Packet.RequestClients);
  8.                                 TNManager.client.EndSend();
  9.                         NextRefreshUpdate = Time.time + RefreshOffserRate;
  10.                 }
  11.         }
  12.  
  13. void OnNetworkConnect (bool success, string message) {
  14.    if (success){
  15.       TNManager.client.packetHandlers[(byte)Packet.RequestClients] = OnClientsList;
  16. }
  17.  
  18.  
  19. void OnClientsList(Packet response, BinaryReader reader, IPEndPoint source)
  20.         {              
  21.                 int count = reader.ReadInt32();        
  22.                 clients.Clear();               
  23.                 for (int i = 0; i < count; ++i)
  24.                 {
  25.                         clients.Add(reader.ReadInt32());                       
  26.                 }                      
  27.         }

that's all, I hope someone helped  :)