Example #1
0
    void OnReceived(IAsyncResult result)
    {
        // this is what had been passed into BeginReceive as the second parameter:
        UdpClient socket = result.AsyncState as UdpClient;

        // points towards whoever had sent the message:
        IPEndPoint source = new IPEndPoint(0, 0);

        // get the actual message and fill out the source:
        byte[] message = socket.EndReceive(result, ref source);

        // do what you'd like with `message` here:
        string returnData = Encoding.ASCII.GetString(message);

        IncomingMessage = "Got this" + returnData;
        //Debug.Log("Got this: " + returnData);

        latestMessage = JsonUtility.FromJson <Message>(returnData);
        try{
            switch (latestMessage.cmd)
            {
            case commands.NEW_CLIENT:
                NewPlayerArrived();
                NewPlayer p = JsonUtility.FromJson <NewPlayer>(returnData);
                if (myID == "newID")
                {
                    myID = p.player.id;
                }
                PlayersToSpawnList.Add(p.player.id);
                break;

            case commands.UPDATE:    //When we receive this command from server, update all our cubes.
                latestGameState = JsonUtility.FromJson <GameState>(returnData);

                break;

            case commands.CLIENT_REMOVED:                                           // Someone dropped from the server.
                DroppedPlayer d = JsonUtility.FromJson <DroppedPlayer>(returnData); //Store that player.
                Debug.Log("Player Left The Game:");
                PlayerRemoved();
                PlayersToDestroyList.Add(d.id);    //Destroy that player by its id.;
                break;

            case commands.GET_PLAYERS_IN_GAME:
                //When a new player connects, get/make an object(list) of all players already in the game.
                PlayersAlreadyInGame pigl = JsonUtility.FromJson <PlayersAlreadyInGame>(returnData);
                //we need to loop through this list and add them to the spawn list.
                for (int i = 0; i < pigl.players.Length; i++)
                {
                    if (pigl.players[i].id != myID)
                    {
                        PlayersToSpawnList.Add(pigl.players[i].id);
                    }
                }

                break;

            default:
                Debug.Log("Error");
                break;
            }
        }
        catch (Exception e) {
            Debug.Log(e.ToString());
        }

        // schedule the next receive operation once reading is done:
        socket.BeginReceive(new AsyncCallback(OnReceived), socket);
    }
    void OnReceived(IAsyncResult result)
    {
        UdpClient  socket = result.AsyncState as UdpClient;
        IPEndPoint source = new IPEndPoint(0, 0);

        byte[] message    = socket.EndReceive(result, ref source);
        string returnData = Encoding.ASCII.GetString(message);

        IncomingMessage = "Got this" + returnData;

        latestMessage = JsonUtility.FromJson <Message>(returnData);

        try
        {
            switch (latestMessage.cmd)
            {
            case commands.NEW_PLAYER:
                Debug.Log("New Player Arrived");
                Debug.Log(IncomingMessage);
                NewPlayer p = JsonUtility.FromJson <NewPlayer>(returnData);
                if (myID == null)
                {
                    myID = p.player.id;
                }
                SpawnList.Add(p.player.id);
                break;

            case commands.UPDATE:
                latestGameState = JsonUtility.FromJson <GameState>(returnData);
                break;

            case commands.PLAYER_REMOVED:
                DroppedPlayer d = JsonUtility.FromJson <DroppedPlayer>(returnData);
                Debug.Log("Player Left The Game:");
                Debug.Log(IncomingMessage);
                DestroyList.Add(d.id);
                break;

            case commands.ADD_PLAYERS:
                PlayersAlreadyInGame pigl = JsonUtility.FromJson <PlayersAlreadyInGame>(returnData);
                for (int i = 0; i < pigl.players.Length; i++)
                {
                    if (pigl.players[i].id != myID)
                    {
                        SpawnList.Add(pigl.players[i].id);
                    }
                }
                break;

            default:
                Debug.Log("Error");
                break;
            }
        }
        catch (Exception e)
        {
            Debug.Log(e.ToString());
        }

        socket.BeginReceive(new AsyncCallback(OnReceived), socket);
    }