ios - 如何正确拆除多点连接 session ?

标签 ios objective-c multipeer-connectivity

我认为多点连接有时会出现重新连接问题。每当我尝试重新连接到刚刚断开连接的客户端时,它都无法向客户端发送新邀请。我不确定与连接的对等点断开连接的正确方法是什么,以及如果应用程序因处于挂起模式而被终止,如何与所有连接的对等点断开连接。谁能帮我解决这个问题吗?

例如:

如果我连接了 A 和 B,并且 B 超出了范围,则 session 会因无法访问而自动断开连接

- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state {
    case MCSessionStateNotConnected:
}

被调用,我相信此时双方都知道他们已经彼此断开连接,但是,当B返回并想要重新连接时——邀请已发送,但另一方从未收到。究竟发生了什么?我是否必须手动断开 session 才能防止这种情况发生?

最佳答案

每当我断开连接并想要重新连接时,我都会在浏览器端重置 PeerID 和 session :

    // reset my PeerID.  Sometimes when trying to reconnect to the same Advertiser with the same PeerID nothing happens
    mcPeerID = [[MCPeerID alloc] initWithDisplayName:[UIDevice currentDevice].name];
    mcSession = [[MCSession alloc] initWithPeer:mcPeerID];
    mcSession.delegate = self;

    mcNearbyServiceBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:mcPeerID serviceType:kMCServiceType];
    mcNearbyServiceBrowser.delegate = self;
    [mcNearbyServiceBrowser startBrowsingForPeers];

在广告商方面,connectedPeers 计数在断开连接时正确减少,因此作为 * 不同的 * 浏览器重新连接似乎不会产生负面影响(也许这不是正确的编码,但它有效)。有了新的 PeerID,我可以毫无问题地重新连接。我同时重置 session 以保持它们同步(即,我不希望 session 从旧的 PeerID 初始化)。

关于ios - 如何正确拆除多点连接 session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23277526/

相关文章:

ios - 共享我编辑的 UIImage 时 UIActivityViewController 崩溃,但适用于 UIImage(名为 :)

ios - 从 UITableView 中删除行时删除 UITextField 内容

ios - 魔法记录: nil is not a legal NSPersistentStoreCoordinator

iOS 基于位置的点对点通信方法

ios - Multipeer 连接范围和非 ios 设备

ios - 在 UITextView 中使用属性文本

ios - 为什么 block 类型在数组中不同?

objective-c - 如果将自动释放对象标记为自动释放会发生什么

ios - 如何在 Swift 中使用 dismissViewControllerAnimated 中的完成处理程序?

swift - 通过多个 View Controller 的多点连接