【问题标题】:iOS Pusher client not automatically reconnectingiOS Pusher 客户端不会自动重新连接
【发布时间】:2015-10-07 18:58:19
【问题描述】:

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

一切正常,事件顺利进行。但是,如果设备(运行 iOS 9.0.2 的 iPhone 6s)失去互联网连接(由于我进入飞行模式)然后在一分钟后重新连接(我退出飞行模式),Pusher 永远不会重新连接。

我添加了一些 UIAlertViews 来根据它的委托方法测试它在做什么。

最初会显示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
    }

}

关于它为什么不起作用的任何想法?

非常感谢所有想法和理论!谢谢你:)

【问题讨论】:

    标签: ios swift pusher


    【解决方案1】:

    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 ")
        }
    

    【讨论】:

    • 这对我们不起作用,我们在应用程序内连接了 Pusher,我们留在应用程序中并与互联网断开连接,因此 Pusher 也断开连接,然后我们连接到互联网但 Pusher 仍然断开连接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 2015-03-10
    • 1970-01-01
    相关资源
    最近更新 更多