{"id":799,"date":"2017-12-06T10:22:51","date_gmt":"2017-12-06T10:22:51","guid":{"rendered":"http:\/\/smartfoxserver.com\/blog\/?p=799"},"modified":"2017-12-06T10:41:06","modified_gmt":"2017-12-06T10:41:06","slug":"handling-network-switches-on-mobile-devices","status":"publish","type":"post","link":"https:\/\/smartfoxserver.com\/blog\/handling-network-switches-on-mobile-devices\/","title":{"rendered":"Handling network switches on mobile devices"},"content":{"rendered":"<p>In the <a href=\"http:\/\/smartfoxserver.com\/blog\/what-really-happens-when-you-unplug-your-networkwifi\/\">last blog post<\/a> we talked about what happens behind the scenes when we pull the ethernet cable or shut down the wifi network, and why this isn&#8217;t a reliable way to test a &#8220;sudden disconnection&#8221; scenario.<\/p>\n<p>In this new article we&#8217;ll continue our journey &#8220;behind the scenes&#8221; taking a look at\u00a0mobile devices and in particular\u00a0at how to correctly handle the switch between networks, such as WiFi, 3G and 4G.<!--more--><\/p>\n<h2>\u00bb The problem with mobile devices<\/h2>\n<p>Most multiplayer games\u00a0use 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\u00a0a local\u00a0WiFi to a 4G connection and viceversa.<\/p>\n<p>These events are quite disruptive for TCP socket connections and\u00a0developers need to be able to handle them correctly in order to avoid unexpected issues on the client side.<\/p>\n<p>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\u00a0that connection. The reason for this is that our original\u00a0session is tied to the public address from the WiFi network. When we switch to 3G\/4G we&#8217;ll be assigned a different public IP address which essentially renders us &#8220;unrecognizable&#8221; to the server, so we can&#8217;t continue to use our previous session.<\/p>\n<p><img loading=\"lazy\" class=\"size-full wp-image-800 aligncenter\" src=\"http:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2017\/12\/mobile-4g-wifi.png\" alt=\"\" width=\"395\" height=\"519\" srcset=\"https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2017\/12\/mobile-4g-wifi.png 395w, https:\/\/smartfoxserver.com\/blog\/wp-content\/uploads\/2017\/12\/mobile-4g-wifi-228x300.png 228w\" sizes=\"(max-width: 395px) 100vw, 395px\" \/><\/p>\n<p>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&#8217;ve effectively changed our public IP address.<\/p>\n<p>HRC+ does help when you&#8217;re\u00a0connected under the same IP address, but when we change network the server\u00a0will 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\u00a0player it would be\u00a0fairly easy to use this system maliciously\u00a0to kick existing players out or impersonating them.<\/p>\n<h2>\u00bb\u00a0Handling network switches<\/h2>\n<p>To handle a network switch correctly we will need to use the mobile OS own events so that we can be promptly notified\u00a0when a change in the network status has occurred. This form of notification is available both\u00a0under iOS and Android.<\/p>\n<ul>\n<li>Network changes in iOS are handled via the <strong>Reachability<\/strong> class<\/li>\n<li>Network events in Android are managed via the\u00a0<strong>NetworkUtil.getConnectivityStatus()\u00a0<\/strong>call<\/li>\n<\/ul>\n<p>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.<\/p>\n<p>For Android the code would look like this:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nprivate SmartFox sfs;\r\n\r\n\/\/ ...\r\n\r\nprivate void handleNetworkSwitch()\r\n{\r\n\tsfs.disconnect();\r\n\tsfs.removeAllEventListeners();\r\n\t\r\n\tstartNewConnection();\r\n}\r\n\r\nprivate void startNewConnection()\r\n{\r\n\tsfs = new SmartFox();\r\n\tsfs.addEventListener(SFSEvent.CONNECTION, onConnectionHandler);\r\n\t\r\n\t\/\/ Add more event listeners...\r\n}\r\n<\/pre>\n<p>For\u00a0iOS\u00a0the code is even\u00a0simpler:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar sfs:SmartFox2XClient?\r\n\r\nfunc networkSwitchEvent(notification:NSNotification)\r\n{\r\n\tsfs?.disconnect()\r\n    startNewConnection()\r\n}\r\n\r\nfunc startNewConnection()\r\n{\r\n\tsfs = SmartFox2XClient(smartFoxWithDebugMode: true, delegate: someDelegate)\r\n}\r\n<\/pre>\n<p>This is because the event system under iOS uses a single delegate,\u00a0 so there&#8217;s no need to add or remove event listeners one by one.<\/p>\n<h2>\u00bb Further readings<\/h2>\n<p>To learn more about each OS network state event management we highly recommend these articles from the stackoverflow website:<\/p>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/45959705\/how-to-detect-wifi-network-change-in-ios\" target=\"_blank\" rel=\"noopener noreferrer\">How to detect network changes on iOS<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/40713270\/how-to-get-network-state-change-on-android\" target=\"_blank\" rel=\"noopener noreferrer\">How to get network state change on Android?<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;t a reliable way to test a &#8220;sudden disconnection&#8221; scenario. In this new article we&#8217;ll continue our journey &#8220;behind the scenes&#8221; taking a look at\u00a0mobile devices and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[23],"tags":[88,87,84,89,37,7,86],"_links":{"self":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/799"}],"collection":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/comments?post=799"}],"version-history":[{"count":12,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/799\/revisions"}],"predecessor-version":[{"id":812,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/799\/revisions\/812"}],"wp:attachment":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/media?parent=799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/categories?post=799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/tags?post=799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}