iOS - Pubnub SDK遇到延迟以获得发布回调

时间:2017-11-01 13:05:51

标签: ios swift delay publish-subscribe pubnub

我创建了一个使用swift发布和订阅实时消息的示例iOS应用程序,使用了Pubnub SDK。发布设备还订阅了发布渠道。我正面临一些延迟问题(大约1.5秒),以便在订阅者回调上获取已发布的消息。此延迟仅在两种情况下发生。

  1. 连接pubnub客户端后发布第一条消息。
  2. 在空闲时间超过20秒后发布新消息。(空闲时间表示我在此期间不发布任何消息)。
  3. 这两种情况我都有1.5秒的延迟。 但是,如果我不断发布消息,则没有延迟问题,我的延迟时间仅为0.2秒。

    我正在使用Pubnub SDK版本 - 4.7.2,并在运行版本为9及更高版本的iOS设备上运行。

    在印度和澳大利亚进行测试时遇到了这个问题,我没有机会在其他国家进行测试。

    我正在使用pubnub客户端附加我的代码,以及我如何发布消息和订阅频道。

    fileprivate let publishKey = "pub-c-2ca0-redacted"
    fileprivate let subscribeKey = "sub-c-91bd-redacted"
    
    extension Date {
    
        static var formatter: DateFormatter = {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "HH:mm:ss.SSSSz"
            return dateFormatter
        } ()
    
        var stringFormat: String {
            return Date.formatter.string(from: self)
        }
    
    }
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var textField: UITextField!
        @IBOutlet weak var label: UILabel!
        var client : PubNub?
        let publishQueue = DispatchQueue(label: "Publish Queue", qos: .userInitiated, attributes: [.concurrent])
        let callbackQueue = DispatchQueue(label: "PubNubCallbackQueue", qos: .userInitiated, attributes: [.concurrent])
    
    
    
        @IBAction func publish(_ sender: Any) {
            // We NEED to get the text field contents in the main thread or we violate UIKit API
            let publishStep0 = Date()
            DispatchQueue.main.async {
                let publishStep1 = Date()
                let publishText = self.textField.text ?? "default"
                self.publishQueue.async {
                    let publishStep2 = Date()
                    self.client?.publish(publishText, toChannel: "my_channel1",
                                         compressed: false, withCompletion: { (status) in
                                            let publishStep3 = Date()
                                            print("****** \(#function) publish steps ******")
                                            print("Step 1: \(#function) => Tap Button: \(publishStep0.stringFormat)")
                                            print("Step 1: \(#function) => Get Text from TextField \(publishStep1.stringFormat)")
                                            print("Step 2: \(#function) => Initiate Publish \(publishStep2.stringFormat)")
                                            print("Step 3: \(#function) => Receive Publish Callback \(publishStep3.stringFormat)")
                                            print("****************************************")
                                            if !status.isError {
                                            }
                                            else{
    
                                            }
                    })
                }
            }
    
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            let config = PNConfiguration(publishKey: publishKey, subscribeKey: subscribeKey)
            config.stripMobilePayload = false
            self.client = PubNub.clientWithConfiguration(config, callbackQueue: callbackQueue)
            self.client?.logger.enabled = true
            self.client?.logger.setLogLevel(PNLogLevel.PNVerboseLogLevel.rawValue)
            // optionally add the app delegate as a listener, or anything else
            // View Controllers should get the client from the App Delegate
            // and add themselves as listeners if they are interested in
            // stream events (subscribe, presence, status)
            self.client?.addListener(self)
            self.client?.subscribeToChannels(["my_channel1"], withPresence: false)
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    }
    
    // MARK: - PNObjectEventListener
    extension ViewController: PNObjectEventListener {
    
        func client(_ client: PubNub, didReceive status: PNStatus) {
    
            print("Status \(status.stringifiedCategory()) at time: \(Date().stringFormat)")
        }
    
        func client(_ client: PubNub, didReceiveMessage message: PNMessageResult) {
    
            DispatchQueue.main.async {
                print("$$$$$$$$$$$$$$$$ Message received $$$$$$$$$$$$$$$$")
                print("message: \(message.debugDescription)")
                print("time: \(Date().stringFormat)")
                print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
    //            self.label.text = self.label.text?.appending(",\(message.data.message!) ") // this is bad because there might not initially be text, what is the behavior of appending when the label has no text?
                let currentText = self.label.text ?? ""
                let appendingText = message.data.message ?? "No text in message"
                self.label.text = currentText.appending(",\(appendingText)")
                self.label.setNeedsLayout() // don't forget to tell the view engine to update the label
            }
    
        }
    
        func client(_ client: PubNub, didReceivePresenceEvent event: PNPresenceEventResult) {
            // This most likely won't be used here, but in any relevant view controllers
        }
    }
    

0 个答案:

没有答案