How reconnect asap if Photon Network disconnect suddenly or user switched network from wifi to data

edited August 2019 in Tutorials and Code Sharing
I developed a game like 8 ball pool in unity3d. I am using photon network for multiplayer feature. The game works fine except in some conditions:

1.Photon network disconnects suddenly.
2. If a user try to switch network for example WiFi to mobile data then Photon network could no longer be connected (due to ip address changed).

To solve this I tried this:
  public override void OnDisconnectedFromPhoton() {
        Debug.Log("Disconnected from photon");

For matchmaking I wrote like this:
public void JoinRoomAndStartGame()
        ExitGames.Client.Photon.Hashtable expectedCustomRoomProperties = new ExitGames.Client.Photon.Hashtable() { { "tbl", GameManager.Instance.tableNumber }, { "isAvailable", true} };
        PhotonNetwork.JoinRandomRoom(expectedCustomRoomProperties, 0);


    public void OnPhotonRandomJoinFailed()
        RoomOptions roomOptions = new RoomOptions();
        roomOptions.PlayerTtl = 15000;
        roomOptions.CustomRoomPropertiesForLobby = new String[] { "tbl", "isAvailable" };
        roomOptions.CustomRoomProperties = new ExitGames.Client.Photon.Hashtable() { { "tbl", GameManager.Instance.tableNumber }, { "isAvailable", true} };
        roomOptions.MaxPlayers = 2;
        roomOptions.IsVisible = true;
        PhotonNetwork.CreateRoom(null, roomOptions, TypedLobby.Default);

I am new in photon. How to reconnect properly in those conditions ? I would be grateful if anyone help me to sort out this problem


  • Hi @masyam,

    Thank you for choosing Photon!

    I see you are using PUN Classic.

    How to reconnect properly in those conditions ?
    Your approach is correct. Reconnect or ReconnectAndRejoin are the methods to be used. However, you may call them in the OnConnectionFail callback instead of OnDisconnectedFromPhoton.

    If the calls fail, could you tell us what error do you get?
    Reconnect attempts should be done when the disconnect cause "allows" it.
    You may need to delay the reconnect calls a bit until the client has finished disconnecting properly. But this was related to an issue we fixed for PUN2, not sure if it's the same in PUN Classic.