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.CONNECTION_APPROVED: // SELF ConnectionApprovedMsg cpMsg = JsonUtility.FromJson <ConnectionApprovedMsg>(recMsg); Debug.Log("Connection Approved message received!"); foreach (NetworkObjects.NetworkPlayer player in cpMsg.player) { Debug.Log("Our Internal ID: " + player.id); Debug.Log("Color R: " + player.cubeColor.r + " Color G: " + player.cubeColor.g + " Color B: " + player.cubeColor.b); Debug.Log("Pos X: " + player.cubPos.x + " Pos Y: " + player.cubPos.y + " Pos Z: " + player.cubPos.z); newPlayers.Add(player.id); myAddress = player.id; } break; case Commands.PLAYER_CONNECTED: // Everyone else PlayerConnectedMsg pcMsg = JsonUtility.FromJson <PlayerConnectedMsg>(recMsg); Debug.Log("Player Connected message received!"); foreach (NetworkObjects.NetworkPlayer player in pcMsg.player) { //Debug.Log("Our Internal ID: " + player.id); //Debug.Log("Color R: " + player.cubeColor.r + " Color G: " + player.cubeColor.g + " Color B: " + player.cubeColor.b); //Debug.Log("Pos X: " + player.cubPos.x + " Pos Y: " + player.cubPos.y + " Pos Z: " + player.cubPos.z); newPlayers.Add(player.id); } break; case Commands.PLAYER_DISCONNECTED: PlayerDisconnectMsg pdMsg = JsonUtility.FromJson <PlayerDisconnectMsg>(recMsg); Debug.Log("Player disconnect message received! User ID: " + pdMsg.player.id); droppedPlayers.Add(pdMsg.player.id); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Player update message received!"); break; case Commands.GAMESTATE: GameStateMsg gsMsg = JsonUtility.FromJson <GameStateMsg>(recMsg); Debug.Log("Game State update message received!"); lastestGameState = gsMsg; for (int i = 0; i < lastestGameState.GameState.Count; i++) { Debug.Log("Game State ID: " + lastestGameState.GameState[i].id + " RGB: " + lastestGameState.GameState[i].cubeColor + "Pos: " + lastestGameState.GameState[i].cubPos); } break; case Commands.SERVER_UPDATE: // Gives The Player List Already In Server ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); for (int i = 0; i < suMsg.players.Count; i++) { Debug.Log("PlayerList: " + suMsg.players[i].id); } initialSetofPlayers = suMsg; break; default: Debug.Log("Unrecognized message received!"); break; } }
void OnConnect(NetworkConnection c) { m_Connections.Add(c); Debug.Log("Accepted a connection"); //playerData.player.id = Null playerData.player.cubeColor.r = 0; playerData.player.cubeColor.g = 0; playerData.player.cubeColor.b = 0; playerData.player.cubPos.x = 0; playerData.player.cubPos.y = 0; playerData.player.cubPos.z = 0; Debug.Log("Connected List Length: " + ConnectedList.GameState.Count + " Connected User ID: " + c.InternalId); NetworkObjects.NetworkPlayer newConnectID = new NetworkObjects.NetworkPlayer(); newConnectID.pulse = DateTime.Now; newConnectID.id = c.InternalId.ToString(); // Value 2 newConnectID.cubeColor = playerData.player.cubeColor; newConnectID.cubPos = playerData.player.cubPos; ConnectedList.GameState.Add(newConnectID); SendToClient(JsonUtility.ToJson(ConnectedList), c); Debug.Log("Completed ConnectedList Update"); // Send The Connected User Their own ID // Player 1 = 0, Player 2 = 1 ConnectionApprovedMsg cpMsg = new ConnectionApprovedMsg(); NetworkObjects.NetworkPlayer connectID = new NetworkObjects.NetworkPlayer(); connectID.id = c.InternalId.ToString(); cpMsg.player.Add(connectID); SendToClient(JsonUtility.ToJson(cpMsg), c); Debug.Log("Sent Connected User ID"); // Sends The Player List To The Newly Connect ID ServerUpdateMsg suM = new ServerUpdateMsg(); for (int i = 0; i < m_Connections.Length; i++) { NetworkObjects.NetworkPlayer tempID = new NetworkObjects.NetworkPlayer(); tempID.id = ConnectedList.GameState[i].id; suM.players.Add(tempID); Debug.Log("Got This -> " + suM.players[i].id); } Debug.Log("Amount Connected -> " + m_Connections.Length); SendToClient(JsonUtility.ToJson(suM), c); // Send New Connected User ID to all Existing Players That Are Connected PlayerConnectedMsg pcMsg = new PlayerConnectedMsg(); for (int i = 0; i < m_Connections.Length; i++) { if (c.InternalId.ToString() != ConnectedList.GameState[i].id) // 2 != 2 0, 1, will work { newConnectID.id = c.InternalId.ToString(); pcMsg.player.Add(newConnectID); SendToClient(JsonUtility.ToJson(pcMsg), m_Connections[i]); } } }