void OnData(DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.INITIALIZE: InitialMsg initMsg = JsonUtility.FromJson <InitialMsg>(recMsg); localID = initMsg.serverID; break; case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("Handshake message received!: " + hsMsg.player.id); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Player update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); UpdateList(suMsg.players); break; case Commands.PLAYER_DISCONNECT: DisconnectMsg disMsg = JsonUtility.FromJson <DisconnectMsg>(recMsg); Debug.Log("Disconnection message received!: " + disMsg.serverID); RemovePlayer(disMsg.serverID); break; default: Debug.Log("Unrecognized message received!"); break; } }
void OnConnect(NetworkConnection c) { m_Connections.Add(c); Debug.Log("Accepted a connection"); //Send initial Message to client InitialMsg m = new InitialMsg(); m.serverID = c.InternalId.ToString(); SendToClient(JsonUtility.ToJson(m), c); //Build new player var player = new NetworkObjects.NetworkPlayer(); player.id = c.InternalId.ToString(); player.cubeColor = new Color( UnityEngine.Random.Range(0.0f, 1.0f), UnityEngine.Random.Range(0.0f, 1.0f), UnityEngine.Random.Range(0.0f, 1.0f)); //Add to servers list //will be added to all clients on next update players.Add(player); }