How to handle Connection issues in Android devices while developing Multiplayer Games – AppWarp

We’ve developed a demo Android chat room application using AppWarp which illustrates how to handle intermittent network connection errors. It uses AppWarp’s network resiliency APIs.

Why is Network Resiliency required: On mobile devices, data connectivity has always been an issue. While users are on the go, the data source will often switch towers or downgrade/upgrade between 2G and 3G or WiFi or app goes to background by screen lock or by pressing home button. This has an impact on applications/games that rely on persistent data connectivity. AppWarp also provides powerful feature for handling network resiliency by which a user can maintain its app’s previous state in case of loosing connectivity.

To know more about AppWarp Resiliency : click Here



Application Contains Two Activities


Resilient Chat MainActivity How to handle Connection issues in Android devices while developing Multiplayer Games   AppWarpResilient Chat ChatActivity How to handle Connection issues in Android devices while developing Multiplayer Games   AppWarp


MainActivity:
Main Activity allows user to connect to the AppWarp Server. Users enter their name and connect to the AppWarp Server. To enable the Resiliency Feature, you need to set the Recovery Allowance interval after initializing WarpClient:

1
2
3
4
5
6
7
8
9
private void init(){
    WarpClient.initialize(Constants.apiKey, Constants.secretKey);
    WarpClient.setRecoveryAllowance(120);
    try {
        theClient = WarpClient.getInstance();
    } catch (Exception ex) {
        Toast.makeText(this, "Exception in Initilization", Toast.LENGTH_LONG).show();
    }
}

This tells the server to set the recovery allowance to the specified time during which the server will not destroy the user session even in case of loss of connectivity.

We have created a static room using AppHq console. Once connected and subscribed to the room, we move to the ChatActivity.

1
2
3
4
5
6
7
8
9
10
11
public static final String roomId = "1469583531"; // static room id defined in Constant.java
 
@Override
public void onSubscribeRoomDone(RoomEvent event) {
    if(event.getResult()==WarpResponseResultCode.SUCCESS){
        Intent intent = new Intent(this, ChatActivity.class);
        startActivity(intent);
    }else{
        showToastOnUIThread("onSubscribeRoomDone Failed with ErrorCode: "+event.getResult());
    }
}

ChatActivity.java

This Activity contains the logic of sending/receiving chat and maintains chat log. The upper part contains a list of joined users in the same room. Green Indicator means the user is online and grey means paused (temporary connection error).

Lower part of screen contains chat log send by users.

With the starting of this activity, we retrieve online users of room by calling

1
theClient.getLiveRoomInfo(Constants.roomId);

with the response in onGetLiveRoomInfoDone we add joined user in users list adapter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void onGetLiveRoomInfoDone(final LiveRoomInfoEvent event) {
    if(event.getResult()==WarpResponseResultCode.SUCCESS){
        onlineUserList.clear();
        if(event.getJoinedUsers().length>1){// if more than one user is online
            final String onlineUser[] = Utils.removeLocalUserNameFromArray(event.getJoinedUsers());
            for(int i=0;i<onlineUser.length;i++){
                User user = new User(onlineUser[i].toString(), true);
                Log.d(onlineUser[i].toString(), onlineUser[i].toString());
                onlineUserList.add(user);
            }
        resetAdapter();
    }else{
        showToastOnUIThread("No online user found");
    }
    }else{
        showToastOnUIThread("onGetLiveRoomInfoDone Failed with ErrorCode: "+event.getResult());
    }
}
 
:

Handing Connection Resiliency:

In case if any user breaks connectivity to AppWarp server by any reason, server will maintain the session until the defined recovery time but it will send a notification to the other users in the room that given user is in a paused state.

If user recovers its connection in the defined Resiliency time then the other users get notification that user has resumed. Otherwise the users will get the OnUserLeftRoom notification and remove the user from OnlineUser List.

Maintain Pause/Resume State: If we are using AppWarp Resiliency Feature we will get a notification if any user breaks connection to AppWarp server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Override
public void onUserPaused(String locid, boolean isLobby, String userName) {
    for(int i=0;i<onlineUserList.size();i++){
        User user = onlineUserList.get(i);
        if(user.getName().equals(userName)){
            user.setStatus(false);
        }
    }
    resetAdapter();
}
 
@Override
 
public void onUserResumed(String locid, boolean isLobby, String userName) {
    for(int i=0;i<onlineUserList.size();i++){
        User user = onlineUserList.get(i);
        if(user.getName().equals(userName)){
            user.setStatus(true);
        }
    }
    resetAdapter();
}

Recover Connection:

If user’s internet connection is broken by some reasons like switch over 2G/3G/WiFi/towers or by any other reason, we get a connection Error in ConnectonRequestListener with error code WarpResponseResultCode.CONNECTION_ERROR_RECOVERABLE by this we call recover connection API to resume our previous session. We recommend retrying to recover the connection at an interval of 5 seconds.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
theClient.RecoverConnection();
 
@Override
public void onConnectDone(final ConnectEvent event) {
    if(event.getResult() == WarpResponseResultCode.SUCCESS){
        showToastOnUIThread("Connection success");
    }
    else if(event.getResult() == WarpResponseResultCode.SUCCESS_RECOVERED){
        showToastOnUIThread("Connection recovered");
        runOnUiThread(new Runnable() {
        @Override
        public void run() {
        if(progressDialog!=null){
            progressDialog.dismiss();
        }
        progressDialog = ProgressDialog.show(ChatActivity.this, "", "Please wait..");
    }
    });
    theClient.getLiveRoomInfo(Constants.roomId);
    }
    else if(event.getResult() == WarpResponseResultCode.CONNECTION_ERROR_RECOVERABLE){
        runOnUiThread(new Runnable() {
        @Override
        public void run() {
            progressDialog = ProgressDialog.show(ChatActivity.this, "", "Recoverable connection error. Recovering session after 5 seconds");
        }
    });
    handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        progressDialog.setMessage("Recovering...");
        theClient.RecoverConnection();
    }
    }, 5000);
    }
    else{
       showToastOnUIThread("Non-recoverable connection error."+event.getResult());
        handleLeaveRoom();
        this.finish();
    }
 
}

Source Code can be downloaded or viewed from our git repo. If you have any questions or need any further assistance, please feel free to write us at support@shephertz.com.

twitter How to handle Connection issues in Android devices while developing Multiplayer Games   AppWarpfacebook How to handle Connection issues in Android devices while developing Multiplayer Games   AppWarpgoogle How to handle Connection issues in Android devices while developing Multiplayer Games   AppWarplinkedin How to handle Connection issues in Android devices while developing Multiplayer Games   AppWarppinterest How to handle Connection issues in Android devices while developing Multiplayer Games   AppWarpreddit How to handle Connection issues in Android devices while developing Multiplayer Games   AppWarpstumbleupon How to handle Connection issues in Android devices while developing Multiplayer Games   AppWarptumblr How to handle Connection issues in Android devices while developing Multiplayer Games   AppWarp

Leave a Reply

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


+ one = 10

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>