iOS Connection Resiliency Sample

In this blog post we are going to discuss how you can use our Connection resiliency feature which was announced in the previous blog post.It is a new feature of our gaming server platform AppWarp which provides support to recover from intermittent connection errors.

To avail the benefits of Connection Resiliency, a developer should enable this feature just after initializing the WarpClient as follows:

1
[[WarpClient getInstance] setRecoveryAllowance: maxRecoveryTime ];

Above API sets the connection recovery time (in seconds) during which the session can be recovered which is by default 0.Therefore there is no connection recovery and the SDK behavior will be same as before.Recommended values are between 60 to 120 i.e. 1 minute to 2 minutes.

We will be using our Fight Ninja demo project to walk you through the Connection Resiliency implementation.

Whenever the connection to AppWarp server gets disturbed due to any unexpected network problem,the onConnectDone callback of ConnectionRequestListener will be invoked with an error “CONNECTION_ERROR_RECOVERABLE”.Then we can call AppWarp’s “recoverConnection” API to recover the session if the device’s network connectivity is restored.So,now it is very important that we handle the onConnectDone callback very carefully as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-(void)onConnectDone:(ConnectEvent*) event
{
    if (event.result==SUCCESS)
    {
       NSLog(@"connection success response for normal connection request.");
       [[WarpClient getInstance] joinRoomInRangeBetweenMinUsers:0 andMaxUsers:1 maxPrefered:YES];
    }
    else if (event.result==SUCCESS_RECOVERED)
    {
       NSLog(@"connection recovered as the recoverConnection is called within the recovery time duration.");
       [[AppWarpHelper sharedAppWarpHelper] unScheduleRecover];
     }
    else if (event.result==CONNECTION_ERROR_RECOVERABLE)
    {
         NSLog(@"recoverable connection error");
        [[AppWarpHelper sharedAppWarpHelper] scheduleRecover];
     }
    else if (event.result==BAD_REQUEST)
    {
       NSLog(@"Bad request");
     }
     else
    {
          NSLog(@"Disconnected");
          NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Connection Error:",@"title",@"Please check ur internet connection!",@"message", nil];
          [[AppWarpHelper sharedAppWarpHelper] onConnectionFailure:dict];
     }
}

You can navigate to the class ConnectionListener.m of Fight Ninja to find the above callback.

Here the most important thing is that the session will be recovered only when the network is working again.So there are two ways in which we can manage the recoverConnection call.

First: we should have a proper way to detect the device network connectivity and when we know that the connection is working again,we can call the “recoverConnection” API.

Second: when the recoverable connection error comes, we can schedule the “recoverConnection” API call after every certain time interval,let’s say 5 secs and unschedule if connection is recovered with the result SUCCESS_RECOVERED or if the connection is disconnected in case of recovery time out.

In the Fight Ninja, we have chosen the second way and the scheduling of the recoverConnection is done using NSTimer.Now you need to navigate to the class AppWarpHelper.m which will show you how to schedule the API:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-(void)scheduleRecover
{
      if (!timer)
      {
            timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(recoverConnection) userInfo:nil repeats:YES];
            [[CCDirector sharedDirector] pause];
            [[NFStoryBoardManager sharedNFStoryBoardManager] showPausedView:@"Reconnecting ..."];
      }
}
 
-(void)unScheduleRecover
{
      if (timer)
      {
          [timer invalidate];
          timer = nil;
          if ([[CCDirector sharedDirector] isPaused])
          {
                  [[NFStoryBoardManager sharedNFStoryBoardManager] removePausedView];
          }
      }
}
 
-(void)recoverConnection
{
     NSLog(@"%s",__FUNCTION__);
     [[WarpClient getInstance] recoverConnection];
}

The method “scheduleRecover” is called from “onConnectDone” callback of ConnectionListner and it schedules the recoverConnection to be called after every 5 seconds interval until “unscheduleRecover” method is called from “onConnectDone”.

Whenever a user is disconnected from a room with recoverable connection error then the other users in that room will receive a notification from AppWarp server in the Notification Listener saying a user is paused and that can be handled for the other users as per the requirement.If the App is a 2-player Game,then with this event game can be paused until that user recovers the session.

1
2
3
4
5
6
7
8
-(void)onUserPaused:(NSString *)userName withLocation:(NSString *)locId isLobby:(BOOL)isLobby
{
       [[CCDirector sharedDirector] pause];
       NSLog(@"%s..username=%@",__FUNCTION__,userName);
       NSLog(@"locId=%@",locId);
       NSLog(@"isLobby=%d",isLobby);
       [[NFStoryBoardManager sharedNFStoryBoardManager] showPausedView:[NSString stringWithFormat:@"Waiting for %@ response...",userName]];
}

reconnecting1 iOS Connection Resiliency Sample

ray response iOS Connection Resiliency Sample

If the connection is restored successfully for that user, a user resumed event will be raised for all the users in the room.

1
2
3
4
5
6
7
-(void)onUserResumed:(NSString *)userName withLocation:(NSString *)locId isLobby:(BOOL)isLobby
{
     NSLog(@"%s..username=%@",__FUNCTION__,userName);
     NSLog(@"locId=%@",locId);
     NSLog(@"isLobby=%d",isLobby);
     [[NFStoryBoardManager sharedNFStoryBoardManager] removePausedView];
}

On the other hand,after a specified recovery time if the concerned user is unable to restore its connection, a user left room event will be raised for other users in the room.

This sample was created for iOS.Currently,this feature is available in the latest version of our Android and iOS SDKs.We will soon be updating it for other platforms as well.To learn more,visit our connection resiliency feature page.if you have any questions or need any further assistance,please feel free to write us at support@shephertz.com

twitter iOS Connection Resiliency Samplefacebook iOS Connection Resiliency Samplegoogle iOS Connection Resiliency Samplelinkedin iOS Connection Resiliency Samplepinterest iOS Connection Resiliency Samplereddit iOS Connection Resiliency Samplestumbleupon iOS Connection Resiliency Sampletumblr iOS Connection Resiliency Sample

Leave a Reply

Your email address will not be published. Required fields are marked *


− 3 = five

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>