1
\$\begingroup\$

Hi I am experiencing a rather weird problem wherein which I am disconnecting from Photon Chat immediately after I am connecting to it. Here is the code I am using for Photon Chat.

using ExitGames.Client.Photon;
using Photon.Chat;
using Photon.Chat.Demo;
using Photon.Pun;
using System;
using UnityEngine;
using UnityEngine.UI;

public class PhotonChatController : MonoBehaviour, IChatClientListener
{
    [SerializeField] private string nickname;
    [SerializeField] private Text Status;
    private ChatClient chatclient;

    private void Awake()
    {
        nickname = PlayerPrefs.GetString("USERNAME");
        FriendBoxManager.SendInviteEvent += SendInvite;
    }
    void Start()
    {
        chatclient = new ChatClient(this);
        ConnectToPhotonChat();
    }
    private void OnDestroy()
    {
        FriendBoxManager.SendInviteEvent -= SendInvite;
    }

    private void SendInvite(string recepientFriend)
    {
        chatclient.SendPrivateMessage(recepientFriend, "INVITE");
    }

    private void ConnectToPhotonChat()
    {
        Debug.Log("Connect to Photon Chat");
        chatclient.AuthValues = new Photon.Chat.AuthenticationValues(nickname);
        ChatAppSettings chatSettings = PhotonNetwork.PhotonServerSettings.AppSettings.GetChatSettings();
        chatclient.ConnectUsingSettings(chatSettings);
    }

    void Update()
    {
        chatclient.Service();//in the update so that I constantly is connected and receiving messages
    }

    public void SendDirectMessage(string recepient, string message)
    {
        chatclient.SendPrivateMessage(recepient, message);
    }

    public void DebugReturn(DebugLevel level, string message)
    {

    }

    public void OnDisconnected()
    {
        Debug.Log("You have disconnected from the Photon Chat........");
    }

    public void OnConnected()
    {
        Debug.Log("You have connected to the Photon Chat.......");
       // SendDirectMessage("chethan123", "Hi Chethan 123");
    }

   

    public void OnChatStateChange(ChatState state)
    {

    }

    public void OnGetMessages(string channelName, string[] senders, object[] messages)
    {
    }

    public void OnPrivateMessage(string sender, object message, string channelName)//callback when somebody sends another player a private message
    {
        if (!string.IsNullOrEmpty(message.ToString()))
        {
            //channel name is the format [sender : recepient]
            string[] splitnames = channelName.Split(new char[] { ':' });//here you are seperatign the sender and receiver names
            string senderName = splitnames[0];
            if (!sender.Equals(senderName, StringComparison.OrdinalIgnoreCase))
            {
                Debug.Log($"{sender}:{message}");
                Status.text=$"{sender}:{message}";
            }
        }
    }

    public void OnSubscribed(string[] channels, bool[] results)
    {
    }

    public void OnUnsubscribed(string[] channels)
    {
    }

    public void OnStatusUpdate(string user, int status, bool gotMessage, object message)
    {
    }

    public void OnUserSubscribed(string channel, string user)
    {
    }

    public void OnUserUnsubscribed(string channel, string user)
    {
    }
}

Also I am connecting to PUN in another game object using the following code:

public class PhotonConnector : MonoBehaviourPunCallbacks

{

  public static Action GetPhotonFriends = delegate { };

  private string Username;

  //public Text MyName;

  public Text StatusText;

   

   

  private PhotonView PV;

  private int maxPlayers;

  [SerializeField] private GameObject MessageWindow;

  [SerializeField] private float faderTimeforWindow = 0.3f;//see inspector for final values

  //username and room name is dynamic in future

  void Start()

  {

    Username = PlayerPrefs.GetString("USERNAME");

    // MyName.text = Username;

    ConnectToPhoton(Username);

    maxPlayers = 2;

    PV = GetComponent<PhotonView>();

  }



  private void ConnectToPhoton(string nickName)//username and nickname is the same.,photon uses nickname terminolgy

  {

    Debug.Log($"Connected to Photon as {nickName}");

    Status("Connecting to \n server.....");//message display on the matchMaking Screen

    PhotonNetwork.AuthValues = new AuthenticationValues(nickName);//just does a basic authetication as such

    PhotonNetwork.AutomaticallySyncScene = true;

    PhotonNetwork.NickName = nickName;



    PhotonNetwork.ConnectUsingSettings();

  }



  private void CreatePhotonRoom(string RoomName)

  {

    RoomOptions roomOptions = new RoomOptions();

    roomOptions.IsOpen = true;//room is open for others to join

    roomOptions.IsVisible = true;

    roomOptions.MaxPlayers = (byte)maxPlayers;



    

    PhotonNetwork.CreateRoom(RoomName, roomOptions, TypedLobby.Default);

  }  

 public override void OnConnectedToMaster()//checking whether u r connected to master photon server

  {

    base.OnConnectedToMaster();



    Debug.Log("You have connected to photon Master server");

    Status("Connected to \nServer!");



    if (!PhotonNetwork.InLobby)//if after connected to server if ur not in lobby ,connect to lobby

    {

      PhotonNetwork.JoinLobby();

    }

  }



  public override void OnJoinedLobby()//to verify whether u have joined a lobby

  {

    base.OnJoinedLobby();

    Debug.Log("You have connected to photon Lobby");

    Status(" ");

    // GetPhotonFriends?.Invoke();

  }



  public override void OnCreatedRoom()//called when the first person has joined the room or created it

  {

    base.OnCreatedRoom();

    Debug.Log($"You have created a Photon Room Named {PhotonNetwork.CurrentRoom.Name}");



  }



  public override void OnJoinedRoom()

  {

    base.OnJoinedRoom();

    

    Debug.Log($"You have Joined a Photon Room Named {PhotonNetwork.CurrentRoom.Name}");

  }



  public override void OnLeftRoom()//when current player left the room

  {

    base.OnLeftRoom();

    Debug.Log("You have left a photon room");

  }

  public override void OnJoinRoomFailed(short returnCode, string message)

  {

    base.OnJoinRoomFailed(returnCode, message);

    Debug.Log($"You failed to join a Photon Room : {message}");

  }

  public override void OnJoinRandomFailed(short returnCode, string message)

  {

    base.OnJoinRandomFailed(returnCode, message);

     

    Debug.Log($"You failed to join a random Photon Room : {message}");

  }



  public override void OnPlayerEnteredRoom(Player newPlayer)//when another player enters the room

  {

    base.OnPlayerEnteredRoom(newPlayer);

    Debug.Log($"Another player has joined the room->{newPlayer.UserId} and nick name->{newPlayer.NickName}");

    

  }

  public override void OnPlayerLeftRoom(Player otherPlayer)//when other player leaves the current room ur in

  {

    base.OnPlayerLeftRoom(otherPlayer);

    Debug.Log($"Player has left the room->: {otherPlayer.UserId} and nick name -> {otherPlayer.NickName}");

  }



  public override void OnMasterClientSwitched(Player newMasterClient)//called when the master client in the room leaves

  {

    base.OnMasterClientSwitched(newMasterClient);

    PhotonNetwork.Disconnect();//so that unwanted stuff doesnt happen

    Debug.Log($"New master client is {newMasterClient.NickName}");

  }



  public override void OnDisconnected(DisconnectCause cause)

  {

    base.OnDisconnected(cause);

    Status("Disconnected from \nserver");

  }

Also this is what I am getting the error as in the console.I have attached a screenshot for the same

Screen Shot of the console

Can we be connected to Photon Pun and Photon Chat at the same time?

\$\endgroup\$
1
  • \$\begingroup\$ Yeah, when I don't put that in Update, it doesn't show the error. But what I need is that the message has to be sent to the other person. I dont think it is in the first place connecting itself because Debug.Log("You have connected to the Photon Chat......."); could have got executed. I really dont think ,this is the right code or if it is the right code then why that statement is not getting executed? Also if not there where should I put the chatclient.Service() function actrually \$\endgroup\$ Jan 29, 2022 at 14:03

1 Answer 1

2
\$\begingroup\$

So the mistake I was doing was connecting to Photon Chat Server with "in" (India) in the server location. Actually for PUN "in" will be alright but for Photon Chat "asia" is the server location to be used(best suited for me)

I got the answer here, hope it helps someone as well

"There is no IN region for Photon Chat."

enter image description here

https://doc.photonengine.com/en-us/chat/current/connection-and-authentication/regions#available_regions

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .