reconnecting after connect error - cocos2dx

0 votes
Hello, If I try to recover the session in within the recovery allowance, things work fine, but, if not I'm getting connection error, then when I try reconnecting with new session it still using same session and keep getting connection error. The question is how I force appwarp to start working with new session, I tried initialize without any sucess
asked Aug 7, 2014 in App42 Cloud API-BaaS by smhmdmjd (11 points)
recategorized Oct 26, 2014 by sushil
Hi,
Generally, connection_error comes when there is no internet, can you please check if this is the problem.
Can you provide some more detail, like how you are trying to reconnect with new session. If you will provide the code snippet, it will be better.
Hi Rajeev, thanks for the resonse, I will try to give more details, I actually trying to make my game robust and even try to connect to internet after the allowance recovery period.
I tried to shut down the internet connection and turn it back on in purpose within the allowance and that works fine, when tried to to turn it after the allowance recovery time, I received connection error (which makes sense), but then trying to connect with new session without any success


void ClientService::onConnectDone(int res)
{
    CONNECTION_STATE state = CONNECTION_ERROR;
    if (res==AppWarp::ResultCode::success)
    {
      unscheduleRecover();
        log("\nonConnectDone .. SUCCESS..session=%d\n",AppWarp::AppWarpSessionID);
        state = CONNECTION_SUCCESS;
    }
    else if (res==AppWarp::ResultCode::success_recovered)
    {
        unscheduleRecover();
        log("\nonConnectDone .. SUCCESS with success_recovered..session=%d\n",AppWarp::AppWarpSessionID);
        state = CONNECTION_SUCESS_RECOVERD;
    }
    else if (res==AppWarp::ResultCode::connection_error_recoverable)
    {
        scheduleRecover();
        log("\nonConnectDone .. FAILED..connection_error_recoverable..session=%d\n",AppWarp::AppWarpSessionID);
        state = CONNECTION_ERROR_RECOVERABLE;
    }
    else if (res==AppWarp::ResultCode::bad_request)
    {       
        unscheduleRecover();
        log("\nonConnectDone .. FAILED with bad request..session=%d\n",AppWarp::AppWarpSessionID);
        state = CONNECTION_ERROR;
    }
    else
    {
        AppWarp::Client::getInstance()->disconnect();
        unscheduleRecover();
        log("\nonConnectDone .. FAILED with unknown reason..session=%d\n",AppWarp::AppWarpSessionID);
        state = CONNECTION_ERROR;
    }
   
    auto cc= new ClientEventUserLoginResponse(state);
    clientCallback(cc);
}



callback receiver class

void MainController::HandleLoginResponse(ClientEventUserLoginResponse* clientEventUserLoginResponse)
{
    isConnected = clientEventUserLoginResponse->State == CONNECTION_SUCCESS;
    _connectivity->UpdateConnectivityStatus(clientEventUserLoginResponse->State == CONNECTION_SUCCESS);
    if(clientEventUserLoginResponse->State == CONNECTION_SUCCESS ||
        clientEventUserLoginResponse->State == CONNECTION_SUCESS_RECOVERD)
    {
        log("MainController::CONNECTION_SUCCESS");
        if(internetDownLayer->getParent() != nullptr)
            internetDownLayer->removeFromParentAndCleanup(false);
        UserDefault::sharedUserDefault()->setStringForKey("username",userName);
        UserDefault::sharedUserDefault()->setStringForKey("password",password);

    }
    else if(clientEventUserLoginResponse->State == CONNECTION_ERROR_RECOVERABLE)
    {
        // open dialog
        log("MainController::CONNECTION_ERROR_RECOVERABLE");
        if(firstPopup && internetDownLayer->getParent() == nullptr)
        {
            Director::getInstance()->getRunningScene()->addChild(internetDownLayer,4,"internetDownLayer");
            firstPopup = false;
        }
    }
    else if(clientEventUserLoginResponse->State == CONNECTION_ERROR)
    {
        // schedule connect
        //remove dialog if open
        log("MainController::CONNECTION_ERROR");
        if(firstPopup && internetDownLayer->getParent() == nullptr)
        {
            Director::getInstance()->getRunningScene()->addChild(internetDownLayer,4,"internetDownLayer");
            firstPopup = false;
        }
        scheduleOnce(schedule_selector(MainController::recoverConnection),5.0f);

        // back to main menu if playing
        if(isOnline) // playing
            MainScene::getMainScene(_mainScene)->switchToLayer(0);
    }
}

void MainController::recoverConnection(float dt)
{
    log(" MainController::recoverConnection");

    ClientEventUserLoginRequest* loginRequest = new ClientEventUserLoginRequest(userName,password);
    Login(loginRequest);
}

1 Answer

0 votes

Hi,

When recoveryAllowance time-out occur, you get auth_error as result code. In the above code snippet, this case is not handled. Please refer the following code snippet for better understanding:

void HelloWorld::onConnectDone(int res)

{

    if (res==AppWarp::ResultCode::success)

    {

        // Fired when you get success in response of connect

    }

    else if (res==AppWarp::ResultCode::success_recovered)

    {

        // Fired when you get success in response of recover

    }

    else if (res==AppWarp::ResultCode::auth_error)

    {

        /* Fired when server denies your request in case of

            1.user name is already in use

            2.recoveryAllowance time-out

            3.if user name is not valid

         */

          // Here you should call connect 

    }

    else if (res==AppWarp::ResultCode::connection_error)

    {

        // Fired when there is no internet

    }

    else if (res==AppWarp::ResultCode::connection_error_recoverable)

    {

        // Fired when internet get disconnected while you are playing the game

        // In this case you should call recoverConnection to recover previous session

       // Here you should call recoverConnection

    }

    else if (res==AppWarp::ResultCode::bad_request)

    {

        //Fired when the parameters passed to the request are invalid.

        //For example if null or empty string is passed in the username parameter while calling connect.

    }

    else

    {   }

}

I hope it will solve the problem. Let me know if problem continues.

answered Aug 8, 2014 by rajeev.etc (1,660 points)
Then what should I do in case I received with_err and after a while the internet is back and I want to reconnect, the only solution I have is restarting the app
In other words if internet is available after recoveryAllowance time-out
I have mentioned three reasons of auth_error in the above code snippet that you can handle as follows:

case 2 (RecoveryAllowance time-out):  You can call connect with same or different user name in this case.

case1 & 3: you should not call connect API with same user name otherwise auth_error will keep coming.
OK, thank you for the help
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...