コード例 #1
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:     //Will be used for sending every other player data
            HandshakeMsg hs_Msg = JsonUtility.FromJson <HandshakeMsg>(recMsg);
            Debug.Log("Handshake message received!");
            foreach (NetworkObjects.NetworkPlayer player in hs_Msg.player)
            {
                newPlayers.Add(player.id);
            }
            break;

        case Commands.GAME_UPDATE:
            latestGameState = JsonUtility.FromJson <GameUpdateMsg>(recMsg);
            Debug.Log("Game Update message received!");
            break;

        //case Commands.PLAYER_UPDATE://Never used player update probably could've but decided to make my own commands
        //    PlayerUpdateMsg pu_Msg = JsonUtility.FromJson<PlayerUpdateMsg>(recMsg);
        //    Debug.Log("Player update message received!");
        //    break;
        case Commands.CONNECTION_ACCEPTED:     //This will be used for your own data
            ConnectionAcceptedMsg ca_Msg = JsonUtility.FromJson <ConnectionAcceptedMsg>(recMsg);
            Debug.Log("Connection Approved message received!");
            foreach (NetworkObjects.NetworkPlayer playerData in ca_Msg.player)
            {
                Debug.Log("Position X: " + playerData.cubPos.x + " Position Y: " + playerData.cubPos.y + " Position Z: " + playerData.cubPos.z);
                newPlayers.Add(playerData.id);
                myAddress = playerData.id;
            }
            break;

        case Commands.SERVER_UPDATE:    //Sets the player list
            initialSetofPlayers = JsonUtility.FromJson <ServerUpdateMsg>(recMsg);
            Debug.Log("Server update message received!");
            //Debug.Log("PlayerList: " + initialSetofPlayers.players[0].id);
            break;

        default:
            Debug.Log("Unrecognized message received!");
            break;
        }
    }
コード例 #2
0
    void OnConnect(NetworkConnection c)
    {
        m_Connections.Add(c);
        Debug.Log("Accepted a connection");

        //ServerUpdateMsg s_UpdateMessage = new ServerUpdateMsg();
        //for (int i = 0; i < 1; i++)
        //{

        //    tempID.id = c.InternalId.ToString();
        //    s_UpdateMessage.players.Add(tempID);
        //}

        // NetworkObjects.NetworkPlayer fakeID = new NetworkObjects.NetworkPlayer();


        NetworkObjects.NetworkPlayer realId = new NetworkObjects.NetworkPlayer();
        //Setting the players ID. E.g. Player 1 ID = 0
        realId.id = c.InternalId.ToString();
        //realId.cubeColor = playerData.player.cubeColor;
        realId.cubPos = playerInfo.player.cubPos;
        //Adding player ID to the player list
        playerList.GameUpdate.Add(realId);
        //Sending it to the client for a updated list
        SendToClient(JsonUtility.ToJson(playerList), c);

        //Sending the player their own internalID so they have a reference
        ConnectionAcceptedMsg ca_Msg = new ConnectionAcceptedMsg();

        NetworkObjects.NetworkPlayer connectID = new NetworkObjects.NetworkPlayer();
        connectID.id = c.InternalId.ToString();
        ca_Msg.player.Add(connectID);
        SendToClient(JsonUtility.ToJson(ca_Msg), c);

        //Sending the player a list of currently connected users
        ServerUpdateMsg suM = new ServerUpdateMsg();

        //Looping through all currently connected users
        //fakeId was was just for fun
        for (int i = 0; i < m_Connections.Length; i++)
        {
            NetworkObjects.NetworkPlayer fakeId = new NetworkObjects.NetworkPlayer();
            fakeId.id = playerList.GameUpdate[i].id;
            suM.players.Add(fakeId);
        }
        SendToClient(JsonUtility.ToJson(suM), c);

        // This is essentially a player joined message so now every client is aware of the new one who joined
        HandshakeMsg hs_Msg = new HandshakeMsg();

        for (int i = 0; i < m_Connections.Length; i++)
        {
            if (c.InternalId.ToString() != playerList.GameUpdate[i].id)
            {
                realId.id = c.InternalId.ToString();
                hs_Msg.player.Add(realId);
                SendToClient(JsonUtility.ToJson(hs_Msg), m_Connections[i]);
            }
        }

        //This is some stuff I had before but i've upgraded the code

        //Debug.Log("Got This -> " + s_UpdateMessage.players[0].id);
        //suM.playerlist.players = c.InternalId.ToString();

        //SendToClient(JsonUtility.ToJson(s_UpdateMessage), c);

        ////// Example to send a handshake message:
        //HandshakeMsg m = new HandshakeMsg();
        //m.player.id = c.InternalId.ToString();
        //SendToClient(JsonUtility.ToJson(m), c);
        ////m.player.cubeColor.r = 5;
        ////m.player.cubeColor.g = 5;
        ////m.player.cubeColor.b = 5;
    }