Handling network switches on mobile devices

In the last blog post we talked about what happens behind the scenes when we pull the ethernet cable or shut down the wifi network, and why this isn’t a reliable way to test a “sudden disconnection” scenario.

In this new article we’ll continue our journey “behind the scenes” taking a look at mobile devices and in particular at how to correctly handle the switch between networks, such as WiFi, 3G and 4G.

» The problem with mobile devices

Most multiplayer games use one or more persistent connections to a central server (or other clients, in p2p mode) and thus are very sensitive to sudden changes in the network setup. This is particularly evident on mobile devices where an application can be put in the background or the user might switch from a local WiFi to a 4G connection and viceversa.

These events are quite disruptive for TCP socket connections and developers need to be able to handle them correctly in order to avoid unexpected issues on the client side.

Regardless of the operating system on our device, if we started a game connection via our WiFi and later switch to 3G/4G while still in game, we will loose that connection. The reason for this is that our original session is tied to the public address from the WiFi network. When we switch to 3G/4G we’ll be assigned a different public IP address which essentially renders us “unrecognizable” to the server, so we can’t continue to use our previous session.

To solve this issue there is only one solution: dropping the old connection and starting a new one. Even the HRC+ system in SFS2X, which helps with temporary loss of connection, will not help us here because we’ve effectively changed our public IP address.

HRC+ does help when you’re connected under the same IP address, but when we change network the server will not allow us in. The reason, as you may imagine, is security. If a user with another IP address could claim to be an existing player it would be fairly easy to use this system maliciously to kick existing players out or impersonating them.

» Handling network switches

To handle a network switch correctly we will need to use the mobile OS own events so that we can be promptly notified when a change in the network status has occurred. This form of notification is available both under iOS and Android.

  • Network changes in iOS are handled via the Reachability class
  • Network events in Android are managed via the NetworkUtil.getConnectivityStatus() call

When a network is switched we highly recommend to close your current connection to SmartFoxServer, remove all event event listeners and proceed by creating a new connection object.

For Android the code would look like this:

private SmartFox sfs;

// ...

private void handleNetworkSwitch()
{
	sfs.disconnect();
	sfs.removeAllEventListeners();
	
	startNewConnection();
}

private void startNewConnection()
{
	sfs = new SmartFox();
	sfs.addEventListener(SFSEvent.CONNECTION, onConnectionHandler);
	
	// Add more event listeners...
}

For iOS the code is even simpler:

var sfs:SmartFox2XClient?

func networkSwitchEvent(notification:NSNotification)
{
	sfs?.disconnect()
    startNewConnection()
}

func startNewConnection()
{
	sfs = SmartFox2XClient(smartFoxWithDebugMode: true, delegate: someDelegate)
}

This is because the event system under iOS uses a single delegate,  so there’s no need to add or remove event listeners one by one.

» Further readings

To learn more about each OS network state event management we highly recommend these articles from the stackoverflow website: