xmppframework 的 didReceive 存在无法正常工作
xmppframework's didReceive presence is not working properly
我正在使用 func xmppStream(_ sender: XMPPStream, didReceive presence: XMPPPresence)
管理在线离线 status.This 功能对一对一用户工作正常,但对多个用户无法正常工作。
例如,我有 3 个用户 A、B 和 C,现在情况是在线离线状态对于 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)
)
如果你想检查特定用户的存在,使用这个。
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())
}
}
我已经通过添加以下代码解决了我的问题
let userReceiverJID = XMPPJID(string: "\(receiverJID)@\(CHATHOST)")
APP_DELEGATE.xmppRoster.acceptPresenceSubscriptionRequest(from: userReceiverJID!, andAddToRoster: true)
APP_DELEGATE.xmppRoster.subscribePresence(toUser: userReceiverJID!)
我正在使用 func xmppStream(_ sender: XMPPStream, didReceive presence: XMPPPresence)
管理在线离线 status.This 功能对一对一用户工作正常,但对多个用户无法正常工作。
例如,我有 3 个用户 A、B 和 C,现在情况是在线离线状态对于 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)
)
如果你想检查特定用户的存在,使用这个。
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())
}
}
我已经通过添加以下代码解决了我的问题
let userReceiverJID = XMPPJID(string: "\(receiverJID)@\(CHATHOST)")
APP_DELEGATE.xmppRoster.acceptPresenceSubscriptionRequest(from: userReceiverJID!, andAddToRoster: true)
APP_DELEGATE.xmppRoster.subscribePresence(toUser: userReceiverJID!)