void OnConnect(NetworkConnection c)
    {
        m_Connections.Add(c);
        Debug.Log("Accepted a connection");
        Debug.Log("Added player:" + c.InternalId.ToString());
        // Example to send a handshake message:
        HandshakeMsg m = new HandshakeMsg();

        m.player.id = c.InternalId.ToString();
        //give the player a random starting location
        m.player.cubPos = new Vector3(UnityEngine.Random.Range(-5, 5), UnityEngine.Random.Range(-5, 5), 0);
        //add the new player to our list of connected players
        connectedPlayers.Add(m.player);
        SendToClient(JsonUtility.ToJson(m), c);

        //send the new player all the other players and the other players the new player
        foreach (NetworkConnection connection in m_Connections)
        {
            if (connection.IsCreated)
            {
                Debug.Log("Sending connected players to player:" + connection.InternalId.ToString());
                NewPlayerUpdateMsg n = new NewPlayerUpdateMsg();
                n.players = new List <NetworkObjects.NetworkPlayer>(connectedPlayers);
                SendToClient(JsonUtility.ToJson(n), connection);
            }
        }
    }
Exemple #2
0
    //IEnumerator SendRepeatedHandshake()
    //{
    //    while (true)
    //    {
    //        yield return new WaitForSeconds(2);
    //        Debug.Log("Sending Handshake");
    //        HandshakeMsg m = new HandshakeMsg();
    //        m.player.id = m_Connection.InternalId.ToString();
    //        SendToServer(JsonUtility.ToJson(m));

    //    }
    //}



    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!");
            //add our own id so we know who we are
            if (myId == "")
            {
                myId = hsMsg.player.id;
                Debug.Log("My id is:" + myId);
            }
            connectedPlayers.Add(hsMsg.player);
            Debug.Log("PLayer spawned status: " + hsMsg.player.spawned);

            break;

        case Commands.PLAYER_UPDATE:
            //not really receiveing player update messages as this is this client and not the server
            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!");
            for (int i = 0; i < suMsg.players.Count; i++)
            {
                foreach (NetworkObjects.NetworkPlayer player in connectedPlayers)
                {
                    if (player.id == suMsg.players[i].id)    //get the matching player from the server and out player list
                    {
                        //update current player list positoins
                        player.cubPos = suMsg.players[i].cubPos;
                    }
                }
            }
            break;

        case Commands.NEWPLAYER_UPDATE:
            NewPlayerUpdateMsg npMsg = JsonUtility.FromJson <NewPlayerUpdateMsg>(recMsg);
            for (int i = 0; i < npMsg.players.Count; i++)
            {
                //check if there are any new players that were added
                bool playerFound = false;
                foreach (NetworkObjects.NetworkPlayer player in connectedPlayers)
                {
                    if (npMsg.players[i].id == player.id)
                    {
                        playerFound = true;
                        Debug.Log("already have the player");
                    }
                }
                if (!playerFound)     // the player in the latest game state is new and we need to add them
                {
                    connectedPlayers.Add(npMsg.players[i]);

                    Debug.Log("Added other player to conencted players");
                }
            }
            break;

        case Commands.DROPPED_UPDATE:
            DroppedUpdateMsg dpMsg = JsonUtility.FromJson <DroppedUpdateMsg>(recMsg);
            foreach (NetworkObjects.NetworkPlayer player in connectedPlayers)
            {
                if (player.id == dpMsg.player.id)
                {
                    Debug.Log("found dropped player");
                    droppedPlayers.Add(player);
                }
            }
            break;

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