1. Background
Mobile network signal fluctuations frequently, to the mobile game developers to bring a lot of trouble, poor handling will result in poor user experience and repeat items such as a serious problem. Therefore, the weak network problem in the TDR Technology Review as the client's key challenge, and weak network special test standards can be launched. This article gives a more general solution to the problems encountered in the past projects.
2. Network connection mode
In general, game clients are connected by creating sockets and servers, but they are also divided into two types of connections based on usage scenarios: TCP connections and HTTP connections.
1) A TCP connection is a long connection that we often say. In this connection, once the socket connection is established, the communication parties can send data to each other until the connection is terminated. At present, the company's mobile-connected games Use this kind of data communication method more.
2) An HTTP connection is a short connection that we often say. This connection uses the "request-response" communication method, each interaction by the client initiated the request, the server received the request to reply to the data, after the data transfer is complete, the socket connection will be disconnected. This connection is typically used when downloading CDN resources or cloud configurations.
3. Network Detection
3.1 Network environment of detection equipment
Both iOS and Android provide a way to detect the local network environment, with the features we need:
1) Network environment identification, distinguish the current network environment: Wifi/wwlan/notreachable and so on.
2) Network switching awareness, the network environment after the switch will receive system messages.
There is a reachability example on the Apple Developer website (developer.apple.com) that encapsulates the underlying network components to achieve this functionality. The Connectivity Manager service is available on Android and can be encapsulated to achieve the same functionality. The appendix provides the relevant open source code and encapsulates the implementation of Reachability on iOS and Android platforms, respectively.
3.2 Heartbeat time-out and packet-back timeout detection
The heartbeat is every time interval (assuming 15 seconds) the client and the server make a request/response to determine if the other person is alive. If the client sends a request successfully, it is considered that the connection has been interrupted or the server is down because it has not received a response from the server for a long time (assuming 60 seconds). If the server has not received a client request for a long time (assuming 300 seconds), you can assume that the client is offline. In addition, regular business packets can also be considered as the expansion of the heartbeat packet, so each time the business packet communication succeeds, both the client and the server have to be re-clocked. A general heartbeat packet is an empty packet to save traffic, but usually also contains a small number of fields, such as time synchronization information for the client and the server.
Some key protocols, such as requests to enter a room, need to wait for a service response before deducting physical access to the room. However, when the network is unstable, the client's request may be sent successfully, the server's response is slow to receive, in this case, the client needs to do a timeout control, such as 15 seconds after the client has not received the package, then give a hint, can not allow the client infinite wait. This causal association of a pair of protocols is called a request-response protocol, and it is recommended that all key protocols adopt this mechanism. One thing to note is that this asynchronous operation has a waiting time, which in general will mask the input (/show activity indicator), preventing the user from doing other actions that cause duplicate requests. This also requires us to avoid nesting and concurrency of multiple key protocols at the logical level of the code.
<ignore_js_op>
3.3 Test Packet failure
Generally speaking, reachability is sensitive and reliable, the equipment network changes can be timely sense, as long as the monitoring state switch to notreachable can be considered to be disconnected (need to rule out the situation of transient), but reachability also has limitations, can not perceive the transmission layer connection disconnected. For example: The phone and wireless router connection is normal, but the wireless router and modem connection is interrupted, then reachability is not detected network disconnection. The network condition needs to be judged based on the socket error code.
3.4 Detecting Socket status
The above several mechanisms are in the application layer to do network condition detection, basically can deal with most of the situation, but sometimes for better user experience, we need more accurate detection method. Getting the socket's underlying error code and status can provide us with more evidence to judge. Because the actual project does not use this content, it will not be expanded here.
3.5 Network Latency Detection
Some types of games are particularly sensitive to network latency (such as real-time PvP games), and higher network latency can lead to a miserable experience. These types of games need to be optimized not only from a technical perspective, but also based on the user's current network latency. For example, the average delay in the 1000ms above the hint can not play. We can evaluate latency in two ways: signal strength (Received signal strength indication) and packet time statistics.
1) Signal strength (RSSI) Detection: This method of judging network latency is not objective, because network latency depends not only on signal strength, but also on a range of factors such as bandwidth, transmission node count, and network hardware throughput. However, when other parameters are relatively fixed, we can still refer to the signal strength to judge the network condition, similar to reachability, Rssi can give us feedback on the current network status in time. Use the Wifimanager class on the Android platform to obtain specific RSSI values and signal strength levels. Unfortunately, iOS 5.0 and later systems no longer support RSSI acquisition (and, of course, jailbreak).
2) Collection Time statistics: that is, in the case of client and server time synchronization, each packet with a timestamp information, based on the past packet computing average network latency. This kind of detection method is relatively reasonable, but the timeliness is poor, in the network fluctuation frequently can not be properly evaluated in time, the contrary in the connection smooth network environment, will get the ideal effect.
To achieve a reasonable network latency on the Android platform, you can combine both of these approaches. The second way can only be used temporarily on the iOS platform.
4, the connection mechanism of disconnection
When the disconnection is detected, the re-connect mode can be started. Based on the current game state to determine the re-connect policy, there are generally the following three ways:
1) Silent re-connection, that is, the user is not aware of the case of re-connected. After the disconnection is generally detected, you can first try to silently re-connect a certain number of times (such as 3 times). If you are disconnected during game play, you will generally try to silently re-connect and ignore the number of times you are connected, as the popup will break the integrity of the PVP experience. Silent Re-connection provides a user-friendly experience that can cope with short-lived network outages (such as moving in and out of an elevator or process from the background).
2) Explicit reconnection, after the silent reconnection a certain number of times (assuming 3 times), still unable to connect successfully, you need to pop up a prompt box, interrupt the game process, tell the user the current network environment is poor, guide the user in a better network and then try to connect.
3) server failure reconnection, in which case the client is not connected to the game server anyway. At this point the client also needs to give the correct boot, rather than mistakenly treat as a disconnection fault. So we add a step after the disconnection failure: try to connect the CDN server, if the CDN server can connect properly, then the network is unblocked, we go to get the cloud configuration on the CDN, check if there is a server routine maintenance of the logo, if any, give the server daily Maintenance bulletin, Otherwise, you can assume that the server is down, then give a notice of the server failure. If the CDN server is unable to connect in this step, it is true that the network is not unblocked and can continue to go through the reconnection process or wait.
5, the Development of network protocol specifications
To achieve a good re-connect experience, not only need a good solution, but also need to have agreement on the support. Generally, the following rules can be referenced in the protocol formulation:
1) The login protocol is as simple as possible and contains only the required fields (such as player level, money, stamina, etc.). In general, the re-login and authentication process is required, and a streamlined login protocol can improve the efficiency of re-connection and save traffic. The user's non-critical check data (such as knapsack data, leaderboards, card information) can be delayed until the interface is opened and then request or use local cache data.
2) The decoupling of the protocol, different business logic needs the request packet, here need to do the protocol decoupling, reduce redundant data, reduce the packet volume, improve the success rate of single packet transmission.
3) Support packet compression, for larger protocol packages (such as leaderboard data, friend data, etc.) need to do targeted compression, improve the success rate of single packet delivery.
4) The key protocol needs to add serial number, avoid the client repeated requests caused by multiple charges and other issues.
6. CDN Resource Download Scheme
With the development of hardware and technology, mobile game quality and PC-side travel is getting closer, of course, the amount of resources is getting closer. Limited by the mobile network bandwidth, the larger installation package to the player set a higher threshold, so the current hand-travel products are increasingly considering the micro-end solution. That is, the installation package contains only some of the game levels, or only as a downloader, and the full game resources on the CDN resource server, and then on-demand download, which is the general page of the idea of travel. In this case, we have compared several packaging mechanisms for everyone's reference.
<todo> test data available later </todo>
Appendix 1: Wire Break re-connect flowchart
<ignore_js_op>
Appendix 2:rssi Signal Strength Grading
<ignore_js_op>
Mobile platform Game Network re-connect scheme (GO)