public void RequestMyID() //asks the { IDUpdateMsg m = new IDUpdateMsg(); m.id = 0; SendToServer(JsonUtility.ToJson(m)); }
void OnData(DataStreamReader stream) { //these guys are converting unreadable data to readable and the final version will be called recMsg 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.ID_UPDATE: IDUpdateMsg idMsg = JsonUtility.FromJson <IDUpdateMsg>(recMsg); myPlayer.id = idMsg.id; Debug.Log("Client: Server returned me my new id: " + myPlayer.id.ToString()); break; case Commands.HANDSHAKE: HandshakeMsg m = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("(Server) Hello, player " + myPlayer.id.ToString()); break; default: Debug.Log("Unrecognized message received!"); break; } }
void OnConnect(NetworkConnection c) { m_Connections.Add(c); Debug.Log("Accepted a connection"); IDUpdateMsg m = new IDUpdateMsg(); m.id = c.InternalId; SendToClient(JsonUtility.ToJson(m), c); Debug.Log("Server: Hello, client this your id: " + c.InternalId.ToString()); }
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); m_players = suMsg.players; for (int i = 0; i < m_players.Count; i++) { Debug.Log("(player " + m_players[i].id + ") " + "position: " + m_players[i].body.transform.position); } Debug.Log("Server update message received!"); break; case Commands.ID_UPDATE: IDUpdateMsg idMsg = JsonUtility.FromJson <IDUpdateMsg>(recMsg); player.id = idMsg.id; Debug.Log("Client: Server returned me my new id: " + player.id.ToString()); break; default: Debug.Log("Unrecognized message received!"); break; } }