public void SendProjectileFireMsg(string internalID) { Debug.Log("Sending Projectile Fire Message to Server"); ProjectileFireMsg projectileFireMsg = new ProjectileFireMsg(); projectileFireMsg.projectileOwnerID = internalID; SendToServer(JsonUtility.ToJson(projectileFireMsg)); }
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!"); SpawnClientOwnedPlayer(hsMsg); break; case Commands.PLAYER_HIT: PlayerHitMsg playerHitMsg = JsonUtility.FromJson <PlayerHitMsg>(recMsg); DealDamageToClient(playerHitMsg.playerInternalID); Debug.Log("Player Hit message received: " + playerHitMsg.playerInternalID); break; case Commands.PROJECTILE_FIRE: ProjectileFireMsg projectileFireMsg = JsonUtility.FromJson <ProjectileFireMsg>(recMsg); FireProjectileFromClient(projectileFireMsg.projectileOwnerID); Debug.Log("Player Fire message received from: " + projectileFireMsg.projectileOwnerID); break; case Commands.PLAYER_INTERNALID: PlayerInternalIDMsg internalIDMsg = JsonUtility.FromJson <PlayerInternalIDMsg>(recMsg); playerInternalID = internalIDMsg.playerInternalID; Debug.Log("Got internalId from server : " + playerInternalID); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Player update message received!"); Debug.Log("Got data from server, player ID: " + puMsg.player.internalID); 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; } }