Publish Subscriber [PhotonChat within Pun] is not set as intended

At first, I set the ChannelCreationOptions for a private Lobby (Party).
private void Start()
        {
            m_PrivateLobby = new ChannelCreationOptions();
            m_PrivateLobby.MaxSubscribers = 3;
            m_PrivateLobby.PublishSubscribers = true;
        }

Then I join a lobby with these ChannelCreationOptions
public static void JoinLobby(string _lobbyName)
        {
                ChatManager.ChatClient.Subscribe(_lobbyName, 0, -1, m_PrivateLobby);
                m_CurrentLobbyName = _lobbyName;
        }

Within the OnSubscribes function, I now want to get all the Subscribed users (Testing purpose)
public void OnSubscribed(string[] channels, bool[] results)
        {
            ChatManager.ChatClient.TryGetChannel(m_CurrentLobbyName, out ChatChannel _currentChannel);
            string[] currentLobbyUser = new string[_currentChannel.Subscribers.Count];
            _currentChannel.Subscribers.CopyTo(currentLobbyUser);
            foreach (string user in currentLobbyUser)
            {
                Debug.Log(user);
            }

The result is: There is no User within the _currentChannel.Subscribers.

While Debugging I realized that also the ChatChannel (_currentChannel) I got within the OnSubscribed Method holds the variable PublishSubscribers with the value of false.

Have I done something wrong is there an error on Photon Side or am I misunderstanding something?

Comments

  • JohnTubeJohnTube mod
    edited February 11
    Hi @WurstOnAir,

    Thank you for choosing Photon!

    Make sure a channel with the same name was not "created" before and does not exist currently on the servers. If a channel is created with some options/properties, those cannot be changed.
    So maybe a channel with the same name was created with PublishSubscribers set to false (default).

    Second thing, make sure the callback is for the same channel you are subscribing to.

    Third thing is, make sure channel subscription is successful:
    public void OnSubscribed(string[] channels, bool[] results)
    {
               if (channels.Length == 1) // should be 1
               {
                     if (_lobbyName.Equals(channels[0])) // the channel we're looking for
                     {
                           if (results[0]) // subscription successful
                           {
    

    Otherwise, what PUN version you are using?
    What Photon Cloud region are you connecting to?
  • edited February 11
    For the if created before already: This is the only function which is creating Lobbys (Subscribing clients to a Chat room)

    Full Script PartyManager:
    public class PartyManager : MonoBehaviour
        {
            private static ChannelCreationOptions m_PrivateLobby;
    
            private static string m_CurrentLobbyName;
    
            private void Start()
            {
                m_PrivateLobby = new ChannelCreationOptions();
                m_PrivateLobby.MaxSubscribers = 3;
                m_PrivateLobby.PublishSubscribers = true;
            }
    
            public static void JoinLobby(string _lobbyName)
            {
                if (RemoveCurrentLobby(_lobbyName))
                {
                    ChatManager.ChatClient.Subscribe(_lobbyName, 0, -1, m_PrivateLobby);
                    m_CurrentLobbyName = _lobbyName;
                }
            }
    
            private static bool RemoveCurrentLobby(string _newLobby)
            {
                if (m_CurrentLobbyName == _newLobby)
                {
                    return false;
                }
                else if (m_CurrentLobbyName != null)
                {
                    if (ChatManager.ChatClient.TryGetChannel(GameManager.UserID.ToString(), out ChatChannel _currentChannel))
                    {
                        string[] currentSubscribers = new string[_currentChannel.Subscribers.Count];
                        _currentChannel.Subscribers.CopyTo(currentSubscribers);
                        string newPartyLeader = null;
                        for (int i = 0; i < currentSubscribers.Length; i++)
                        {
                            if (currentSubscribers[i] != GameManager.UserID.ToString())
                            {
                                if (newPartyLeader == null)
                                {
                                    newPartyLeader = currentSubscribers[i];
                                }
                                ChatManager.ChatClient.SendPrivateMessage(currentSubscribers[i], $"/switchLobby {newPartyLeader}");
                            }
                        }
                    }
                    ChatManager.ChatClient.Unsubscribe(new string[] { m_CurrentLobbyName });
                }
                return true;
            }
    
            public static string[] GetCurrentLobbyUser(string _newlySubscribedChannel)
            {
                if (m_CurrentLobbyName == _newlySubscribedChannel)
                {
                    ChatManager.ChatClient.TryGetChannel(m_CurrentLobbyName, out ChatChannel _currentChannel);
                    string[] currentLobbyUser = new string[_currentChannel.Subscribers.Count];
                    _currentChannel.Subscribers.CopyTo(currentLobbyUser);
                    return currentLobbyUser;
                }
                return new string[0];
            }
        }
    

    Important parts of the ChatManager:
            public static void SwitchPartyLeader(string _newLeader)
            {
                PartyManager.JoinLobby(_newLeader);
            }
    
            public static void AcceptPartyInvite(string _lobbyToJoin)
            {
                PartyManager.JoinLobby(_lobbyToJoin);
            }
    
            public void OnPrivateMessage(string sender, object message, string channelName)
            {
                if (sender == GameManager.UserID.ToString())
                {
                    return;
                }
    
                string[] tmp;
                if (message.ToString().Contains(" "))
                {
                    tmp = message.ToString().Split(' ');
                }
                else
                {
                    tmp = new string[1] { message.ToString() };
                }
    
                // Some stuff releated to other topics
    
                else if (tmp[0] == "/invite")
                {
                    AcceptPartyInvite(tmp[1]);
                    Debug.Log("Got invite from " + tmp[1]);
                    // TODO: Open PartyInviteDialog from PartyManager
                }
                else if (tmp[0] == "/switchLobby")
                {
                    SwitchPartyLeader(tmp[1]);
                }
            }
    
            public void OnSubscribed(string[] channels, bool[] results)
            {
                if (channels.Length == 1) // should be 1
                {
                    if (results[0]) // subscription successful
                    {
                        foreach (string channel in channels)
                        {
                            Debug.Log(channel);
                        }
    
                        foreach (string user in PartyManager.GetCurrentLobbyUser(channels[0]))
                        {
                            Debug.Log(user);
                        }
                    }
                }
            }
    

    I am still getting the same behavior. @JohnTube

    Pun:2.16 Photon lib: 4.1.2.19
    currently best region= 'eu'
  • Oh this could also be important to know:
    public static void InviteFriend(int _id)
            {
                string tmp = GameManager.UserID.ToString();
                PartyManager.JoinLobby(tmp);
                ChatManager.ChatClient.SendPrivateMessage(_id.ToString(), "/invite " + tmp);
    
                Debug.Log("SendInvite");
            }
    
  • I just tested this and it works as expected:
    private string channelName = "lobby"
    
    private void Subscribe()
    {
           if (!this.chatClient.Subscribe(this.channelName, 0, -1,
                new ChannelCreationOptions {MaxSubscribers = 3, PublishSubscribers = true}))
            {
    			Debug.LogError("Error subscribing to lobby");
            }
    }
    
    public void OnSubscribed(string[] channels, bool[] results)
    {
            if (channels.Length == 1) // should be 1
            {
                if (this.channelName.Equals(channels[0])) // the channel we're looking for
                {
                    if (results[0]) // subscription successful
                    {
                        ChatChannel channel;
                        if (this.chatClient.TryGetChannel(this.channelName, false, out channel))
                        {
    						Debug.LogFormat("MaxSubscribers={0} PublishSubscribers={1} Subscribers.Length={2}", 
                                channel.MaxSubscribers, channel.PublishSubscribers, channel.Subscribers.Count);
                        }
                        else
                        {
                                 Debug.LogError("channel lobby not found, this is unexpected");   
                        }
                    } 
                    else 
                    {
                            Debug.LogError("subscription to lobby failed");
                    }
                }
            }
    }
    

    You need to review your code.
  • Ok I figured it out. I use static calls and haven't initialized the PartyManager at all. Since I do so the start method is never called. Thanks for your help.
Sign In or Register to comment.