如何:在 GKSession 中实现服务器强制断开客户端连接

发布于 2024-11-02 06:44:33 字数 334 浏览 4 评论 0原文

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

有什么建议吗?

谢谢

I am implementing the GKSession server/client mode operation in my application on iOS. I found one question related to mine but with no answer. I am trying to allow the server to disconnect any client that is currently connected to the session. I thought that calling disconnectPeerFromAllPeers:(NSString *)peerID would allow me to do this, but is seems to have no effect.

Any suggestions?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永不分离 2024-11-09 06:44:33

实际上是通过问题更新离子 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;
}

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

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

Actually answered via question update ion 01/03/2012, but moved this text to the answer section

I wanted to share how I implemented a disconnect request sent from server to client. All of the code presented below is contained within a class I created to completely encapsulate all the interfacing with a GKSession instance (also implements the GKSessionDelegate methods).

First I have the server send a disconnect request to the client that shall be disconneted. Any data that is sent from client to server or vice versa is contained within a dictionary that also has a key-value pair to specify the type of data that is sent (in this case the data is a disconnect request).

- (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];

}

The client receives the data, casts it into a dictionary and examines the key-value pair that specifies what type of data was sent. If it's a disconnect request, my "GKSessionManager" class then implements a disconnect.

- (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;
}

The user never sees the disconnect request and so has no control over whether or not to deny it.

Hope this information helps. I realize what I wrote my not be entirely clear and I have left a lot of other code out (on purpose) so feel free to comment or ask questions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文