3

Using Unity, C# and Photon (PUN2), I have the user already connected to a room, but now want to (without that user leaving the room) retrieve a list of currently available other Photon rooms, including their current online user count (which can normally only be done pre- or post-room-join while in the lobby). Photon support tells me I need to create a second client using LoadBalancingClient for that, but I don't know exactly how. What would be the code skeleton for the basic Connect - PollAvailableRoomsAndOnlineCounts - Disconnect class and procedure? Thanks!

Philipp Lenssen
  • 8,818
  • 13
  • 56
  • 77

1 Answers1

1

Found the answer in the meantime. Here's the skeleton code of my helper class PhotonRoomPoller.cs:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using ExitGames.Client.Photon;

public class PhotonRoomPoller : MonoBehaviourPunCallbacks
{
    // Creates a second Photon peer to poll online room counts info.
    // A second peer is necessary as one otherwise while in a Room can't join
    // the Lobby, needed to get the room list. API at
    // doc-api.photonengine.com/en/pun/v2/class_photon_1_1_realtime_1_1_load_balancing_client.html

    Action<List<RoomInfo>> callback = null;
    LoadBalancingClient client = null;

    public void GetRoomsInfo(Action<List<RoomInfo>> callback)
    {
        this.callback = callback;

        client = new LoadBalancingClient();
        client.AddCallbackTarget(this);
        client.StateChanged += OnStateChanged;
        client.AppId = PhotonNetwork.PhotonServerSettings.AppSettings.AppIdRealtime;
        client.AppVersion = PhotonNetwork.NetworkingClient.AppVersion;
        client.ConnectToRegionMaster("us");
    }

    void Update()
    {
        if (client != null)
        {
            client.Service();
        }
    }

    void OnStateChanged(ClientState previousState, ClientState state)
    {
        // Debug.Log(state);
        if (state == ClientState.ConnectedToMaster)
        {
            client.OpJoinLobby(null);
        }
    }

    public override void OnRoomListUpdate(List<RoomInfo> infos)
    {
        if (callback != null)
        {
            callback(infos);
        }
        client.Disconnect();
    }

}

Usage, e.g.:

PhotonRoomPoller roomPoller = gameObject.AddComponent<PhotonRoomPoller>();
roomPoller.GetRoomsInfo
(
    (roomInfos) =>
    {
        AddContent(roomInfos);
        Destroy(roomPoller);
    }
);

Special care needs to be given to match all needed settings (though a lot can also remain null or undefined, e.g. gameVersion).

Philipp Lenssen
  • 8,818
  • 13
  • 56
  • 77
  • This line is key `client.AppVersion = PhotonNetwork.NetworkingClient.AppVersion`. I had basically the same implementation, but had this line instead `client.AppVersion = PhotonNetwork.PhotonServerSettings.AppSettings.AppVersion`, and it didn't work. That was because Photon appends the library version string to the app version, but it does it internally without documenting this behaviour. It's even better to do it explicitly, to make clear what is going on: `client.AppVersion = PhotonNetwork.PhotonServerSettings.AppSettings.AppVersion + "_" + PhotonNetwork.PunVersion`. – maff Mar 13 '20 at 10:17