【发布时间】:2015-10-07 18:58:19
【问题描述】:
我正在使用 iOS 的 Pusher 客户端,通过 CocoaPods (pod 'libPusher', '~> 1.5') 安装它。
一切正常,事件顺利进行。但是,如果设备(运行 iOS 9.0.2 的 iPhone 6s)失去互联网连接(由于我进入飞行模式)然后在一分钟后重新连接(我退出飞行模式),Pusher 永远不会重新连接。
我添加了一些 UIAlertViews 来根据它的委托方法测试它在做什么。
最初会显示connectionWillConnect 和connectionDidConnect。
当connectionWillConnect开启飞行模式时,会显示connectionWillAutomaticallyReconnection afterDelay of 0.0。
(然后在没有互联网的情况下离开了一分钟左右。)
然后什么都没有,即使在连接回互联网之后。并且不再正确接收事件。
-
这是我用于所有 Pusher 的类(用 Swift 2.0 编写),它在连接丢失之前运行良好。
class PusherInterface: NSObject, PTPusherDelegate {
// MARK: - PusherInterface Shared Instance
/// Singleton instance of the PusherInterface class.
private static let sharedInstance = PusherInterface()
// MARK: - Pusher Credentials
private static let pusherAppId = "MY_APP_ID"
private static let pusherKey = "MY_KEY"
private static let pusherSecret = "MY_SECRET"
/// The connected client used by Pusher to connect to event channels
private static var client: PTPusher = {
let pusherClient = PTPusher.pusherWithKey(pusherKey, delegate: PusherInterface.sharedInstance)
pusherClient.connect()
return pusherClient as! PTPusher
}()
// MARK: - Setup Pusher
static func startListening() {
client.subscribeToChannelNamed("MY_CHANNEL")
client.bindToEventNamed("MY_EVENT") { pusherEvent in
// Does some stuff with the data back
}
}
// MARK: - Pusher Delegate
func pusher(pusher: PTPusher!, connectionDidConnect connection: PTPusherConnection!) {
NSOperationQueue.mainQueue().addOperationWithBlock {
UIAlertView(title: "connectionDidConnect", message: "", delegate: nil, cancelButtonTitle: "Dismiss").show()
}
}
func pusher(pusher: PTPusher!, connectionWillAutomaticallyReconnect connection: PTPusherConnection!, afterDelay delay: NSTimeInterval) -> Bool {
NSOperationQueue.mainQueue().addOperationWithBlock {
UIAlertView(title: "connectionWillAutomaticallyReconnect", message: "afterDelay \(delay)", delegate: nil, cancelButtonTitle: "Dismiss").show()
}
return true
}
func pusher(pusher: PTPusher!, connectionWillConnect connection: PTPusherConnection!) -> Bool {
NSOperationQueue.mainQueue().addOperationWithBlock {
UIAlertView(title: "connectionWillConnect", message: "", delegate: nil, cancelButtonTitle: "Dismiss").show()
}
return true
}
}
关于它为什么不起作用的任何想法?
非常感谢所有想法和理论!谢谢你:)
【问题讨论】: