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.PLAYER_INIT:
            InitializeConnectionMsg piMsg = JsonUtility.FromJson <InitializeConnectionMsg>(recMsg);

            playerUpdateMsg.player.id = piMsg.yourID;

            HandshakeMsg m = new HandshakeMsg();
            m.player    = playerUpdateMsg.player;
            m.player.id = piMsg.yourID;
            SendToServer(JsonUtility.ToJson(m));

            otherPlayers.Add(m.player.id, yourCharacter);

            Debug.Log("Initialization message received!  Your ID: " + piMsg.yourID);
            break;

        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);
            UpdateNetworkObjects(suMsg);
            Debug.Log("Server update message received!");
            break;

        case Commands.PLAYER_DROPPED:
            ConnectionDroppedMsg cdMsg = JsonUtility.FromJson <ConnectionDroppedMsg>(recMsg);

            // Destroy
            GameObject character = otherPlayers[cdMsg.droppedId];
            otherPlayers.Remove(cdMsg.droppedId);
            Destroy(character);

            Debug.Log("Player dropped message received! " + cdMsg.droppedId);
            break;

        default:
            Debug.Log("Unrecognized message received!");
            break;
        }
    }
    void OnConnect(NetworkConnection c)
    {
        m_Connections.Add(c);
        var newPlayersId  = c.InternalId;
        var connectionMsg = new InitializeConnectionMsg();

        connectionMsg.yourID = newPlayersId.ToString();

        SendToClient(JsonUtility.ToJson(connectionMsg), c);

        Debug.Log("Accepted a connection");

        //// Example to send a handshake message:
        // HandshakeMsg m = new HandshakeMsg();
        // m.player.id = c.InternalId.ToString();
        // SendToClient(JsonUtility.ToJson(m),c);
    }
Exemple #3
0
    void OnConnect(NetworkConnection c)
    {
        m_Connections.Add(c);
        Debug.Log("Accepted a connection");

        NetworkObjects.NetworkPlayer p = new NetworkObjects.NetworkPlayer();
        p.id = c.InternalId.ToString();
        clientList.Add(p);

        NetworkCube nCube = new NetworkCube(c.InternalId.ToString());

        nCube.cube = GameObject.Instantiate(cubePrefab, p.cubePos, Quaternion.identity);
        nCube.cube.GetComponent <CubeController>().id           = c.InternalId;
        nCube.cube.GetComponent <MeshRenderer>().material.color = p.cubeColor;
        cubeList.Add(nCube);

        InitializeConnectionMsg icMsg = new InitializeConnectionMsg();

        icMsg.connectionID = c.InternalId.ToString();
        SendToClient(JsonUtility.ToJson(icMsg), c);

        ServerUpdateMsg sMsg = new ServerUpdateMsg();

        foreach (NetworkObjects.NetworkPlayer np in clientList)
        {
            sMsg.players.Add(np);
        }

        for (int i = 0; i < m_Connections.Length; i++)
        {
            if (!m_Connections[i].IsCreated)
            {
                continue;
            }

            SendToClient(JsonUtility.ToJson(sMsg), m_Connections[i]);
        }
    }
    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);
            Debug.Log("Server update message received!");

            if (playerList.Count == 0)
            {
                playerList = suMsg.players;
                foreach (NetworkObjects.NetworkPlayer p in playerList)
                {
                    CreateCube(p);
                }
            }
            else
            {
                foreach (NetworkObjects.NetworkPlayer npServer in suMsg.players)
                {
                    int ind = GetIndex(npServer.id);

                    if (ind >= playerList.Count)
                    {
                        playerList.Add(npServer);
                        CreateCube(npServer);
                    }
                    else
                    {
                        playerList[ind] = npServer;
                        cubeList[ind].cube.transform.position = npServer.cubePos;
                    }
                }
            }
            break;

        case Commands.INITIALIZE:
            InitializeConnectionMsg icMsg = JsonUtility.FromJson <InitializeConnectionMsg>(recMsg);
            connectedID = int.Parse(icMsg.connectionID);
            Debug.Log("Initialize Connection message received!");
            Debug.Log("Connected ID of " + connectedID);
            break;

        case Commands.PLAYER_DISCONNECT:
            PlayerDisconnectMsg pdMsg = JsonUtility.FromJson <PlayerDisconnectMsg>(recMsg);
            Debug.Log("Player disconnect message received!");

            int index = GetIndex(pdMsg.connectionID);

            playerList.RemoveAt(index);
            Destroy(cubeList[index].cube);
            cubeList.RemoveAt(index);
            break;

        default:
            Debug.Log("Unrecognized message received!");
            break;
        }
    }
Exemple #5
0
    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!");

            int index = 0;
            foreach (NetworkObjects.NetworkPlayer p in clientList)
            {
                if (puMsg.player.id == p.id)
                {
                    break;
                }
                index++;
            }
            clientList[index].cubePos = puMsg.player.cubePos;
            cubeList[index].cube.transform.position = puMsg.player.cubePos;
            Debug.Log("New Cube Position: " + clientList[index].cubePos);

            ServerUpdateMsg sMsg = new ServerUpdateMsg();
            sMsg.players = clientList;
            for (int j = 0; j < m_Connections.Length; j++)
            {
                if (!m_Connections[j].IsCreated)
                {
                    continue;
                }

                SendToClient(JsonUtility.ToJson(sMsg), m_Connections[j]);
            }
            break;

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

        case Commands.INITIALIZE:
            InitializeConnectionMsg icMsg = JsonUtility.FromJson <InitializeConnectionMsg>(recMsg);
            Debug.Log("Initialize Connection message received!");
            break;

        case Commands.PLAYER_DISCONNECT:
            PlayerDisconnectMsg pdMsg = new PlayerDisconnectMsg();
            Debug.Log("Player disconnect message received!");
            break;

        default:
            Debug.Log("SERVER ERROR: Unrecognized message received!");
            break;
        }
    }