void OnDisconnect(int i) { Debug.Log("Client disconnected from server"); DisconnectedPlayerMsg m = new DisconnectedPlayerMsg(m_Connections[i].InternalId.ToString()); SendToClient(JsonUtility.ToJson(m), m_Connections[i]); m_Connections[i] = default(NetworkConnection); }
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!"); 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); for (int i = 0; i < suMsg.players.Count; i++) { bool spawned = false; foreach (GameObject p in activeCubes) { if (p.GetComponent <CubeBase>().id == suMsg.players[i].id) { spawned = true; p.GetComponent <CubeBase>().ColourChange(suMsg.players[i].R, suMsg.players[i].G, suMsg.players[i].B); if (p.GetComponent <PlayerCube>() == null) { p.transform.position = new Vector3(suMsg.players[i].X, suMsg.players[i].Y, suMsg.players[i].Z); } } } if (!spawned) { AddCube(suMsg.players[i]); } } //Debug.Log("Server update message received!"); break; case Commands.CREATE_PLAYER: // Create local cube Debug.Log("CREATE PLAYER message received!"); CreatePlayerMsg myCubeMsg = JsonUtility.FromJson <CreatePlayerMsg>(recMsg); SpawnMyCube(myCubeMsg); break; case Commands.DESTROY_PLAYER: // Debug.Log("DESTROY_PLAYER message received!"); DisconnectedPlayerMsg dpMsg = JsonUtility.FromJson <DisconnectedPlayerMsg>(recMsg); for (int i = 0; i < activeCubes.Count; i++) { if (activeCubes[i].GetComponent <CubeBase>().id == dpMsg.droppedID) { activeCubes[i].SetActive(false); //DestroyImmediate(activeCubes[i]); activeCubes.RemoveAt(i); i--; } } break; default: Debug.Log("Unrecognized message received!"); break; } }