Exemple #1
0
    /*
     * Joins an already created room. If the room does not exist it is created and then the client joins the newly created room.
     * Username is the clients name, battleName is the name of the room to join, and stateHandler is a callback function that gets
     * invoked on a state change.
     */
    public async void JoinOrCreateRoom(string username, float playerHealth, string battleName, Colyseus.Room <State> .RoomOnStateChangeEventHandler stateHandler, Colyseus.Room <State> .RoomOnMessageEventHandler messageHandler)
    {
        // Joins/sets up the room.
        Debug.LogFormat("{0} is trying to join room: {1}", username, battleName);
        room = await client.JoinOrCreate <State>(roomName, new Dictionary <string, object>() { { "name", username }, { "playerHealth", playerHealth }, { "battleName", battleName } });

        Debug.LogFormat("Room Id: {0}\tSession Id: {1}", room.Id, room.SessionId);

        // Sets event callback functions.
        room.OnStateChange += stateHandler; // look at OnStateChangeHandler below as an example.
        room.OnMessage     += messageHandler;
        room.OnLeave       += (code) => Debug.LogFormat("Left Room with Code: {0}", code);
        room.OnError       += (message) => Debug.LogErrorFormat("Colyseus Error: {0}", message);
    }