Search Unity

Sending Data From Server To Client .

Discussion in 'Connected Games' started by Livindaki, Jul 11, 2019.

  1. Livindaki

    Livindaki

    Joined:
    Jun 13, 2017
    Posts:
    36
    HI ..
    i have a project that had a Dynamic Array: List<All_Rooms> Rooms_Name
    the first Client "Server" that enter the game had all the information about the rooms .. thats mean the array Rooms_Name !=null .
    but every client entering the game need to fill the Rooms_Name Array .. so he need from the server to send to him that array ...
    How can i do that Please ?
     
    Last edited: Jul 11, 2019
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,011
    Depends on what your network API supports. Worst case though you should be able to serialize the list to a byte array and send that to the clients. If your network API supports something similar to Unet's SyncList or SyncListStruct you could use that. Might help if you described what the All_Rooms class contains and what network API you are using.
     
  3. mimat18

    mimat18

    Joined:
    Feb 16, 2019
    Posts:
    11
    Class All_Rooms
    (
    Public string room_Name;
    Public string Password ;
    Public int Max;
    )
    My class like that and I want to send that class to a client
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,011
    That's a pretty simple thing to serialize. Like I already mentioned, with Unet (or Mirror) I'd probably just change it from a class to a struct and use SyncListStruct. But that "worst case" though where you manually serialize the list to a byte array might look something like this if you were to do it manually:

    Code (csharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class RoomHolderAndSenderThingy
    7. {
    8.     public list<All_Rooms> Rooms_Name;  //I assume some other script actually initializes and fills in this list with contents
    9.  
    10.     public byte[] SerializeAllRooms()
    11.     {
    12.         int location = 0;
    13.         int totalBytes = 0;
    14.         list<byte[]> roomsByteArrays = new list<byte[]>();
    15.         foreach (All_Rooms room in Rooms_Name)
    16.         {
    17.             byte[] roomBytes = room.Serialize();
    18.             totalBytes += roomBytes.Length;
    19.             roomsByteArrays.Add(roomBytes);
    20.         }
    21.      
    22.         totalBytes += 4;
    23.         byte[] serialized = new byte[totalBytes];
    24.  
    25.         Array.Copy(BitConverter.GetBytes(Rooms_Name.Count), 0, serialized, location, 4);
    26.         location += 4;
    27.         foreach (byte[] roomsByteArray in roomsByteArrays)
    28.         {
    29.             Array.Copy(roomsByteArray, 0, serialized, location, roomsByteArray.Length);
    30.             location += 4;
    31.         }
    32.  
    33.         return serialized;
    34.     }
    35.  
    36.     public void DeserializeAllRooms(byte[] serialized)
    37.     {
    38.         Rooms_Name = new list<All_Rooms>();
    39.  
    40.         int location = 0;
    41.         int numOfRooms = BitConverter.ToInt32(serialized, location);
    42.         location += 4;
    43.  
    44.         int looper = 0;
    45.         while (looper < numOfRooms)
    46.         {
    47.             All_Rooms newRoom = new All_Rooms();
    48.             int roomLength = newRoom.Deserialize(serialized, location);
    49.             location += roomLength;
    50.             Rooms_Name.Add(newRoom);
    51.  
    52.             looper++;
    53.         }
    54.  
    55.     }
    56.  
    57.    public static byte[] EncodeString(string stringData)
    58.     {
    59.         byte[] encoded;
    60.         if ((stringData != null) && (stringData.Length > 0))
    61.         {
    62.             byte[] stringByte = System.Text.Encoding.UTF8.GetBytes(stringData);
    63.             encoded = new byte[stringByte.Length + 4];
    64.             Array.Copy(BitConverter.GetBytes(stringByte.Length), 0, encoded, 0, 4);
    65.             Array.Copy(stringByte, 0, encoded, 4, stringByte.Length);
    66.         }
    67.         else
    68.         {
    69.             encoded = BitConverter.GetBytes((int)0);
    70.         }
    71.         return encoded;
    72.     }
    73.  
    74.     public static string DecodeString(byte[] byteArray, out int byteLength, int location = 0)
    75.     {
    76.         int length = BitConverter.ToInt32(byteArray, location);
    77.  
    78.         byteLength = length + 4;
    79.         if (length > 0)
    80.         {
    81.             return System.Text.Encoding.UTF8.GetString(byteArray, location + 4, length);
    82.         }
    83.         else
    84.         {
    85.             return "";
    86.         }
    87.     }
    88. }
    89.  
    90. public class All_Rooms
    91. {
    92.     public string room_Name;
    93.     public string Password;
    94.     public int Max;
    95.  
    96.     public byte[] Serialize()
    97.     {
    98.         byte[] roomNameBytes = RoomHolderAndSenderThingy.EncodeString(room_Name);
    99.         byte[] passwordBytes = RoomHolderAndSenderThingy.EncodeString(Password);
    100.         byte[] serialized = new byte[roomNameBytes.Length + passwordBytes.Length + 4];
    101.  
    102.         int location = 0;
    103.         Array.Copy(roomNameBytes, 0, serialized, location, roomNameBytes.Length);
    104.         location += roomNameBytes.Length;
    105.         Array.Copy(passwordBytes, 0, serialized, location, passwordBytes.Length);
    106.         location += passwordBytes.Length;
    107.         Array.Copy(BitConverter.GetBytes(Max), 0, serialized, location, 4);
    108.         location += 4;
    109.  
    110.         return serialized;
    111.     }
    112.  
    113.     public int Deserialize(byte[] serialized, int startLocation)
    114.     {
    115.         int location = startLocation;
    116.         int lengthOfString = 0;
    117.  
    118.         room_Name = RoomHolderAndSenderThingy.DecodeString(serialized, out lengthOfString, location);
    119.         location += lengthOfString;
    120.         Password = RoomHolderAndSenderThingy.DecodeString(serialized, out lengthOfString, location);
    121.         location += lengthOfString;
    122.         Max = BitConverter.ToInt32(serialized, location);
    123.         location += 4;
    124.  
    125.         return location - startLocation;
    126.     }
    127. }
    I typed most of it directly into the forum, so there may be errors, but it probably works. You'd just need to then use the DeserializeAllRooms and SerializeAllRooms methods with whatever your network API uses for sending messages, be it some form of RPC or even a raw low level message. Whatever you're using for sending/receiving.

    Obviously it needs some error checking and exception checking added, probably need to check if you need to fragment the message if your network API doesn't do that automatically, but I'm just writing terrible code on a forum, so what can I say? :p
     
    Last edited: Jul 13, 2019
  5. Livindaki

    Livindaki

    Joined:
    Jun 13, 2017
    Posts:
    36
    Thanks a lot i ll try use it now .
     
  6. Livindaki

    Livindaki

    Joined:
    Jun 13, 2017
    Posts:
    36
    Thanks Again for your help .
    But i worked all the day trying do and send or any receive with your functions .but no way ..:-(
    can you please give me an example about using that functions for sending the All_Rooms class from Server to client and from client to Server .
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,011
    Can you please state specifically what network API you are using?
     
  8. mimat18

    mimat18

    Joined:
    Feb 16, 2019
    Posts:
    11
    I am using unity photon pun2