我正在开发聊天应用程序,因为我需要向其他用户发送位置。(一对一聊天)我已经阅读了XEP-0080,但是在XMPP框架XEP-80类中,我无法使用。我还检查了Xmpppubsub模块,但没有找到如何将用户位置发送给其他用户。
参考链接:
- https://github.com/robbiehanson/xmppframework/issues/506
- 如何使用iOS SDK中的XMPP通过位置?
- https://github.com/buddycloud/buddycloud-ios-client
服务器:ejabber
如果提供代码和教程链接的片段,这是充分的帮助。
在许多尝试之后,我成功地将用户自定义位置发送给其他聊天用户。
使用的扩展名:xep-0080
下面我有提及发送位置的功能
public class func sendLocationMessage(msg:String,lat : String ,long : String ,to receiver: String,completionHandler completion:@escaping XMPPMessageMngCompletionHandler){
let body = DDXMLElement.element(withName: "body") as! DDXMLElement
let messageID = XMPPConnect.sharedInstance.xmppStream.generateUUID()
body.stringValue = "Location"
let completeMessage = DDXMLElement.element(withName: "message") as! DDXMLElement
let reuestElemetn = DDXMLElement.element(withName: "request", stringValue: "urn:xmpp:receipts")
completeMessage.addChild(reuestElemetn as! DDXMLNode)
completeMessage.addAttribute(withName: "id", stringValue: messageID!)
completeMessage.addAttribute(withName: "type", stringValue: "chat")
completeMessage.addAttribute(withName: "to", stringValue: receiver)
completeMessage.addChild(body)
let geoElemetn = DDXMLElement.element(withName: "geoloc") as! DDXMLElement
geoElemetn.addAttribute(withName: "xmlns", stringValue: "http://jabber.org/protocol/geoloc")
let latElement = DDXMLElement.element(withName: "lat") as! DDXMLElement
latElement.stringValue = lat
geoElemetn.addChild(latElement);
let lngElement = DDXMLElement.element(withName: "lon") as! DDXMLElement
lngElement.stringValue = long
geoElemetn.addChild(lngElement);
let uriElement = DDXMLElement.element(withName: "uri") as! DDXMLElement
uriElement.stringValue = msg; //google map image url.
geoElemetn.addChild(uriElement)
completeMessage.addChild(geoElemetn)
sharedInstance.didSendMessageCompletionBlock = completion
XMPPConnect.sharedInstance.xmppStream?.send(completeMessage)
}
从此功能中,您也可以将位置发送到Android(Smack Lib)
对于 didreceivemessage 委托方法您可以检查属性。
if message.attribute(forName: "geoloc") != nil {
self.receivedLocationMsgFromUser(message: message, from: from)
}else{
self.receivedTextMsgFromUser(message: message, msgStr: msg, from: from)
}