How to warn users if the master is not connected?

I want users to exit the game if the internet is not ready or the master is not connected. The following code is what I currently is using. The problem is that there is no error message at all if my internet cable is unplugged.

How to warn users if the master is not connected, so I can ask the users to check their cables.


Thanks alot.


using Photon.Pun;
using Photon.Realtime;
using UnityEngine;


namespace MultiUserPackage
{
    public class PhotonLobby : MonoBehaviourPunCallbacks, IConnectionCallbacks
    {
        public static PhotonLobby Lobby;
        //public AzureSpatialAnchorScripts myAnchorScripts;
        public mySpatialScript myAnchorScripts;
        private int roomNumber = 1;
        private int userIdCount;


        private void Awake()
        {
            if (Lobby == null)
            {
                Lobby = this;
            }
            else
            {
                if (Lobby != this)
                {
                    Destroy(Lobby.gameObject);
                    Lobby = this;
                }
            }
            DontDestroyOnLoad(gameObject);


            GenericNetworkManager.OnReadyToStartNetwork += StartNetwork;
        }


        public override void OnConnectedToMaster()
        {
            var randomUserId = Random.Range(0, 999999);
            PhotonNetwork.AutomaticallySyncScene = true;
            PhotonNetwork.AuthValues = new AuthenticationValues();
            PhotonNetwork.AuthValues.UserId = randomUserId.ToString();
            userIdCount++;
            PhotonNetwork.NickName = PhotonNetwork.AuthValues.UserId;
            clickToJoin();
        }


        public override void OnErrorInfo(ErrorInfo errorInfo)
        {
            base.OnErrorInfo(errorInfo);
            Debug.Log(errorInfo.ToString());
        }


        //public override void OnFailedToConnectToPhoton(DisconnectCause cause)
        //{
        //    Debug.Log(cause.ToString());
        //}


        public void clickToJoin()
        {
            //PhotonNetwork.JoinRandomRoom();
            var roomOptions = new RoomOptions { IsVisible = true, IsOpen = true, MaxPlayers = 10 };
            PhotonNetwork.JoinOrCreateRoom("Rimag999", roomOptions, TypedLobby.Default);
        }
        public void clickToLeave()
        {
            PhotonNetwork.LeaveRoom();
        }


        public override void OnJoinedRoom()
        {
            base.OnJoinedRoom();


            Debug.Log("PhotonLobby.OnJoinedRoom()");
            Debug.Log("Current room name: " + PhotonNetwork.CurrentRoom.Name);
            Debug.Log("Other players in room: " + PhotonNetwork.CountOfPlayersInRooms);
            Debug.Log("Total players in room: " + (PhotonNetwork.CountOfPlayersInRooms + 1));
            myAnchorScripts.Automation();
        }


        public override void OnJoinRandomFailed(short returnCode, string message)
        {
            Debug.Log("\nPhotonLobby.OnJoinRandomFailed()");
            Debug.LogError("Joining Room Failed");
            CreateRoom();
        }


        public override void OnCreateRoomFailed(short returnCode, string message)
        {
            Debug.Log("\nPhotonLobby.OnCreateRoomFailed()");
            Debug.LogError("Creating Room Failed");
            CreateRoom();
        }


        public override void OnCreatedRoom()
        {
            base.OnCreatedRoom();
            roomNumber++;
            Debug.LogError($"Trying to create room for {roomNumber} times");
        }


        public void OnCancelButtonClicked()
        {
            PhotonNetwork.LeaveRoom();
        }


        private void StartNetwork()
        {
            PhotonNetwork.ConnectUsingSettings();
            Lobby = this;
            if (PhotonNetwork.IsConnected)
            {
                Debug.Log("Not connected, please check internet connection");
            }
        }


        private void CreateRoom()
        {
            var roomOptions = new RoomOptions {IsVisible = true, IsOpen = true, MaxPlayers = 10};
            PhotonNetwork.CreateRoom("Rimag999");
        }
    }
}

```

Best Answer

  • Tobias
    Tobias admin
    Answer ✓

    There is no "master" when the clients can't connect.

    Try to ConnectUsingSettings and if that fails, PUN calls your callback OnDisconnected(). Make sure your script registered for the callback.

    Running the client with the SupportLogger enabled (see PhotonServerSettings) might show some errors / callbacks you miss now.

Answers

  • Tobias
    Tobias admin
    Answer ✓

    There is no "master" when the clients can't connect.

    Try to ConnectUsingSettings and if that fails, PUN calls your callback OnDisconnected(). Make sure your script registered for the callback.

    Running the client with the SupportLogger enabled (see PhotonServerSettings) might show some errors / callbacks you miss now.

  • OnDisconnected() does work as expected. Thanks.