我试图围绕这样一个事实进行编码,即在设备使用音频(或 Android 的 Earshot)超出范围后,messageLostHandler 不会触发几分钟。
我希望每隔几秒钟就会从另一台设备收到一条消息。它会触发一次。这是预期的吗?由于我不能依赖 messageLost 处理程序 - 我如何知道设备何时真正超出超声波的范围?
我在收到 subscriptionWithMessageFoundHandler 后编写了一个计时器,希望另一条消息进来我可以使计时器无效或重新启动。如果计时器触发,我会知道 x 秒过去了,并且其他设备必须超出范围。没有这样的运气。
更新:这是有问题的代码:
let strategy = GNSStrategy.init(paramsBlock: { (params: GNSStrategyParams!) -> Void in
params.discoveryMediums = .Audio
})
publication = messageMgr.publicationWithMessage(pubMessage, paramsBlock: { (pubParams: GNSPublicationParams!) in
pubParams.strategy = strategy
})
subscription = messageMgr.subscriptionWithMessageFoundHandler({[unowned self] (message: GNSMessage!) -> Void in
self.messageViewController.addMessage(String(data: message.content, encoding:NSUTF8StringEncoding))
// We only seem to get a 1x notification of a message. So this timer is folly.
print("PING") //Only 1x per discovery.
}, messageLostHandler: {[unowned self](message: GNSMessage!) -> Void in
self.messageViewController.removeMessage(String(data: message.content, encoding: NSUTF8StringEncoding))
}, paramsBlock: { (subParams: GNSSubscriptionParams!) -> Void in
subParams.strategy = strategy
})
请注意,“PING”只打印一次。