XMPPFramework:我可以确定用户的在线/离线状态吗?

发布于 2024-10-17 00:07:18 字数 95 浏览 6 评论 0原文

如何使用 iPhone 版 XMPPFramework 确定用户是否在线/离线?

我有他们的 JID 等。有没有办法请求出席或其他什么?

谢谢。

How would I determine if a user is online/offline using XMPPFramework for iPhone?

I have their JID etc etc. Is there a way of requesting a presence or something?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放血 2024-10-24 00:07:18

您检查过 XMPPFramework 示例项目的源代码吗?

如果我没记错的话,这应该是相关的代码片段:

// Subscribe to the buddy's presence
// 
// <presence to="bareJID" type="subscribe"/>

NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];
[presence addAttributeWithName:@"to" stringValue:[jid bare]];
[presence addAttributeWithName:@"type" stringValue:@"subscribe"];

[xmppStream sendElement:presence];

并且您的流委托获得的回调应该是

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence;

我假设您已经拥有 xmmpframework 源,如果没有,您可以在此处克隆存储库

hg clone https://xmppframework.googlecode.com/hg/ xmppframework

示例项目位于“Xcode”中文件夹。

Have you checked out the source code of the XMPPFramework sample project?

If I remember correctly, this should be the relevant code snippet:

// Subscribe to the buddy's presence
// 
// <presence to="bareJID" type="subscribe"/>

NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];
[presence addAttributeWithName:@"to" stringValue:[jid bare]];
[presence addAttributeWithName:@"type" stringValue:@"subscribe"];

[xmppStream sendElement:presence];

And the callback your stream delegate gets should be

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence;

I assume that you already have the xmmpframework source, if not, you can clone the repository here

hg clone https://xmppframework.googlecode.com/hg/ xmppframework

The sample projects are in the "Xcode" folder.

耳钉梦 2024-10-24 00:07:18

首先,必须需要 ROSTER List 中的双方订阅才能获取用户状态。

输入图片此处描述

此处,如何订阅&接受出席请求。

    let senderName = strReceiverEjabberedName + "@" + eJabVirtualHost
    let roomJID = XMPPJID(string: senderName)
    
    // Send subscribe request
    let detxTag:XMLElement = XMLElement(name: "presence")
    detxTag.addAttribute(withName: "to", stringValue: roomJID?.bare ?? "")
    detxTag.addAttribute(withName: "type", stringValue: "subscribe")
    xmppController.xmppStream.send(detxTag)
    
    // Send subscribed request
    let detxTaged:XMLElement = XMLElement(name: "presence")
    detxTaged.addAttribute(withName: "to", stringValue: roomJID?.bare ?? "")
    detxTaged.addAttribute(withName: "type", stringValue: "subscribed")
    xmppController.xmppStream.send(detxTaged)
    
    // Subscribe & Accept Presence Request
    xmppController.xmppRoster?.subscribePresence(toUser: roomJID!)
    xmppController.xmppRoster?.acceptPresenceSubscriptionRequest(from: roomJID!, andAddToRoster: true)

如果您的朋友向您发送预授权请求,那么您会在名册代表中收到请求。

extension XMPPServiceController: XMPPRosterDelegate {
    func xmppRoster(_ sender: XMPPRoster, didReceivePresenceSubscriptionRequest presence: XMPPPresence) {
        sender.acceptPresenceSubscriptionRequest(from: presence.from!, andAddToRoster: true)
    }
}

完成上述步骤后,您将在下面的 XMPP 委托方法中收到您朋友的在线/离线状态。

func xmppStream(sender: XMPPStream?, didReceivePresence presence: XMPPPresence?) {
    let presenceType = presence?.type
    if presenceType == "available" { //ONLINE }
    else if presenceType == "unavailable" { //OFFLINE }
}

First, Both side subscription in ROSTER List is must required for getting user status.

enter image description here

Here, How to subscribe & Accept a Presence Request.

    let senderName = strReceiverEjabberedName + "@" + eJabVirtualHost
    let roomJID = XMPPJID(string: senderName)
    
    // Send subscribe request
    let detxTag:XMLElement = XMLElement(name: "presence")
    detxTag.addAttribute(withName: "to", stringValue: roomJID?.bare ?? "")
    detxTag.addAttribute(withName: "type", stringValue: "subscribe")
    xmppController.xmppStream.send(detxTag)
    
    // Send subscribed request
    let detxTaged:XMLElement = XMLElement(name: "presence")
    detxTaged.addAttribute(withName: "to", stringValue: roomJID?.bare ?? "")
    detxTaged.addAttribute(withName: "type", stringValue: "subscribed")
    xmppController.xmppStream.send(detxTaged)
    
    // Subscribe & Accept Presence Request
    xmppController.xmppRoster?.subscribePresence(toUser: roomJID!)
    xmppController.xmppRoster?.acceptPresenceSubscriptionRequest(from: roomJID!, andAddToRoster: true)

If your friend send precense request to you then you received request in Roster Delegate.

extension XMPPServiceController: XMPPRosterDelegate {
    func xmppRoster(_ sender: XMPPRoster, didReceivePresenceSubscriptionRequest presence: XMPPPresence) {
        sender.acceptPresenceSubscriptionRequest(from: presence.from!, andAddToRoster: true)
    }
}

Once the above steps did you will receive your friend Online/Offline Status in the below XMPP delegate method.

func xmppStream(sender: XMPPStream?, didReceivePresence presence: XMPPPresence?) {
    let presenceType = presence?.type
    if presenceType == "available" { //ONLINE }
    else if presenceType == "unavailable" { //OFFLINE }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文