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("Server: Hello, player " + hsMsg.player.id.ToString()); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); m_listOfPlayers[i].body.transform.position = puMsg.player.body.transform.position; ServerUpdateMsg serUpMsg = new ServerUpdateMsg(); serUpMsg.players = m_listOfPlayers; SendToClient(JsonUtility.ToJson(serUpMsg), m_Connections[i]); Debug.Log("Player update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }
void OnData(int i, 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.LOGIN: LoginMsg lMsg = JsonUtility.FromJson <LoginMsg>(recMsg); Debug.Log("[Server] Client login message received! : " + lMsg.clientId); OnClientLogIn(i, lMsg.clientId); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); //Debug.Log( "[Server] Player update message received! : " + puMsg.player.ToString() ); GameServerManager.Instance.UpdatePlayerCommands(puMsg.player, puMsg.commands); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("[Server] Server update message received!"); break; default: Debug.Log("[Server] Unrecognized message received!"); break; } }
void OnConnect(NetworkConnection c) { m_Connections.Add(c); Debug.Log("Accepted a connection"); HandshakeMsg message = new HandshakeMsg(); message.player.id = c.InternalId.ToString(); SendToClient(JsonUtility.ToJson(message), c); NetworkObjects.NetworkPlayer nPlayer = new NetworkObjects.NetworkPlayer(); nPlayer.id = c.InternalId.ToString(); nPlayer.cubeColor = UnityEngine.Random.ColorHSV(0f, 1f, 1f, 1f, 0f, 1f); nPlayer.cubPos = new Vector3(UnityEngine.Random.Range(-5, 5), UnityEngine.Random.Range(-5, 5), UnityEngine.Random.Range(0, 0)); for (int i = 0; i < m_Connections.Length; i++) { if (m_Connections[i] != c) { PlayerUpdateMsg othermessage = new PlayerUpdateMsg(); othermessage.cmd = Commands.PLAYER_JOINED; othermessage.player = nPlayer; SendToClient(JsonUtility.ToJson(othermessage), m_Connections[i]); } } PlayerList.Add(nPlayer); foreach (var player in PlayerList) { PlayerUpdateMsg otherothermessage = new PlayerUpdateMsg(); otherothermessage.cmd = Commands.PLAYER_JOINED; otherothermessage.player = player; SendToClient(JsonUtility.ToJson(otherothermessage), c); } }
void OnConnect(NetworkConnection c) { m_Connections.Add(c); m_Players.Add(new NetworkObjects.NetworkPlayer()); m_Players[m_Players.Count - 1].id = c.InternalId.ToString(); //Yeah, I know I did something unreadable here. //But it felt more practical to assign these things simultaneously PlayerUpdateMsg msg = new PlayerUpdateMsg(); msg.player.id = m_Players[m_Players.Count - 1].id; System.Random r = new System.Random(); msg.player.r = m_Players[m_Players.Count - 1].r = (float)r.NextDouble(); msg.player.g = m_Players[m_Players.Count - 1].g = (float)r.NextDouble(); msg.player.b = m_Players[m_Players.Count - 1].b = (float)r.NextDouble(); msg.player.x = m_Players[m_Players.Count - 1].x = r.Next(0, 16); msg.player.y = m_Players[m_Players.Count - 1].y = r.Next(0, 16); msg.player.z = m_Players[m_Players.Count - 1].z = r.Next(0, 16); SendToClient(JsonUtility.ToJson(msg), c); Debug.Log("Accepted a connection! ID: " + c.InternalId.ToString() + ".\nTotal players connected: " + m_Connections.Length.ToString()); //Create Cube message: SendUpdateMessage(); PrintReport(); }
void UpdatePlayers() { if (latestGameState.GameUpdate.Count > 0) { foreach (NetworkObjects.NetworkPlayer player in latestGameState.GameUpdate) { string playerID = player.id; //No more disco cube //currentPlayers[player.id].GetComponent<Renderer>().material.color = new Color(player.color.R, player.color.G, player.color.B); //This makes sure that the server doesn't change my position back to where it was if (player.id != myAddress) { //This is how you see the updated position of other players currentPlayers[player.id].GetComponent <Transform>().position = new Vector3(player.cubPos.x, player.cubPos.y, player.cubPos.z); } } //Sending the server information of our cubes position so that they can update other cubes of our location. Pretty cool man! foreach (NetworkObjects.NetworkPlayer player in latestGameState.GameUpdate) { if (player.id == myAddress) { //Getting the transform of the players cube to send to server PlayerUpdateMsg playerLocalPosition = new PlayerUpdateMsg(); playerLocalPosition.player.id = player.id; playerLocalPosition.player.cubPos.x = currentPlayers[player.id].GetComponent <Transform>().position.x; playerLocalPosition.player.cubPos.y = currentPlayers[player.id].GetComponent <Transform>().position.y; playerLocalPosition.player.cubPos.z = currentPlayers[player.id].GetComponent <Transform>().position.z; //Making the position into a json and sending it to the server //string positionBytes = JsonUtility.ToJson(playerLocalPosition); //Byte[] sendingPositionBytes = Encoding.UTF8.GetBytes(positionBytes); //udp.Send(sendingPositionBytes, sendingPositionBytes.Length); SendToServer(JsonUtility.ToJson(playerLocalPosition)); } } latestGameState.GameUpdate = new List <NetworkObjects.NetworkPlayer>(); } }
void OnData(DataStreamReader stream, int i) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); // Convert the stream into bytes stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); // Convert bytes into string NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); // Convert the string into a json object 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); clients[puMsg.player.id].cubPos = puMsg.player.cubPos; //clients[puMsg.player.id].cubeColor = puMsg.player.cubeColor; Debug.Log("Player update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }
void OnData(DataStreamReader stream, int i, NetworkConnection client) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); //Where to store bytes data stream.ReadBytes(bytes); //Get bytes data from stream string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); //convert bytes data to string(JSON) NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); //convert JSON to c# object 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("Got " + puMsg.player.id + " player Info"); UpdateClientInfo(puMsg); //Debug.Log("Player update message received!"); //Debug.Log("Got data from client, Player Pos: " + puMsg.player.pos); //TestSendBack(client, puMsg); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }
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!"); UpdatePlayerInfo(puMsg.player); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); 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); //Get bytes string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); //convert bytes to JSON string NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); //convert JSON to c# class switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("Handshake message received!"); break; case Commands.PLAYER_INTERNALID: PlayerUpdateMsg internalId = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Got internalId from server"); playerInfo.player.id = internalId.player.id; playerInternalIdText.text = playerInfo.player.id; break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); //Debug.Log("Player update message received!"); Debug.Log("Got data from server, player Pos: " + puMsg.player.pos); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); UpdateClientsInfo(suMsg); break; //To spawn existed players case Commands.SPAWN_EXISTED_PLAYERS: ServerUpdateMsg existedPlayerInfo = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("existed player info received!"); SpawnExistedPlayer(existedPlayerInfo); break; //Spawn new player case Commands.SPAWN_NEW_PLAYER: PlayerUpdateMsg newPlayerInfo = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("new client info received!"); SpawnNewPlayer(newPlayerInfo); break; //handle disconnected player case Commands.DISCONNECTED_PLAYER: DisconnectedPlayersMsg dpm = JsonUtility.FromJson <DisconnectedPlayersMsg>(recMsg); Debug.Log("Disconnected player info recieved!"); DeleteDisconnectPlayer(dpm); break; default: Debug.Log("Unrecognized message received!"); break; } }
void UpdatePlayers() { if (lastestGameState.GameState.Count > 0) { foreach (NetworkObjects.NetworkPlayer player in lastestGameState.GameState) { string playerID = player.id; Debug.Log("Game State ID: " + player.id + " Our InternalID: " + myAddress); //currentPlayers[player.id].GetComponent<Renderer>().material.color = new Color(player.cubeColor.r, player.cubeColor.g, player.cubeColor.b); if (player.id != myAddress) { Debug.Log("Setting Position of random ass player"); currentPlayers[player.id].GetComponent <Transform>().position = new Vector3(player.cubPos.x, player.cubPos.y, player.cubPos.z); } } foreach (NetworkObjects.NetworkPlayer player in lastestGameState.GameState) { if (player.id == myAddress) { Debug.Log("Yeeting my position to the server"); PlayerUpdateMsg playerData = new PlayerUpdateMsg(); //playerData.player.pulse = DateTime.Now; playerData.player.id = player.id; playerData.player.cubPos.x = currentPlayers[player.id].GetComponent <Transform>().position.x; playerData.player.cubPos.y = currentPlayers[player.id].GetComponent <Transform>().position.y; playerData.player.cubPos.z = currentPlayers[player.id].GetComponent <Transform>().position.z; SendToServer(JsonUtility.ToJson(playerData)); } } lastestGameState.GameState = new List <NetworkObjects.NetworkPlayer>(); } }
private IEnumerator UploadClientDataRoutine(float timeInterval) { PlayerUpdateMsg updateMsg = new PlayerUpdateMsg(); if (m_thisClientID != 0) { while (bUpdateServer) { if (bVerboseDebug) { Debug.Log("[Routine] Updating server with local character data."); } //Inserting player data in message updateMsg.player.clientID = m_thisClientID; updateMsg.player.cubePosition = m_clientIDDict[m_thisClientID].position; updateMsg.player.cubeOrientation = m_clientIDDict[m_thisClientID].eulerAngles; updateMsg.player.cubeColor = Color.white; updateMsg.player.bUnassignedData = false; //Sending message SendToServer(JsonUtility.ToJson(updateMsg)); yield return(new WaitForSeconds(timeInterval)); } } else { Debug.LogError("[Error] local client ID unassigned (0). Aborting operation..."); } }
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("[Client] Handshake message received! " + hsMsg.player.id); GameplayManager.Instance.SpawnPlayer(hsMsg.player, true); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("[Client] Player update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("[Client] Server update message received!" + suMsg.players.ToArrayString()); GameplayManager.Instance.UpdatePlayers(suMsg.players); break; default: Debug.Log("[Client] Unrecognized message received!"); break; } }
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()); NetworkMessage header = JsonUtility.FromJson <NetworkMessage>(recMsg); switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); AddPlayer(hsMsg); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); UpdatePlayer(puMsg); break; case Commands.DISCONNECT: DisconnectMsg dcMsg = JsonUtility.FromJson <DisconnectMsg>(recMsg); RemovePlayer(dcMsg); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }
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.NEW_CLIENT: NewClientMsg ncMsg = JsonUtility.FromJson <NewClientMsg>(recMsg); Debug.Log("Server msg: New Client message received!"); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Server msg: Player update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); ServerMessage(suMsg); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }
void SpawnNewPlayer(PlayerUpdateMsg data) { GameObject avatar = Instantiate(clientAvatar); listOfClients[data.player.id] = avatar; avatar.GetComponentInChildren <TextMesh>().text = data.player.id; }
void UpdateClient(PlayerUpdateMsg data) { if (allClients.ContainsKey(data.player.id)) { allClients[data.player.id].cubPos = data.player.cubPos; } }
void OnData(DataStreamReader stream, int i, NetworkConnection client) { 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 position " + puMsg.player.pos); UpdatePlayerInfo(puMsg); break; case Commands.SERVER_UPDATE: ServerUpdateMsg ServerUpdateMessage = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server Update."); break; default: Debug.Log("ServerError (Default Log)"); break; } }
// on data received 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) { // player handshake case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); SpawnPlayer(hsMsg.player); Debug.Log("Handshake message received!"); break; // player update case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); ClientPlayerUpdate(puMsg.players); Debug.Log("Player update message received!"); break; // server update case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; // player list update case Commands.PLAYER_LIST: ClientListMsg listMsg = JsonUtility.FromJson <ClientListMsg>(recMsg); for (int p = 0; p < listMsg.players.Length; p++) { SpawnPlayer(listMsg.players[p]); } Debug.Log("Player list message received!"); break; // player disconnect case Commands.PLAYER_DISCONNECT: DiscPlayerMsg discMsg = JsonUtility.FromJson <DiscPlayerMsg>(recMsg); RemoveClientPlayer(discMsg.players); Debug.Log("Player disconnect message received!"); break; // player connect case Commands.PLAYER_JOIN: HandshakeMsg nhsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); ClientPlayerID = nhsMsg.player.id; SpawnPlayer(nhsMsg.player); break; default: Debug.Log("Unrecognized message received!"); break; } }
private 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("[Notice] Handshake message received!"); break; case Commands.PLAYER_UPDATE: //shouldnt happen Debug.LogError("[Error] Player update message received! Client shouldn't receive messages from clients."); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); //Debug.Log("[Routine] Server update message received!"); UpdateRemotePlayers(suMsg.players); break; case Commands.PONG: if (bVerboseDebug) { //Debug.Log("[Routine] Pong message received!"); } break; case Commands.SERVER_HANDSHAKE: ServerHandshakeMsg shMsg = JsonUtility.FromJson <ServerHandshakeMsg>(recMsg); Debug.Log("[Notice] Handshake from server received!"); m_clientIDDict.Add(shMsg.clientID, localClientCharacterRef); //Add local character to player dictionary m_thisClientID = shMsg.clientID; //keep a reference to local player ID SpawnRemotePlayers(shMsg.players); //Spawn remote players StartCoroutine(UploadClientDataRoutine(m_clientUpdateInterval)); //Start routinely updating server with local cube character data break; case Commands.NEW_PLAYER: PlayerUpdateMsg puMSg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("[Notice] A new player has connected. (" + puMSg.player.clientID + ")"); SpawnRemotePlayer(puMSg.player); break; case Commands.PLAYER_DISCONNECT: PlayerIDMsg pdMSg = JsonUtility.FromJson <PlayerIDMsg>(recMsg); Debug.Log("[Notice] A player has disconnected. (" + pdMSg.clientID + ")"); RemoveRemotePlayer(pdMSg.clientID); break; default: Debug.LogError("[Error] Unrecognized message received!"); break; } }
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; } }
public void SendPosition(Vector3 pos) { //Debug.Log("Update Player Position: " + pos); PlayerUpdateMsg m = new PlayerUpdateMsg(); m.player.pos = pos; SendToServer(JsonUtility.ToJson(m)); }
void SpawnNewPlayer(PlayerUpdateMsg puMsg) { GameObject cube = Instantiate(playerCube); connectedClientsDict[puMsg.player.id] = cube; cube.GetComponentInChildren <TextMeshProUGUI>().SetText(puMsg.player.id); cube.GetComponent <PlayerController>().isMe = false; // stops new cubes from being controlled by this client }
void UpdateClientInfo(PlayerUpdateMsg puMsg) { if (listOfClients.ContainsKey(puMsg.player.id)) { listOfClients[puMsg.player.id].id = puMsg.player.id; listOfClients[puMsg.player.id].pos = puMsg.player.pos; } }
// Sends curent player cube position to server void UpdateClientPlayer() { PlayerUpdateMsg playerUpdate = new PlayerUpdateMsg(); playerUpdate.player.id = clientPlayer.id; playerUpdate.player.pos = FindPlayerCube(clientPlayer).transform.position; SendToServer(JsonUtility.ToJson(playerUpdate)); }
void UpdatePlayersToClient() { PlayerUpdateMsg updatedPlayerMsg = new PlayerUpdateMsg(players); foreach (NetworkConnection c in m_Connections) { SendToClient(JsonUtility.ToJson(updatedPlayerMsg), c); } }
void ClientUpdate() { PlayerUpdateMsg playerUpdateMsg = new PlayerUpdateMsg(playerList); foreach (NetworkConnection c in m_Connections) { SendToClient(JsonUtility.ToJson(playerUpdateMsg), c); } }
void UpdateClientStats(PlayerUpdateMsg puMsg) //done { if (clientLookUpTable.ContainsKey(puMsg.player.id)) { clientLookUpTable[puMsg.player.id].id = puMsg.player.id; clientLookUpTable[puMsg.player.id].cubPos = puMsg.player.cubPos; clientLookUpTable[puMsg.player.id].heartBeat = Time.time; } }
void UpdatePlayerInfo(PlayerUpdateMsg puMsg) { if (ConnectedPlayers.ContainsKey(puMsg.player.id)) { ConnectedPlayers[puMsg.player.id].id = puMsg.player.id; ConnectedPlayers[puMsg.player.id].pos = puMsg.player.pos; ConnectedPlayers[puMsg.player.id].isDropped = puMsg.player.isDropped; } }
void OnData(DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); //Get bytes string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); //convert bytes to JSON string NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); //convert JSON to c# class switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("Handshake message received!"); break; case Commands.PLAYER_WHOAMI: PlayerUpdateMsg internalId = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Got internalId from server"); playerInfo.player.id = internalId.player.id; playerIDText.SetText(playerInfo.player.id); myIDLabel.SetText(playerInfo.player.id); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Got data from server, player Pos: " + puMsg.player.pos); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); UpdateClientsInfo(suMsg); break; case Commands.ALREADY_HERE_PLAYERS: ServerUpdateMsg alreadyHerePlayerInfo = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("existed player info received!"); SpawnPlayersAlreadyHere(alreadyHerePlayerInfo); break; case Commands.SPAWN_NEW_PLAYER: PlayerUpdateMsg newPlayerMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("new client info received!"); SpawnNewPlayer(newPlayerMsg); break; case Commands.DISCONNECTED_PLAYER: DisconnectedPlayersMsg dpMsg = JsonUtility.FromJson <DisconnectedPlayersMsg>(recMsg); Debug.Log("Player disconnected :("); DeleteDisconnectPlayer(dpMsg); break; default: Debug.Log("Unrecognized message received!"); break; } }
void UpdateClientInfo(PlayerUpdateMsg puMsg) { if (connectedClientsDict.ContainsKey(puMsg.player.id)) { connectedClientsDict[puMsg.player.id].id = puMsg.player.id; connectedClientsDict[puMsg.player.id].pos = puMsg.player.pos; connectedClientsDict[puMsg.player.id].isDead = puMsg.player.isDead; } }