How can I reconnect a Photon Bolt client after it disconnects?

225 views Asked by At

I'm trying to make a Photon Bolt game that connects two devices. The problem is that the Client tends to get disconnected a lot, an it doesn't reconnect automatically. I've tried using methods like ReconnectAndRejoin, but it seems like it only works in PUN. Right now I'm using this custom solution, without success:

[BoltGlobalBehaviour(BoltNetworkModes.Client)]
public class InitialiseGameClient : Photon.Bolt.GlobalEventListener
{
    
    private bool disconnected;

    public void Update(){

        if(disconnected){

            Reconnect();

        }

    }

    public override void Disconnected(BoltConnection connection)
    {

        disconnected = true;
        
    }
    


    public void Reconnect(){

        BoltLauncher.StartClient();
        PlayerPrefs.DeleteAll();

        if (BoltNetwork.IsRunning && BoltNetwork.IsClient)
        {

            foreach (var session in BoltNetwork.SessionList)
            {
                UdpSession udpSession = session.Value as UdpSession;

                if (udpSession.Source != UdpSessionSource.Photon)
                continue;

                PhotonSession photonSession = udpSession as PhotonSession;

                string sessionDescription = String.Format("{0} / {1} ({2})",
                photonSession.Source, photonSession.HostName, photonSession.Id);

                RoomProtocolToken token = photonSession.GetProtocolToken() as RoomProtocolToken;

                if (token != null)
                {
                    sessionDescription += String.Format(" :: {0}", token.ArbitraryData);
                }
                else
                {
                    object value_t = -1;
                    object value_m = -1;

                    if (photonSession.Properties.ContainsKey("t"))
                    {
                        value_t = photonSession.Properties["t"];

                    }

                    if (photonSession.Properties.ContainsKey("m"))
                    {
                        value_m = photonSession.Properties["m"];
                    }

                    sessionDescription += String.Format(" :: {0}/{1}", value_t, value_m);
                }

                ServerConnectToken connectToken = new ServerConnectToken
                {
                    data = "ConnectTokenData"
                };

                Debug.Log((int)photonSession.Properties["t"]);

                var propertyID = PlayerPrefs.GetInt("PropertyID", 2);;

                if((int)photonSession.Properties["t"] == propertyID){


                    BoltMatchmaking.JoinSession(photonSession, connectToken);
                    disconnected = false;

                }
            }
        }
        

    }
}

With this method I'm trying to use the same code used to connect the the client for the first time in the reconnect function, and keep trying until the client manages to connect. However it seems that the code never executes, even if the disconnect function gets triggered (the reconnect doesn't). Is there any Bolt integrated function that helps with reconnecting? Thanks in advance.

1

There are 1 answers

0
Michael Urvan On

You need to shutdown bolt, then try reconnecting. Even if you don't get the below exception, it's just an example and you should shutdown and do BoltLauncher.StartClient() etc.

BoltException: Bolt is already running, you must call BoltLauncher.Shutdown() before starting a new instance of Bolt.