2

I developed a game like 8 ball pool. 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");
        PhotonNetwork.ReconnectAndRejoin();
    }

But it's not working properly.

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
MASYAM
  • 35
  • 1
  • 5
  • 1
    I guess it fails because `PhotonNetwork.ReconnectAndRejoin();` is tried to be done in the moment when user apparently lost the connection ... you should try doing it in a routine for trying to reconnect in a loop in certain time intervals until it succeeds – derHugo Aug 15 '19 at 07:00

1 Answers1

0
public override void OnDisconnected(DisconnectCause cause)
{
    StartCoroutine(MainReconnect());  
}
private IEnumerator MainReconnect()
{
    while (PhotonNetwork.NetworkingClient.LoadBalancingPeer.PeerState != ExitGames.Client.Photon.PeerStateValue.Disconnected)
    {
        Debug.Log("Waiting for client to be fully disconnected..", this);

        yield return new WaitForSeconds(0.2f);
    }

    Debug.Log("Client is disconnected!", this);

    if (!PhotonNetwork.ReconnectAndRejoin())
    {
        if (PhotonNetwork.Reconnect())
        {
            Debug.Log("Successful reconnected!", this);
        }
    }
    else
    {
        Debug.Log("Successful reconnected and joined!", this);
    }
}
ynh
  • 19
  • 1