Search Unity

LLAPI Issue, Player Updating?

Discussion in 'Multiplayer' started by coombsjoshua20, Sep 10, 2017.

  1. coombsjoshua20

    coombsjoshua20

    Joined:
    Oct 19, 2016
    Posts:
    19
    So im using LLAPI to manage my server and client for a game. Im currently getting this issue however.

    This is printed in the console when it should be spitting out constant updated coordinates for players..

    Here is the code for client thus far.. I really cannot see the issue

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Text;
    4. using UnityEngine;
    5. using UnityEngine.Networking;
    6. using UnityEngine.UI;
    7.  
    8. public class Client : MonoBehaviour {
    9.  
    10.     public class Player
    11.     {
    12.         public string playerName;
    13.         public GameObject avatar;
    14.         public int connectionId;
    15.     }
    16.  
    17.     private const int MAX_CONNECTION = 100;
    18.  
    19.     private int port = 5701;
    20.  
    21.     private int hostId;
    22.     private int webHostId;
    23.  
    24.     private int ourClientId;
    25.     private int connectionId;
    26.  
    27.     private int reliableChannel;
    28.     private int unreliablechannel;
    29.  
    30.     private float connectionTime;
    31.     private bool isStarted = false;
    32.     private byte error;
    33.     private bool isConnected = false;
    34.  
    35.     private string playerName;
    36.  
    37.     public GameObject playerPrefab;
    38.     public Dictionary<int,Player> players = new Dictionary<int, Player>();
    39.  
    40.     public void Connect()
    41.     {
    42.         //Does the player have a name
    43.         string pName = GameObject.Find("NameInput").GetComponent<InputField>().text;
    44.         if (pName == "")
    45.         {
    46.             Debug.Log("You must set a name");
    47.             return;
    48.         }
    49.  
    50.         playerName = pName;
    51.  
    52.         NetworkTransport.Init();
    53.         ConnectionConfig cc = new ConnectionConfig();
    54.  
    55.         reliableChannel = cc.AddChannel(QosType.Reliable);
    56.         unreliablechannel = cc.AddChannel(QosType.Unreliable);
    57.  
    58.         HostTopology topo = new HostTopology(cc, MAX_CONNECTION);
    59.  
    60.         hostId = NetworkTransport.AddHost(topo, 0);
    61.         connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);
    62.  
    63.         connectionTime = Time.time;
    64.         isConnected = true;
    65.     }
    66.  
    67.     private void Update()
    68.     {
    69.         if (!isConnected)
    70.             return;
    71.  
    72.         int recHostId;
    73.         int connectionId;
    74.         int channelId;
    75.         byte[] recBuffer = new byte[1024];
    76.         int bufferSize = 1024;
    77.         int dataSize;
    78.         byte error;
    79.         NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
    80.         switch (recData)
    81.         {
    82.             case NetworkEventType.DataEvent:           //3
    83.                 string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
    84.                 Debug.Log("Receiving : " + msg);
    85.                 string[] splitData = msg.Split('|');
    86.  
    87.                 switch (splitData[0])
    88.                 {
    89.                     case "ASKNAME":
    90.                         OnAskName(splitData);
    91.                         break;
    92.  
    93.                     case "CNN":
    94.                         SpawnPlayer(splitData[1], int.Parse(splitData[2]));
    95.                     break;
    96.  
    97.                     case "DC":
    98.                         PlayerDisconnected(int.Parse(splitData[1]));
    99.                 break;
    100.  
    101.                     case "ASKPOSITION":
    102.                         OnAskPosition(splitData);
    103.                         break;
    104.  
    105.                     default:
    106.                         Debug.Log("Invalid message : " + msg);
    107.                         break;
    108.                 }
    109.  
    110.                 break;
    111.  
    112.         }
    113.     }
    114.  
    115.     private void OnAskName(string[] data)
    116.     {
    117.         //Set this clients ID
    118.         ourClientId = int.Parse(data[1]);
    119.  
    120.         // Send our name to the server
    121.         Send("NAMEIS|" + playerName, reliableChannel);
    122.  
    123.         // Create all the other players
    124.         for (int i = 2; i < data.Length - 1; i++)
    125.         {
    126.             string[] d = data[i].Split('%');
    127.             SpawnPlayer(d[0], int.Parse(d[1]));
    128.         }
    129.     }
    130.  
    131.     private void OnAskPosition(string[] data)
    132.     {
    133.         if (!isStarted)
    134.             return;
    135.         //update everyone else
    136.         for (int i = 1; i <= data.Length-1; i++)
    137.         {
    138.  
    139.  
    140.             string[] d = data[i].Split('%');
    141.  
    142.             // prevent the server from updating us
    143.             if (ourClientId != int.Parse(d[0]))
    144.             {
    145.             Vector3 position = Vector3.zero;
    146.             position.x = float.Parse(d[1]);
    147.             position.y = float.Parse(d[2]);
    148.             players[int.Parse(d[0])].avatar.transform.position = position;
    149.             }
    150.         }
    151.  
    152.         //Send own position
    153.         Vector3 myPosition = players[ourClientId].avatar.transform.position;
    154.         string m = "MYPOSITION|" + myPosition.x.ToString() + '|' + myPosition.y.ToString();
    155.         Send(m, unreliablechannel);
    156.  
    157.     }
    158.  
    159.     private void SpawnPlayer(string playerName, int cnnId)
    160.     {
    161.         GameObject go = Instantiate(playerPrefab) as GameObject;
    162.  
    163.         //Is this ours?
    164.         if (cnnId == ourClientId)
    165.         {
    166.             //add mobility
    167.             go.AddComponent<PlayerCon>();
    168.             GameObject.Find("Canvas").SetActive(false);
    169.             isStarted = true;
    170.         }
    171.  
    172.         Player p = new Player();
    173.         p.avatar = go;
    174.         p.playerName = playerName;
    175.         p.connectionId = cnnId;
    176.         players.Add(cnnId, p);
    177.     }
    178.  
    179.     private void PlayerDisconnected(int cnnId)
    180.     {
    181.         Destroy(players[cnnId].avatar);
    182.         players.Remove(cnnId);
    183.     }
    184.  
    185.     private void Send(string message, int channelId)
    186.     {
    187.         Debug.Log("Sending : " + message);
    188.         byte[] msg = Encoding.Unicode.GetBytes(message);
    189.  
    190. NetworkTransport.Send(hostId,connectionId, channelId, msg, message.Length * sizeof(char), out error);
    191.        
    192.     }
    193. }
    194.  
     
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    You should REALLY not use unicode encoded strings like that for your messages. You probably want to use the NetworkWriter & NetworkReader,

    In addition, why is your server even asking for the position? Why doesnt the client just send it x times per second.

    As for the issue. The message recieved is missing a | symbol.
     
    xVergilx likes this.