重新连接到断开连接的对等点 [英] Reconnecting to disconnected peers

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

问题描述

我在我的应用程序中使用 iOS 7 Multipeer 框架,但我遇到了设备断开连接的问题.如果我在两个设备上打开应用程序:设备 A 和设备 B,这两个设备会自动相互连接.但是,几秒钟后,设备 A 与设备 B 断开连接.即,最初的连接是这样的:

I'm using the iOS 7 Multipeer framework in my app but I'm experiencing a problem with devices disconnecting. If I open the app in two devices: device A and device B the two devices connect to each other automatically. However, after several seconds device A disconnects from device B. i.e. At first the connection is like this:

A ---> B
A <--- B

几秒钟后:

A ---> B
A      B

设备 A 保持连接,但设备 B 获得 MCSessionStateNotConnected.

Device A maintains it's connection but device B get's a MCSessionStateNotConnected.

这意味着 A 可以向 B 发送数据,但 B 无法回复.我试图通过检查设备是否已连接来解决此问题,如果未连接,则使用以下方法重新启动连接:

This means that A can send data to B but B can't reply. I tried to get around this by checking if the device is connected and if it's not, re-initiating the connection using:

[browser invitePeer:peerID toSession:_session withContext:Nil timeout:10];

但是 didChangeState 回调只是通过 MCSessionStateNotConnected 调用.

But the didChangeState callback just get's called with MCSessionStateNotConnected.

奇怪的是,如果我将应用程序 A 发送到后台,然后重新打开它,B 重新连接到它并保持连接.

Strangely if I send app A to the background, then re-open it, B reconnects to it and the connection is maintained.

Multipeer API(和文档)似乎有点稀疏,所以我假设它可以正常工作.在这种情况下,我应该如何重新连接设备?

The Multipeer API (and documentation) seems a bit sparse so I was assuming that it would just work. In this situation how should I re-connect the device?

推荐答案

我也遇到了同样的问题,好像是我的应用浏览和广告同时发送,发送/接受了两次邀请.当我停止执行此操作并让一个对等方遵从另一个对等方的邀请时,设备保持连接.

I was having the same problem, and it seems to have been related to my app browsing and advertising at the same time, and two invitations being sent/accepted. When I stopped doing this and let one peer defer to the other for invitations the devices stayed connected.

在我的浏览器委托中,我正在检查发现的对等方 displayName 的哈希值,并且仅在我的对等方具有更高的哈希值时才发送邀请:

In my browser delegate I'm checking the hash value of the discovered peer's displayName and only sending an invitation if my peer has a higher hash value:

编辑

正如@Masa 所指出的,NSStringhash 值在 32 位和 64 位设备上会有所不同,因此使用 compare 更安全:displayName 上的 方法.

As pointed out by @Masa the hash value of an NSString will be different on 32 and 64 bit devices, so it's safer to use the compare: method on displayName.

- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info {

    NSLog(@"Browser found peer ID %@",peerID.displayName);       

    //displayName is created with [[NSUUID UUID] UUIDString]

    BOOL shouldInvite = ([_myPeerID.displayName compare:peerID.displayName]==NSOrderedDescending);

    if (shouldInvite){
        [browser invitePeer:peerID toSession:_session withContext:nil timeout:1.0]; 
    }
    else {
        NSLog(@"Not inviting");
    }
}

正如您所说,文档很少,所以谁知道 Apple 真正想让我们做什么,但我已经尝试使用单个会话发送和接受邀请,并为每个接受/发送的邀请创建一个新会话,但这种特殊的做事方式给了我最大的成功.

As you say, the documentation is sparse so who knows what Apple really wants us to do, but I've experimented with both sending and accepting invitations using a single session, and also creating a new session for each invitation accepted/sent, but this particular way of doing things has given me the most success.

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

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