简体   繁体   English

如何检测与当前设备的MCSession的非编程断开连接?

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

To avoid being an XY problem, I will explain the situation first. 为了避免成为XY问题,我将首先说明情况。

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. 我为此使用Multipeer Connectivity。 The game supports 2-4 players. 游戏支持2-4位玩家。

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. 但是,我希望向所有仍在连接的玩家显示警报,说“ XXX已断开连接”并“杀死”断开连接的玩家,并向未连接的玩家显示另一个警报,提示“您已断开连接”并关闭游戏视图控制器。

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) 关闭屏幕(这会使应用程序进入后台,从而明显停止所有Mutipeer连接)
  • 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". 当用户按下退出按钮时,在调用session.disconnect()之后,我可以显示消息“您已断开连接”。 In sessionPeerDidChangeState delegate method, I can detect that a player has disconnected and I can show "XXX disconnected" alert there. sessionPeerDidChangeState委托方法中,我可以检测到玩家已断开连接,并且可以在其中显示“ XXX断开连接”警报。

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. 在我自己的测试中,我发现如果用户关闭屏幕,则甚至不会调用sessionPeerDidChangeState委托方法。 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". 我知道对于由2个对等方组成的会话是不可能的,因为无法确定“谁与谁断开连接”。 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. 无论如何,游戏不能只允许一个玩家继续游戏,因此如果我发现self.session.connectedPeers为空,我将仅显示“所有其他玩家都已离开”警报并关闭游戏视图控制器。 I'm only concerned about the case where there are 3 players and 1 of them left. 我只担心剩下3名球员,剩下1名的情况。 I should be able to know which player it is that disconnected, but I don't know how. 我应该能够知道与哪个玩家断开连接,但是我不知道如何。

Lets say you have 3 peers connected (peer 1, peer 2 and peer 3). 假设您有3个对等连接(对等1,对等2和对等3)。 Lets say peer 3 turns on Airplane mode. 假设对等3打开飞行模式。 Peer 1 and peer 2 would get a delegate call sessionPeerDidChangeState that peer 3 has disconnected. 对等1和对等2将获得一个委托呼叫sessionPeerDidChangeState ,该对等3已断开连接。 But peers 1 and 2 still have connection between themselves (so both will have self.session.connectedPeers == 1 ). 但是对等方1和2之间仍然具有连接(因此,两者都将具有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. 另一方面,对等体3将看到对等体1和对等体2都彼此断开连接,并且self.session.connectedPeers == 0 ,因此对等体3可以假定它是丢失连接的那个。

And as is noted in the question if home button is pressed than you can disconnect from the session in applicationDidEnterBackground . 正如在问题中指出的那样,如果按下主页按钮,则可以从applicationDidEnterBackground的会话断开连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM