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.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("Handshake message received!"); myID = hsMsg.player.id; playerObject = Instantiate(playerPrefab); playerObject.gameObject.name = myID; currentPlayers.Add(myID, playerObject); StartCoroutine(SendPositionToServer(0.2f)); 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!"); latestGameState = suMsg.players; //Debug.Log(recMsg); break; case Commands.DROPPED_CLIENT: DroppedClientMsg dcMsg = JsonUtility.FromJson <DroppedClientMsg>(recMsg); Destroy(currentPlayers[dcMsg.player.id]); currentPlayers.Remove(dcMsg.player.id); Debug.Log(dcMsg); break; default: Debug.Log("Unrecognized message received!"); break; } }
void OnDisconnect(int i) { Debug.Log("Client disconnected from server"); DroppedClientMsg m = new DroppedClientMsg(); m.player.id = m_Connections[i].InternalId.ToString(); foreach (NetworkConnection c in m_Connections) { SendToClient(JsonUtility.ToJson(m), c); } m_connectedClients[m_Connections[i].InternalId.ToString()] = null; m_connectedClients.Remove(m_Connections[i].InternalId.ToString()); m_Connections[i] = default(NetworkConnection); }