xmppframework的didReceive存在无法正常工作

时间:2018-11-27 11:15:58

标签: swift offline xmppframework

我正在使用func xmppStream(_ sender: XMPPStream, didReceive presence: XMPPPresence)来管理在线脱机状态。此功能对一对一用户而言效果很好,但对多用户而言则无法正常工作。

例如,我有3个用户A,B和C,现在的场景是Online离线状态对于A到B和B到A正常工作,因为func xmppStream(_ sender: XMPPStream, didReceive presence: XMPPPresence)函数调用得很好。但是如果C用户检查A或B的状态,则不会调用func xmppStream(_ sender: XMPPStream, didReceive presence: XMPPPresence)

我将无法理解问题所在。这是我的代码:

func xmppStream(_ sender: XMPPStream, didReceive presence: XMPPPresence) {
    let presenceType = presence.type
    let myUsername = sender.myJID?.user

    print("myusername \(myUsername)")

    if let usr = presence.from?.user {

        print("didReceive :- \(presenceType)")
        print("didReceive usr :- \(usr)")

        if usr != myUsername {
            if presenceType == "available" {
                let dict: NSDictionary = ["presenceType":"1",
                                          "user_id":usr]
                if !APP_DELEGATE.arrPresentUser.contains(dict) {
                    APP_DELEGATE.arrPresentUser.add(dict)
                }
            } else {
                let pred : NSPredicate = NSPredicate(format: "user_id = %@", usr)
                let approvalRequestArray =  APP_DELEGATE.arrPresentUser.filtered(using: pred) as NSArray
                if approvalRequestArray.count > 0 {
                    let dict = approvalRequestArray.object(at: 0) as! NSDictionary
                    APP_DELEGATE.arrPresentUser.remove(dict)
                }
            }
            NotificationCenter.default.post(name: Notification.Name("ReceivedUserStatus"), object: nil)
        }
    }
}

注意:我还尝试过使用 @objc @objc func xmppStream(_ sender: XMPPStream, didReceive presence: XMPPPresence)

2 个答案:

答案 0 :(得分:2)

如果要检查特定用户的存在,请使用它。

  let query = XMLElement(name: "query", xmlns: "jabber:iq:last")
  let iq = XMPPIQ(type: "get", to: "User's JID here", elementID: xmppStream.generateUUID, child: query)
  self.xmppStream.send(iq)

获取委托方法中的状态。

  func xmppStream(_ sender: XMPPStream, didReceive iq: XMPPIQ) -> Bool {
    if iq.isResultIQ {
      print(iq.from?.user)
      print(iq.lastActivitySeconds())
    }
  }

答案 1 :(得分:1)

我通过添加以下代码解决了我的问题

    let userReceiverJID = XMPPJID(string: "\(receiverJID)@\(CHATHOST)")

    APP_DELEGATE.xmppRoster.acceptPresenceSubscriptionRequest(from: userReceiverJID!, andAddToRoster: true)
    APP_DELEGATE.xmppRoster.subscribePresence(toUser: userReceiverJID!)