我有一个应用程序,它使用在GKSessionModeServer模式下运行的GKSession进行本地网络连接。这是有限的,因此使用此应用程序时只能连接两个设备。
只要设备不相互断开连接,所有连接和通信代码都会按预期工作。我希望设备在发生中断时自动重新连接。我一直在研究代码,以便设备在发生断开事件后自动重新连接。
我可以在模拟器上同时运行该应用程序&设备,如果我在模拟器上暂停应用程序,它会完全按照我的预期重新连接(无论是作为服务器还是客户端运行),但是如果我在真实设备上挂起应用程序,则设备上的代码都不起作用。
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
{
NSLog(@"Game: peer %@ changed state %d", peerID, state);
if (state == GKPeerStateDisconnected) {
if (_isServer) {
NSLog(@"Switching session ON");
_session.available = YES;
} else {
NSLog(@"Running a reconnection request");
[_session connectToPeer:peerID withTimeout:_session.disconnectTimeout];
}
[self.delegate bridgeHandDelegateShowDisconnectedScreen:self];
} else if (state == GKPeerStateConnected) {
if (_isServer) {
NSLog(@"Switching Session off");
_session.available = NO;
}
[self.delegate bridgeHandDelegateHideDisconnectedScreen:self];
}
if (_isServer) {
NSLog(@"session state: %d", _session.available);
}
}
正如您所看到的,我已经被NSLog语句的代码所困扰,当我挂起主机的模拟器上的输出是:
模拟器:
2013-03-22 18:28:55.359 Practice[56120:c07] Game: peer 587373189 changed state 3
2013-03-22 18:28:55.359 Practice[56120:c07] Switching session ON
2013-03-22 18:28:55.364 Practice[56120:c07] session state: 1
2013-03-22 18:28:56.563 Practice[56120:c07] Game: peer 587373189 changed state 4
2013-03-22 18:28:56.563 Practice[56120:c07] session state: 1
2013-03-22 18:28:56.564 Practice[56120:c07] Game: connection request from peer 587373189
2013-03-22 18:28:56.565 Practice[56120:c07] MatchmakingServer: Connection accepted from peer: 587373189
2013-03-22 18:28:56.579 Practice[56120:c07] Game: peer 587373189 changed state 2
2013-03-22 18:28:56.580 Practice[56120:c07] Switching Session off
2013-03-22 18:28:56.582 Practice[56120:c07] session state: 0
设备:
2013-03-22 22:24:04.922 BridgeBiddingPractice[3739:907] Game: peer 865951788 changed state 3
2013-03-22 22:24:04.924 BridgeBiddingPractice[3739:907] Switching session ON
2013-03-22 22:24:04.938 BridgeBiddingPractice[3739:907] session state: 0
2013-03-22 22:24:04.942 BridgeBiddingPractice[3739:907] Game: session failed Error Domain=com.apple.gamekit.GKSessionErrorDomain Code=-65540 "The operation couldn’t be completed. (com.apple.gamekit.GKSessionErrorDomain error -65540.)"
我找不到任何提及65540的错误。感谢任何帮助。