最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[整理]XMPP中Presence的Type的含义和用法

XMPP crifan 4360浏览 0评论
swift的XMPP库SwiftXMPP中的代码:
    func goOnline() {
        print("goOnline")
        var presence = XMPPPresence(type: "away")
        curXmppStream!.sendElement(presence)
    }
看起来很可疑啊:
为啥上线,却是发送away-》离开?
而不是online之类的词?
对应的:
    func goOffline() {
        var presence = XMPPPresence(type: "unavailable")
        curXmppStream!.sendElement(presence)
    }
就很正常:
offline了,发送unavailable
搜:
xmpp goOnline  goOffline
参考:
- (void)goOnline {
    XMPPPresence *presence = [XMPPPresence presence];
    [[self xmppStream] sendElement:presence];
}
所以后来改为了:
    private func goOnline() {
//        let presence = XMPPPresence()
        let presence = XMPPPresence(type: "available")
//        let domain = curXmppStream!.myJID.domain
//        if domain == "gmail.com" || domain == "gtalk.com" || domain == "talk.google.com" {
//            let priority = DDXMLElement.elementWithName("priority", stringValue: "24") as! DDXMLElement
//            presence.addChild(priority)
//        }
        curXmppStream!.sendElement(presence)
    }
但是还是想要去搞清楚:
XMPP中的Presence的Type,到底有哪些
搜:
XMPP Presence Type
参考:
http://xmpp.org/rfcs/rfc3921.html

2.2.1.  Types of Presence

The ‘type’ attribute of a presence stanza is OPTIONAL. A presence stanza that does not possess a ‘type’ attribute is used to signal to the server that the sender is online and available for communication. If included, the ‘type’ attribute specifies a lack of availability, a request to manage a subscription to another entity’s presence, a request for another entity’s current presence, or an error related to a previously-sent presence stanza. If included, the ‘type’ attribute MUST have one of the following values:

  • unavailable — Signals that the entity is no longer available for communication.
  • subscribe — The sender wishes to subscribe to the recipient’s presence.
  • subscribed — The sender has allowed the recipient to receive their presence.
  • unsubscribe — The sender is unsubscribing from another entity’s presence.
  • unsubscribed — The subscription request has been denied or a previously-granted subscription has been cancelled.
  • probe — A request for an entity’s current presence; SHOULD be generated only by a server on behalf of a user.
  • error — An error has occurred regarding processing or delivery of a previously-sent presence stanza.
For detailed information regarding presence semantics and the subscription model used in the context of XMPP-based instant messaging and presence applications, refer to Exchanging Presence Information and Managing Subscriptions.
Presence中的type属性是可选的,
Presence是不会去检查处理type的值的
Presence本身,是用于通知服务器表示自己上线了,可以和我通话了
如果传入type参数的话,则type表示

Represents the type of a presence packet. Note: the presence is assumed to be “available” when the type attribute of the packet is null. The valid types are:

搜:
swift XMPP goOnline
swiftXMPP goOnline
参考:
    private func goOnline() {
        let presence = XMPPPresence()
        let domain = xmppStream.myJID.domain

        if domain == "gmail.com" || domain == "gtalk.com" || domain == "talk.google.com" {
            let priority = DDXMLElement.elementWithName("priority", stringValue: "24") as! DDXMLElement
            presence.addChild(priority)
        }
        xmppStream.sendElement(presence)
    }

    private func goOffline() {
        let presence = XMPPPresence(type: "unavailable")
        xmppStream.sendElement(presence)
    }
(void)goOnline{
  //发送在线状态
  XMPPPresence *presence = [XMPPPresence presence];
   [[self xmppStream] sendElement:presence];
 }
-》
现在有点明白了:
此处的,
goOnline
主要的目的是:
告诉服务器,我上线了
而生成并发送一个presence,发送到服务器即可
不要传递type参数
-》否则,传递了type的话,type表示的是你不在线的原因。。。
-》所以是下线了,或者出现其他问题了,才在发送presence时传递type参数,表示自己不在线的原因的
-》所以,此处代码应该写成:
    private func goOnline() {
        let presence = XMPPPresence()
//        let presence = XMPPPresence(type: "available")
//        let domain = curXmppStream!.myJID.domain
//        if domain == "gmail.com" || domain == "gtalk.com" || domain == "talk.google.com" {
//            let priority = DDXMLElement.elementWithName("priority", stringValue: "24") as! DDXMLElement
//            presence.addChild(priority)
//        }
        curXmppStream!.sendElement(presence)
    }

就可以了。

-》
真为设计这个接口的人的逻辑感到无语。。。
能设计出这么违背常人的理解的接口。。。

转载请注明:在路上 » [整理]XMPP中Presence的Type的含义和用法

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.181 seconds, using 22.10MB memory