1

我正在 iOS 上的应用程序中实现 GKSession 服务器/客户端模式操作。我发现了一个与我相关的问题,但没有答案。我试图让服务器断开当前连接到会话的任何客户端。我认为调用 disconnectPeerFromAllPeers:(NSString *)peerID 可以让我这样做,但似乎没有效果。

有什么建议么?

谢谢

4

1 回答 1

0

实际上是通过问题更新 ion 01/03/2012 回答的,但将此文本移至答案部分

我想分享我如何实现从服务器发送到客户端的断开连接请求。下面提供的所有代码都包含在我创建的一个类中,该类完全封装了与 GKSession 实例的所有接口(也实现了 GKSessionDelegate 方法)。

首先,我让服务器向应断开连接的客户端发送断开连接请求。任何从客户端发送到服务器或反之亦然的数据都包含在一个字典中,该字典也有一个键值对来指定发送的数据类型(在这种情况下,数据是断开连接请求)。

- (void)sendDisconnectRequestToPeer:(NSString *)peer {

//create the data dictionary that includes the disconnect value for the data type key
NSMutableDictionary *dictPrvw = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:GKSessionDataTypeDisconnect], kDictKeyDataType, nil];

NSData *dataChunk = [[NSKeyedArchiver archivedDataWithRootObject:dictPrvw] retain];
//[self printDict:dictPrvw];

NSArray *peers = [[NSArray alloc] initWithObjects:peer, nil];

[self sendData:dataChunk toPeers:peers];

[dataChunk release];
[dictPrvw release];

}

客户端接收数据,将其转换为字典并检查指定发送的数据类型的键值对。如果是断开连接请求,我的“GKSessionManager”类就会实现断开连接。

- (void)recievedAllDataChunksInSession:(GKSession *)session fromPeer:(NSString *)peer context:(void *)context {

//The chunk was packaged by the other user using an NSKeyedArchiver, 
//so unpackage it here with our NSKeyedUnArchiver
NSMutableDictionary *responseDictionary = (NSMutableDictionary *)[[NSKeyedUnarchiver unarchiveObjectWithData:self.recievedPackets] mutableCopyWithZone:NULL];

//[self printDict:responseDictionary];

//get the enumerator value for the data type
NSNumber *gkSessDataType = [responseDictionary objectForKey:kDictKeyDataType];
int intDataType = [gkSessDataType intValue];

UIAlertView *anAlrtVw;

switch (intDataType) {
    case GKSessionDataTypeMessageData:
        [self sessionManager:self recievedDataDictionary:responseDictionary];
        break;

    case GKSessionDataTypePreviewRequest:
        if (sess.sessionMode == GKSessionModeServer) {
            [self sendMsgPreviewToPeer:peer];
        }
        break;

    case GKSessionDataTypePreviewSend:
        //[self sessionManager:self recievedDataDictionary:responseDictionary];
        [self sessionManager:self connectedWithPrelimData:responseDictionary];
        break;

    case GKSessionDataTypeDisconnect:
        anAlrtVw = [[UIAlertView alloc] 
                    initWithTitle:nil 
                    message:@"The server has disconnect you." 
                    delegate:self cancelButtonTitle:@"OK" 
                    otherButtonTitles:nil];
        [anAlrtVw show];
        [anAlrtVw release];
        [self closeSession];
        [self disconnectedByServer];

    default:
        break;

}
}


- (void)closeSession {
  [sess disconnectFromAllPeers];
  [sess setDataReceiveHandler: nil withContext: NULL];
  sess.available = NO; 
  sess.delegate = nil;
  self.sess = nil;
  self.serverId = nil;
  self.rqstPeerId = nil;
  serverIsConnecting = NO;
}

用户永远不会看到断开连接请求,因此无法控制是否拒绝它。

希望这些信息有所帮助。我意识到我写的内容并不完全清楚,并且我(故意)遗漏了许多其他代码,因此请随时发表评论或提出问题。

于 2018-04-13T16:19:42.813 回答