How do I detect a non-programmatic disconnection from the MCSession of the current device?

冷暖自知 提交于 2019-12-11 05:03:45

问题


To avoid being an XY problem, I will explain the situation first.

I am developing a game in which multiple devices can connect with each other to have a multiplayer game. I am using Multipeer Connectivity for this. The game supports 2-4 players.

Now I need to handle the case of a player disconnecting from all the other players. The game should still continue when a player disconnects, unless there is only one player left. However, I want an alert to be displayed to all the still-connected players saying "XXX has disconnected" and "kills" the player who disconnected, and another alert to the disconnected player saying that "You disconnected" and dismissing the Game View Controller.

The player can disconnect in the following ways:

  • pressing the quit button
  • turning off the screen (this makes the app goes into background which stops all Mutipeer Connectivity apparently)
  • Turning on Airplane mode
  • Getting too far away from the others

Everything apart from the first way cannot be controlled programmatically. I can currently implement the first one like this:

When the user presses the quit button, just after session.disconnect() is called, I can show the message "You disconnected". In sessionPeerDidChangeState delegate method, I can detect that a player has disconnected and I can show "XXX disconnected" alert there.

However, I cannot figure out how to handle the other cases. During my own testing, I discovered that if the user turns their screen off, the sessionPeerDidChangeState delegate method is not even called. Even if it is called, how am I supposed to figure out whether the device is the one disconnected or not? I need to know this to show an appropriate message and decide whether or not to "kill" the player who disconnected.

I understand that this is not possible with a session consisting of 2 peers, since it is impossible to determine "who disconnect from whom". The game can't continue with only 1 player anyway, so I'm just going to show "All other players have left" alert and dismiss the Game View Controller if I see that self.session.connectedPeers is empty. I'm only concerned about the case where there are 3 players and 1 of them left. I should be able to know which player it is that disconnected, but I don't know how.


回答1:


Lets say you have 3 peers connected (peer 1, peer 2 and peer 3). Lets say peer 3 turns on Airplane mode. Peer 1 and peer 2 would get a delegate call sessionPeerDidChangeState that peer 3 has disconnected. But peers 1 and 2 still have connection between themselves (so both will have self.session.connectedPeers == 1).

On the other hand peer 3 will see that both peer 1 and peer 2 disconnected one after another and self.session.connectedPeers == 0 and thus peer 3 can assume it was the one who lost the connection.

And as is noted in the question if home button is pressed than you can disconnect from the session in applicationDidEnterBackground.



来源:https://stackoverflow.com/questions/46798816/how-do-i-detect-a-non-programmatic-disconnection-from-the-mcsession-of-the-curre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!