2

我有一个使用在 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 的提及。我们将不胜感激地收到任何帮助。

4

0 回答 0