如何检查用户 JID 是否已在 XMPPFrameWork 中使用

How to check if a user JID is already taken in XMPPFrameWork

我是 xmppframework 的新手。我有一个快速的问题。是否有可能知道某个 JID 是否可用。换句话说,是否可以检查用户名是否已与某个帐户关联或注册。

使用 jabber search 是的,您可以搜索并找到具有特定 jID 或用户名的用户或...

在这里,我创建了一个函数,它将一些 jID 作为参数,服务器响应您作为查询,并且在该查询中如果有任何匹配,将会有包含匹配 jID 的用户的项目

func xmppQueryForSearchPacket(forJID jid: String) {
    let searchingServer = "search.something" //something must replace with your xmpp domain name
    let query = DDXMLElement(name: "query", xmlns: "jabber:iq:search")
    let x = DDXMLElement(name: "x", xmlns: "jabber:x:data")
    x.addAttribute(withName: "type", stringValue: "submit")


    let field1 = DDXMLElement(name: "field")
    let value1 = DDXMLElement(name: "value", stringValue: "jabber:iq:search")
    field1.addAttribute(withName: "type", stringValue: "hidden")
    field1.addAttribute(withName: "var", stringValue: "FORM_TYPE")
    field1.addChild(value1)


    let field2 = DDXMLElement(name: "field")
    let value2 = DDXMLElement(name: "value", stringValue: jid)
    field2.addAttribute(withName: "type", stringValue: "text-single")
    field2.addAttribute(withName: "var", stringValue: "search")
    field2.addChild(value2)

    //we just search in userNames

    let field3 = DDXMLElement(name: "field")
    let value3 = DDXMLElement(name: "value", stringValue: "1")
    field3.addAttribute(withName: "type", stringValue: "boolean")
    field3.addAttribute(withName: "var", stringValue: "Username")
    field3.addChild(value3)


    x.addChild(field1)
    x.addChild(field2)
    x.addChild(field3)
    query.addChild(x)


    let iq = XMPPIQ(iqType: .set, to: XMPPJID(string: searchingServer), elementID: XMPPStream.generateUUID, child: query)
   XMPPStream.send(iq)
}

该数据包的答案将以查询形式出现,您可以这样接收:

extension yourClassName: XMPPStreamDelegate {
    func xmppStream(_ sender: XMPPStream, didReceive iq: XMPPIQ) -> Bool {
        let searchingServer = "search.something"
        if iq.from?.bare != searchingServer {
            return true
        }
        return false
    }
}

有关详细信息,请阅读 XEP-0055