示例#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); // Json string; m (json dump) that was sent by server

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

        latestMessage = JsonUtility.FromJson <Message>(returnData);
        try{
            switch (latestMessage.cmd)
            {
            case commands.NEW_CLIENT:
                newPlayer = JsonUtility.FromJson <NewPlayer>(returnData);
                JsonUtility.FromJson <NewPlayer>(returnData);
                break;

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

            case commands.DROPPED_CLIENT:
                latestDroppedPlayers = JsonUtility.FromJson <DroppedPlayers>(returnData);
                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);
    }
示例#2
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);

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

        latestMessage = JsonUtility.FromJson <Message>(returnData);
        try{
            switch (latestMessage.cmd)                                              // Work out what kind of command the server sent - what does it want from us?
            {
            case commands.NEW_CLIENT:                                               // A new client is joining!
                NewPlayer newPlayer = JsonUtility.FromJson <NewPlayer>(returnData); // Make a NewPlayer object for this new player so we can make them a nice cube.
                Debug.Log(returnData);
                playersToSpawn.Add(newPlayer.player.id);                            // If I'M the new client, I should be the first thing I spawn
                if (myAddress == "")                                                // So my address won't yet be set
                {
                    myAddress = newPlayer.player.id;                                // So set it - we'll use this later to avoid any tomfoolery
                }
                break;

            case commands.UPDATE:                                               // The server is just sending an update - this is where all the information about the OTHER cubes in our scene,
                // not us, is sent to us, so we can put their cubes in the right place.
                latestGameState = JsonUtility.FromJson <GameState>(returnData); // latestGameState stores all the current players and their positions, so update that.
                UpdatePlayers();                                                // Move all the cubes around!
                Debug.Log(returnData);
                break;

            case commands.DROPPED_CLIENT:                                                         // The server is telling us that someone has left the cube party.
                DroppedPlayers droppedPlayer = JsonUtility.FromJson <DroppedPlayers>(returnData); // Get the ID of the dropped player
                DestroyPlayers(droppedPlayer.id);                                                 // Get rid of their cube.
                Debug.Log(returnData);
                break;

            case commands.ALREADY_HERE_PLAYERS:                                                                      // This command should only come to the newly connected client - it's the server helpfully telling us who
                // is already here, so we can spawn their cubes.
                AlreadyHerePlayerList alreadyHerePlayers = JsonUtility.FromJson <AlreadyHerePlayerList>(returnData); // Populate the list.
                foreach (Player player in alreadyHerePlayers.players)
                {
                    playersToSpawn.Add(player.id);     // Spawn all the other cubes!
                }
                Debug.Log(returnData);
                break;

            default:
                Debug.Log("Error");     // Something went wrong.
                break;
            }
        }
        catch (Exception e) {
            Debug.Log(e.ToString()); // Something went really wrong.
        }

        // schedule the next receive operation once reading is done:
        socket.BeginReceive(new AsyncCallback(OnReceived), socket);
    }