void OnDisconnect(int i) { Debug.Log("Client disconnected from server"); PlayerLeaveMsg plMsg = new PlayerLeaveMsg(); foreach (NetworkObjects.NetworkPlayer player in serverPlayerList) { if (player.id == m_Connections[i].InternalId.ToString()) { plMsg.player = player; } } foreach (var client in m_Connections) { if (client != m_Connections[i]) { SendToClient(JsonUtility.ToJson(plMsg), client); } } m_Connections[i] = default(NetworkConnection); foreach (NetworkObjects.NetworkPlayer player in serverPlayerList) { if (player.id == plMsg.player.id) { Debug.Log("Player Size: " + serverPlayerList.Count()); Debug.Log("Disconnecting Player with Id: " + player.id); serverPlayerList.Remove(player); } } }
void OnData(DataStreamReader stream, int i) { 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!"); Debug.Log("Player List: " + serverPlayerList); foreach (NetworkObjects.NetworkPlayer player in serverPlayerList) { if (player.id == puMsg.player.id) { player.cubeColor = puMsg.player.cubeColor; player.cubPos = puMsg.player.cubPos; player.cubRot = puMsg.player.cubRot; } } break; case Commands.PLAYER_BULLET: PlayerBulletMsg pbMsg = JsonUtility.FromJson <PlayerBulletMsg>(recMsg); Debug.Log("Player Bullet Fired!"); FireBulletForPlayers(pbMsg, m_Connections[i]); break; case Commands.PLAYER_JOINED: PlayerJoinMessage pjMsg = JsonUtility.FromJson <PlayerJoinMessage>(recMsg); Debug.Log("Player join message received!"); CreatePlayer(pjMsg, m_Connections[i]); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; case Commands.PLAYER_LEFT: PlayerLeaveMsg plMsg = JsonUtility.FromJson <PlayerLeaveMsg>(recMsg); Debug.Log("Player leave message recieved!"); OnDisconnect(int.Parse(plMsg.player.id)); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }
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); Debug.Log("Got this: " + header.cmd); switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("Handshake message received!"); clientID = hsMsg.player.id; Debug.Log("Id: " + clientID); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Player update message received!"); break; case Commands.PLAYER_BULLET: PlayerBulletMsg pbMsg = JsonUtility.FromJson <PlayerBulletMsg>(recMsg); Debug.Log("Player Bullet Fired Message Recieved!"); GameObject.Instantiate(bulletRef, (pbMsg.player.cubPos + (pbMsg.clickedLocation - pbMsg.player.cubPos).normalized), Quaternion.LookRotation((pbMsg.clickedLocation - pbMsg.player.cubPos).normalized)); break; case Commands.PLAYER_JOINED: PlayerJoinMessage pjMsg = JsonUtility.FromJson <PlayerJoinMessage>(recMsg); Debug.Log("Player join message received!"); pjMsg.player.cube = Instantiate(cubeRef); pjMsg.player.cube.GetComponent <Renderer>().material.SetColor("_Color", pjMsg.player.cubeColor); if (pjMsg.player.id == clientID) { pjMsg.player.cube.GetComponent <Character>().networkManRef = this; pjMsg.player.cube.GetComponent <Character>().playerRef = pjMsg.player; } else { pjMsg.player.cube.GetComponent <Character>().setStats(); } playerList.Add(pjMsg.player); break; case Commands.SERVER_UPDATE: suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); foreach (NetworkObjects.NetworkPlayer clientPlayer in playerList) { if (clientPlayer.id == clientID) { } else { foreach (NetworkObjects.NetworkPlayer serverPlayer in suMsg.players) { if (serverPlayer.id == clientPlayer.id) { clientPlayer.cube.transform.position = serverPlayer.cubPos; } else { } } } } break; case Commands.PLAYER_LEFT: PlayerLeaveMsg plMsg = JsonUtility.FromJson <PlayerLeaveMsg>(recMsg); Debug.Log("Player Leave message received!"); KillClientPlayer(plMsg.player); OnDisconnect(); break; default: Debug.Log("Unrecognized message received!"); break; } }