Search Unity

Unity2D: UNET transport layer API server script isn't messaging properly

Discussion in 'UNet' started by wasicool7, Apr 18, 2017.

  1. wasicool7

    wasicool7

    Joined:
    Jul 28, 2016
    Posts:
    25
    I'm following a tutorial unity multiplayer (UNET) on YouTube but I'm getting different results compared to the guy in the video. You see I followed his tutorial to the tea, however in my server script within the update function I used a debug.log in the switch statement default function to tell me if i'm receiving invalid messaging, which I am. I think that my message isn't really being sent properly despite the fact that I followed the guy's tutorial carefully! Am I missing something in my server script?

    This is my server code:

    Code (CSharp):
    1.     public class ServerClient
    2.      {
    3.       public int connectionId;
    4.       public string playerName;
    5.      }
    6.  
    7.     public class Server : MonoBehaviour {
    8.  
    9.     private const int MAX_CONNECTION = 4;
    10.  
    11.     private int port = 8888;
    12.  
    13.     private int hostId;
    14.     private int webHostId;
    15.  
    16.     private int reliableChannel;
    17.     private int unreliableChannel;
    18.  
    19.     private bool isStarted = false;
    20.     private byte error;
    21.  
    22.     private List<ServerClient> clients = new List<ServerClient>();
    23.  
    24.     private void Start()
    25.     {
    26.         NetworkTransport.Init ();
    27.         ConnectionConfig cc = new ConnectionConfig ();
    28.  
    29.         reliableChannel = cc.AddChannel (QosType.Reliable);
    30.         unreliableChannel = cc.AddChannel (QosType.Unreliable);
    31.  
    32.         HostTopology topo = new HostTopology (cc, MAX_CONNECTION);
    33.  
    34.         hostId = NetworkTransport.AddHost (topo, port); // null
    35.         Debug.Log ("Socket Open. hostId is: " + hostId);
    36.         webHostId = NetworkTransport.AddWebsocketHost (topo, port, null); //, port, null
    37.    
    38.         isStarted = true;
    39.     }
    40.  
    41.     private void Update()
    42.     {
    43.         if (!isStarted)
    44.             return;
    45.        
    46.         int recHostId;
    47.         int connectionId;
    48.         int channelId;
    49.         byte[] recBuffer = new byte[1024];
    50.         int bufferSize = 1024;
    51.         int dataSize;
    52.         byte error;
    53.         NetworkEventType recData = NetworkTransport.Receive (out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
    54.         switch (recData) {
    55.         case NetworkEventType.ConnectEvent:   //2
    56.             Debug.Log ("Player " + connectionId + "has connected");
    57.             OnConnection (connectionId);
    58.             break;
    59.         case NetworkEventType.DataEvent:      //3
    60.             string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
    61.             //Debug.Log("Player" + connectionId + " has sent :  " + msg);
    62.             Debug.Log("Recieving from " + connectionId + " : " + msg);
    63.  
    64.             string[] splitData = msg.Split ('|');
    65.  
    66.             switch (splitData[0])
    67.             {
    68.             case "OnNameIs":
    69.                 OnNameIs (connectionId, splitData [1]);
    70.                     break;
    71.  
    72.                 default:
    73.                     Debug.Log ("Invalid message:  " + msg);
    74.                     break;
    75.             }
    76.  
    77.             break;
    78.  
    79.             case NetworkEventType.DisconnectEvent:     // 4
    80.             Debug.Log("Player " + connectionId + "has disconnected");
    81.             break;
    82.         }
    83.     }
    84.        
    85.  
    86.     private void OnConnection(int cnnId)
    87.     {
    88.         // This may add a thrid player
    89.          ServerClient c = new ServerClient();
    90.          c.connectionId = cnnId;
    91.          c.playerName = "TEMP";
    92.          clients.Add(c);
    93.          //So you might want to change this later
    94.  
    95.         string msg = "AskName|" + cnnId + "|";
    96.         foreach (ServerClient sc in clients)
    97.             msg += sc.playerName + '%' + sc.connectionId + '|';
    98.    
    99.         msg = msg.Trim ('|');
    100.  
    101.         Send (msg, reliableChannel, cnnId);
    102.     }
    103.  
    104.     private void OnNameIs(int cnnId, string playerName)
    105.     {
    106.         clients.Find (x => x.connectionId == cnnId).playerName = playerName;
    107.  
    108.         Send ("CNN|" + playerName + '|' + cnnId, reliableChannel, clients);
    109.     }
    110.  
    111.     private void Send(string message, int channelId, int cnnId)
    112.     {
    113.         List<ServerClient> c = new List<ServerClient>();
    114.         c.Add (clients.Find (x => x.connectionId == cnnId));
    115.         Send (message, channelId, c);
    116.     }
    117.  
    118.     private void Send(string message, int channelId, List<ServerClient> c)
    119.     {
    120.         Debug.Log ("Sending  : " + message);
    121.         byte[] msg = Encoding.Unicode.GetBytes (message);
    122.         foreach (ServerClient sc in c)
    123.         {
    124.             NetworkTransport.Send (hostId, sc.connectionId, channelId, msg, message.Length * sizeof(char), out error);
    125.         }
    126.     }
    127. }
    128.  
    The below script is my client script:


    Code (CSharp):
    1.     public class Player
    2.     {
    3.       public string playerName;
    4.       public GameObject avatar;
    5.       public int connectionId;
    6.     }
    7.  
    8.     public class Client : MonoBehaviour {
    9.  
    10.  
    11.     private const int MAX_CONNECTION = 4;
    12.  
    13.     private int port = 8888; //5701
    14.  
    15.     private int hostId;
    16.     private int webHostId;
    17.  
    18.     private int connectionId;
    19.     private int ourClientId;
    20.  
    21.     private int reliableChannel;
    22.     private int unreliableChannel;
    23.  
    24.     private float connectionTime;
    25.     private bool isConnected = false;
    26.     private bool isStarted = false;
    27.     private byte error;
    28.  
    29.     private string playerName;
    30.  
    31.     public GameObject playerPrefab;
    32.  
    33.     public List<Player> players = new List<Player>();
    34.  
    35.     public void Connect()
    36.     {
    37.         //Does the player have a name?
    38.         //take this part out
    39.         string pName = GameObject.Find("NameInput").GetComponent<InputField>().text;
    40.  
    41.         if (pName == "") {
    42.             Debug.Log ("you must enter a name");
    43.             return;
    44.         }
    45.  
    46.         playerName = pName;
    47.  
    48.         //
    49.  
    50.         // place this is the Start fuction
    51.         NetworkTransport.Init ();
    52.         ConnectionConfig cc = new ConnectionConfig ();
    53.  
    54.         reliableChannel = cc.AddChannel (QosType.Reliable);
    55.         unreliableChannel = cc.AddChannel (QosType.Unreliable);
    56.  
    57.         HostTopology topo = new HostTopology (cc, MAX_CONNECTION);
    58.  
    59.         hostId = NetworkTransport.AddHost (topo, 0);
    60.         //
    61.  
    62.         byte error;
    63.         connectionId = NetworkTransport.Connect (hostId, "127.0.0.1", port, 0, out error);
    64.         Debug.Log ("Connection to server. ConnectionId: " + connectionId);
    65.         connectionTime = Time.time;
    66.         isConnected = true;
    67.     }
    68.  
    69.     private void Update()
    70.     {
    71.         if (!isConnected)
    72.             return;
    73.        
    74.         int recHostId;
    75.         int connectionId;
    76.         int channelId;
    77.         byte[] recBuffer = new byte[1024];
    78.         int bufferSize = 1024;
    79.         int dataSize;
    80.         byte error;
    81.         NetworkEventType recData = NetworkTransport.Receive (out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
    82.         switch (recData) {
    83.         case NetworkEventType.DataEvent:      //1
    84.             string msg = Encoding.Unicode.GetString (recBuffer, 0, dataSize);
    85.             Debug.Log ("Recieving : " + msg);
    86.             string[] splitData = msg.Split ('|');
    87.  
    88.             switch (splitData[0])
    89.             {
    90.             case "AskName":
    91.                 OnAskName (splitData);
    92.                 break;
    93.  
    94.             case "CNN":
    95.                 SpawnPlayer (splitData[1], int.Parse(splitData[2]));
    96.                 break;
    97.  
    98.             case "DC":
    99.                 break;
    100.  
    101.             default:
    102.                 Debug.Log ("Invalid message:  " + msg);
    103.                 break;
    104.             }
    105.  
    106.             break;
    107.         }
    108.     }
    109.  
    110.     private void OnAskName(string[] data)
    111.     {
    112.         ourClientId = int.Parse (data [1]);
    113.  
    114.         Send ("OnNameis|" + playerName, reliableChannel);
    115.  
    116.         for (int i = 2; i < data.Length - 1; i++)
    117.         {
    118.             string[] d = data[i].Split('%');
    119.             SpawnPlayer(d[0], int.Parse(d[1]));
    120.         }
    121.     }
    122.  
    123.     private void SpawnPlayer(string playerName, int cnnId)
    124.     {
    125.         GameObject go = Instantiate (playerPrefab) as GameObject;
    126.         Debug.Log ("Object has been spawn", go);
    127.  
    128.         if (cnnId == ourClientId)  // the problem //
    129.         {
    130.             GameObject.Find("Canvas").SetActive (false);
    131.             isStarted = true;
    132.         }
    133.  
    134.         Player p = new Player ();
    135.         p.avatar = go;
    136.         p.playerName = playerName;
    137.         p.connectionId = cnnId;
    138.         p.avatar.GetComponentInChildren<TextMesh>().text = playerName;
    139.         players.Add (p);
    140.     }
    141.  
    142.     private void Send(string message, int channelId)
    143.     {
    144.         Debug.Log ("Sending  : " + message);
    145.         byte[] msg = Encoding.Unicode.GetBytes (message);
    146.         NetworkTransport.Send (hostId, connectionId, channelId, msg, message.Length * sizeof(char), out error);
    147.     }
    148.  
    Thank you in advance! :)