Photon SDK with cocos2d-x reconnect when app returns back from the background.

I am trying to disconnect with the peer when the app goes in BG and reconnect once back in focus. I am using cocos2d-x and photon SDK for android.

im adding these events in appdelegate.cpp
// This function will be called when the app is inactive. Note, when receiving a phone call it is invoked.
void AppDelegate::applicationDidEnterBackground() {
    ExitGames::Photon::PhotonPeer::disconnect();
    Director::getInstance()->stopAnimation();
    // if you use SimpleAudioEngine, it must be paused
    // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    ExitGames::Photon::PhotonPeer::connect();
    Director::getInstance()->startAnimation();

    if(!NetworkManager::getInstance()->isConnectedToPhoton)
        NetworkManager::getInstance()->connect();

    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}

I know that I am not passing wrong arguments for connect function, but I dont have server url for photon as well as not able to make static calls. creating new peer object does not look like a right option here. can someone please guide me on how to achieve this?

Thanks

Comments

  • PS: app works fine without sending in the background. only after it is refocussed, photon is not able to connect. I figured that OS must be closing the socket it communicates on, hence this question.
  • KaiserludiKaiserludi admin
    edited February 11
    Hi @Abhinav.

    ExitGames::Photon::PhotonPeer::connect();
    
    That can't compile as connect() is not a static function. You need to call it on an actual instance of PhotonPeer. The same is true for disconnect().
    but I dont have server url for photon
    How are you connecting to Photon in other situations then (you say that the "app works fine without sending in the background", so what are you passing as address in that case?)

    NetworkManager::getInstance()->connect();
    
    That looks good. You could simply store the URL inside your NetworkManager instance.

    Are you writing you own Photon server side application from scratch? Because if you connect to Photon Cloud or to a self hosted server that runs the LoadBalancing application or a custom application that has been written on top of the LoadBalancing application, then you should rather use LoadBalancing::Client instead of dealing with a Photon::PhotonPeer directly. LoadBalancing::Client offers a reconnectAndRejoin() function that reconnects to the game server to which you have been connected and rejoins the room that you have been in when the disconnect occurred.
  • thanks for replying @Kaiserludi , we are using Photon Realtime, for which we have an app id for the project we created on photon cloud itself.

    We are using that appID to connect with the service everytime as provided in loadbalancing demo. As of now I could not find any URL to the service, however we are setting the
    RegionSelectionMode
    
    to BEST, I suppose that could be providing the url to best available server based on player's location.

    I tried connecting and disconnecting using networkManager, but that did not help. here is our implementation of those functions:
    void NetworkManager::connect() {
      #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
      sharedInstance->getNetworkLogic().connect(ExitGames::LoadBalancing::AuthenticationValues().setUserID(ExitGames::Common::JString() + GETTIMEMS()));
    #endif
    }
    
    void NetworkManager::disconnect() {
        #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
      sharedInstance->getNetworkLogic().disconnect();
      #endif
    }
    

    TBH, due to lack of proper documentation for C++, we started by using Network logic from demo loadbalancing project.
  • KaiserludiKaiserludi admin
    edited February 13
    Hi @Abhinav.

    So you are actually using LoadBalancing::Client and not directly Photon::PhotonPeer.

    In that case you should not call PhotonPeer.connect() directly, when you want do reconnect your Client instance, but you should instead call Client::connect().

    When connecting to Photon Cloud, you don't need a server address, as the Client knows the address of the entry point of Photon Cloud itself.

    Client::connect() on default (if you don't specify a different address / server type through its parameters) always connect to the Photon Cloud name server and retrieves the available regions.
    When using RegionSelectionMode::BEST, then it pings all retrieved regions and connects to the master server of a server cluster that is assigned to the region for which it got the best ping result.

    Once you have successfully connected in RegionSelectionMode::BEST, you can access the cached identifier of the region to which you have connected by calling getRegionWithBestPing().

    After the initial connect any future connects in the lifetime of the Client instance with RegionSelectionMode::BEST will not ping all regions again, but directly connect to that cached region.

    Once you destruct the Client instance, the cached best region is lost and the first connect with RegionSelectionMode::BEST, after constructing a new instance will ping all regions again.

    It is highly recommend to store the return value of getRegionWithBestPing() in a file and to only use RegionSelectionMode::BEST when you don't have a stored region identifier yet or when the user indicates through your UI that he explicitly wants you to re-ping all regions. When you have a stored region identifier, then you should use to select the region with that identifier with RegionSelectionMode::SELECT to avoid the time-intensive re-pinging of all regions on every first connect after constructing a Client instance (like after restarting the app or the device).

    When the client has been inside a game room when the disconnect has occurred, then you can call Client::reconnectAndRejoin() instead of Client::connect() to directly reconnect to the game server that it was connected to and rejoin the room in which it was. This will skip the name server and the region cluster master server and hence is quicker then a manual connect + join.
    I tried connecting and disconnecting using networkManager, but that did not help. here is our implementation of those functions:
    Are you calling service() in this situation? Are you waiting for disconnectReturn() to get called before calling connect() again?
    Have you checked the return value of Client::connect()?. Have you checked the log output of the Photon Client?
    TBH, due to lack of proper documentation for C++, we started by using Network logic from demo loadbalancing project.
    Are you aware of the Photon C++ Client API-reference, which is included with the Client SDK and also available online at https://doc-api.photonengine.com/en/cpp/current?

    The API reference page for class Loadbalancing::Client at https://doc-api.photonengine.com/en/cpp/current/a05522.html explains everything that I have just written and more.
Sign In or Register to comment.