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!");
            break;

        case Commands.SERVER_UPDATE:
            ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg);
            Debug.Log("Server update message received!");
            break;

        case Commands.PLAYER_JOIN:
            PlayerJoinMsg pjMsg = JsonUtility.FromJson <PlayerJoinMsg>(recMsg);
            Debug.Log("Player join message received!");
            playerJoinMsgs.Add(pjMsg);
            foreach (NetworkConnection cp in m_Connections)
            {
                SendToClient(JsonUtility.ToJson(pjMsg), cp);
            }
            break;

        case Commands.PLAYER_MOVEMONT:
            PlayerMovementMsg pmMsg = JsonUtility.FromJson <PlayerMovementMsg>(recMsg);
            Debug.Log("Player movement update message received!");
            foreach (NetworkConnection cp in m_Connections)
            {
                SendToClient(JsonUtility.ToJson(pmMsg), cp);
            }
            break;

        default:
            Debug.Log("SERVER ERROR: Unrecognized message received!");
            break;
        }
    }
示例#2
0
    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.PLAYER_JOIN:
            PlayerJoinMsg pjMsg = JsonUtility.FromJson <PlayerJoinMsg>(recMsg);
            if (UID != pjMsg.uniqueid)
            {
                Instantiate(playerPrefab);
            }
            Debug.Log("Player join message received!");
            break;

        case Commands.PLAYER_MOVEMONT:
            PlayerMovementMsg pmMsg = JsonUtility.FromJson <PlayerMovementMsg>(recMsg);
            Debug.Log("Player movement update message received!");
            foreach (GameObject p in GameObject.FindGameObjectsWithTag("Player"))
            {
                if (p != myself)
                {
                    p.transform.position = pmMsg.PlayerPos;
                }
            }
            break;

        default:
            Debug.Log("Unrecognized message received!");
            break;
        }
    }
示例#3
0
    void OnConnect()
    {
        Debug.Log("We are now connected to the server");
        GameObject playerTest = Instantiate(playerPrefab);

        playerTest.AddComponent <PlayerMovement>();
        PlayerJoinMsg m = new PlayerJoinMsg();

        m.PlayerPos = playerTest.transform.position;
        playerTest.GetComponent <PlayerID>().ID = UID;
        myself     = playerTest;
        m.uniqueid = UID;
        SendToServer(JsonUtility.ToJson(m));
        //// Example to send a handshake message:
        // HandshakeMsg m = new HandshakeMsg();
        // m.player.id = m_Connection.InternalId.ToString();
        // SendToServer(JsonUtility.ToJson(m));
    }