多路连接断开连接 [英] Multipeer Connectivity Disconnect

查看:168
本文介绍了多路连接断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用iOs7中的Multipeer Connectivity Framework保持联系时遇到了麻烦。目前,我的应用程序使用MCNearbyServiceAdvertiser和MCNearbyServiceBrowser以编程方式处理浏览和广告。我有一个警报视图,询问用户他是浏览器还是广告商。在从该视图返回时,我相应地实例化MCNearbyServiceAdvertiser或Browser。

I'm having trouble with staying connected using the Multipeer Connectivity Framework in iOs7. Currently my app is programmatically handling the browsing and advertising using MCNearbyServiceAdvertiser and MCNearbyServiceBrowser. I have an alert view that asks the user if he is a browser or an advertiser. On the return from that view I instantiate either an MCNearbyServiceAdvertiser or Browser accordingly.

#pragma - Alert Delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        _browser = [[MCNearbyServiceBrowser alloc]initWithPeer:_peerID serviceType:@"Context-xl"];
        [_browser setDelegate:self];
        [self.detailViewController setRemote:YES];
        [_browser startBrowsingForPeers];
    } else
    {
        _advertiser = [[MCNearbyServiceAdvertiser alloc]initWithPeer:_peerID discoveryInfo:nil serviceType:@"Context-xl"];
        [_advertiser setDelegate:self];
        [self.detailViewController setRemote:NO];
        [_advertiser startAdvertisingPeer];
    }
    [self.detailViewController configureView];
}

我的会话委托方法peer:... DidChangeState ...正在调用两次,一次用于连接,另一次用于断开连接。会话开始后,我根本不会停止广告客户或浏览器。我应该停止浏览/广告吗?

My session delegate method peer:...DidChangeState... is getting called twice, once for the connect and again for the disconnect. I'm not stopping the advertiser or browser at all after the session is started. Should I stop browsing/advertising?

推荐答案

编辑与Apple一起使用支持票,他们确认调用带有太多数据的sendData或太经常会导致断开连接。

EDIT Used a support ticket with Apple and they confirmed that calling sendData with too much data or too often can cause disconnects.

编辑我的假设是Apple有一个线程或队列轮询以检查对等体是否已连接。如果这个线程/队列停止(即一个断点被击中或者应用程序挂在CPU上或在主线程上做了一些事情),那么这似乎会导致断开连接。

EDIT My hypothesis is that Apple has a thread or queue that is polling to check if peers are connected. If this thread / queue stalls (i.e. a breakpoint is hit or the app pegs the CPU or does something that takes a while on the main thread) it appears that this causes a disconnect.

创建没有加密的会话似乎有助于提高性能和断开连接,尽管它们仍然存在。

Creating my session without encryption seems to have helped performance and with the disconnects, although they still happen.

MCPeerID* peerId = [[MCPeerID alloc] initWithDisplayName:self.displayName];
self.peer = [[MultiPeerPeer alloc] initWithDisplayName:peerId.displayName andPeer:peerId];
self.session = [[MCSession alloc] initWithPeer:peerId securityIdentity:nil encryptionPreference:MCEncryptionNone];

此外,我发现调用sendData的频率太高(每秒超过30-60次)导致框架处于错误状态并导致冻结和断开连接。

In addition, I have found calling sendData too often (more than 30-60 times a second) can cause the framework to get in a bad state and cause freezes and disconnects.

这篇关于多路连接断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆