我的应用程序使用 GKSession 和 GKSessionModePeer。它必须处理任意连接和断开的对等点,因为这是一个长时间运行的应用程序,用户应该能够进入后台并稍后再回来。这在大多数情况下都可以正常工作。但有时,当对等点断开连接时,其他设备会收到 didChangeState:GKPeerStateDisconnected 通知,不仅对于真正断开连接的设备,而且对于实际仍然连接的其他设备。
我可以使用下面的代码和 4 台设备(全部在 iOS 5 上)重现此行为。当一切按预期进行时,当设备 A 退出应用程序时,所有其他设备都会收到通知,并且这些设备上的日志输出为:
Service: didChangeState: peer A disconnected (12345)
但是一段时间后,当一个设备断开连接时(再说一次 A),其他设备会为没有断开连接的设备获得额外的回调。例如,设备 C 将获得:
Service: didChangeState: peer A disconnected (...) // expected
Service: didChangeState: peer B disconnected (...) // never disconnected
大约在同一时间,我有时会在断开设备的日志中看到这些消息,不清楚它们是否真的相关:
dnssd_clientstub DNSServiceRefDeallocate called with NULL DNSServiceRef
和/或
dnssd_clientstub DNSServiceProcessResult called with DNSServiceRef with no ProcessReply function
一旦发生这种情况,GKSession 似乎处于不良状态,不再正确处理连接和断开连接。为了恢复良好状态,我必须在所有设备上硬杀应用程序,稍等片刻,然后重新开始。
在进入后台时,我尝试了不同的处理 GKSession 的方法(仅设置 available=NO 并且不断开连接,根本不做任何事情),但没有一个效果更好。
有没有其他人遇到过这种行为(并解决了它)?
AppDelegate 中的简单复制案例(使用 arc):
- (void)startGKSession
{
self.gkSession = [[GKSession alloc] initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer];
gkSession.disconnectTimeout = 10;
gkSession.delegate = self;
gkSession.available = YES;
}
- (void)shutdownGKSession
{
gkSession.available = NO;
[gkSession disconnectFromAllPeers];
gkSession.delegate = nil;
gkSession = nil;
[self.connectedDevices removeAllObjects];
}
- (void)connectToPeer:(NSString *)peerId
{
[gkSession connectToPeer:peerId withTimeout:10];
}
- (void)session:(GKSession *)session peer:(NSString *)peerId didChangeState:(GKPeerConnectionState)state
{
switch (state) {
case GKPeerStateAvailable:
NSLog(@"Service: didChangeState: peer %@ available, connecting (%@)", [session displayNameForPeer:peerId], peerId);
[self performSelector:@selector(connectToPeer:) withObject:peerId afterDelay:.5];
break;
case GKPeerStateUnavailable:
NSLog(@"Service: didChangeState: peer %@ unavailable (%@)", [session displayNameForPeer:peerId], peerId);
break;
case GKPeerStateConnected:
NSLog(@"Service: didChangeState: peer %@ connected (%@)", [session displayNameForPeer:peerId], peerId);
break;
case GKPeerStateDisconnected:
NSLog(@"Service: didChangeState: peer %@ disconnected (%@)", [session displayNameForPeer:peerId], peerId);
break;
case GKPeerStateConnecting:
NSLog(@"Service: didChangeState: peer %@ connecting (%@)", [session displayNameForPeer:peerId], peerId);
break;
}
}
- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID
{
[session acceptConnectionFromPeer:peerID error:nil];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.connectedDevices = [[NSMutableArray alloc] init];
[self startGKSession];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self shutdownGKSession];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[self startGKSession];
}
@end