gpt4 book ai didi

iOS Pusher 客户端不会自动重新连接

转载 作者:行者123 更新时间:2023-11-30 14:01:00 26 4
gpt4 key购买 nike

我正在使用适用于 iOS 的 Pusher 客户端,通过 CocoaPods 安装它(pod 'libPusher', '~> 1.5')。

一切都安排得很好,事件进展顺利。但是,如果设备(运行 iOS 9.0.2 的 iPhone 6s)失去互联网连接(由于我进入飞行模式而导致),然后在一分钟后重新获得互联网连接(我从飞行模式中退出),Pusher 就永远不会重新获得连接。

我添加了一些 UIAlertViews 来测试它基于它的委托(delegate)方法正在做什么。

最初显示 connectionWillConnectconnectionDidConnect

打开飞行模式时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
}

}

关于为什么它不起作用有什么想法吗?

所有想法和理论将不胜感激!谢谢:)

最佳答案

对于我来说,Swift 也不起作用。我安装了swift implementation Pusher 现在工作正常。

    let pusher = Pusher(key: "c984997151153c177dc2")
pusher.connect()

let channel = pusher.subscribe("test_channel")
channel.bind("my_event") { (event: AnyObject?) -> Void in
print("my_event push received ")
}

关于iOS Pusher 客户端不会自动重新连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33000301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com