Android XMPP通訊自定義Packet&Provider
摘要
在xmpp通信過(guò)程中,asmack中提供的Packet組件是IQ,Message,Presence三種: IQ用于查詢 Message用于消息傳遞 Presence用于狀態(tài)交互 他們都是Packet的子類,實(shí)質(zhì)是用于將消息封裝成響應(yīng)的xml格式來(lái)進(jìn)行數(shù)據(jù)交換,都有著良好的可擴(kuò)展性。
簡(jiǎn)介
我們以開(kāi)源項(xiàng)目androidpn為例:
androidpn (Android Push Notification)是一個(gè)基于XMPP協(xié)議的java開(kāi)源Android push notification實(shí)現(xiàn)。它包含了完整的客戶端和服務(wù)器端。
androidpn包括Server端和Client端,項(xiàng)目名稱是androidpn-server和androidpn-client。
事實(shí)上,androidpn-server可以支持app運(yùn)行于iOS,UWP,Windows,Linux等平臺(tái),不僅限于android,因此,希望項(xiàng)目的名稱改為XPN(Xmpp Push Notification)似乎更加符合其實(shí)際場(chǎng)景,我們以后涉及到Android Push Notification統(tǒng)稱為XPN。
XNP目前狀態(tài)
項(xiàng)目自2014年1月就已經(jīng)停止更新了,此外,asmack項(xiàng)目也停止更新了,作者建議使用openfire官方的smack4.0,不過(guò)這樣的話引入的jar會(huì)特別多,特別大。當(dāng)然,我們下載到了asmack8.10.0比較新的穩(wěn)定版本,可以完全用于學(xué)習(xí)和擴(kuò)展。
項(xiàng)目相關(guān)下載站點(diǎn)
asmack-github.com - asmack項(xiàng)目地址
asmack-asmack.freakempire.de - asmack鏡像地址
androidpn(XPN)-github.com - androidpn下載地址
一.關(guān)于Packet數(shù)據(jù)包
Packet是IQ,Message,Presence的父類,用于實(shí)現(xiàn)消息組件類型。
消息語(yǔ)義學(xué)message
message是一種基本推送消息方法,它不要求響應(yīng)。主要用于IM、groupChat、alert和notification之類的應(yīng)用中。
主要 屬性如下:
type屬性,它主要有5種類型:
normal:類似于email,主要特點(diǎn)是不要求響應(yīng);
chat:類似于qq里的好友即時(shí)聊天,主要特點(diǎn)是實(shí)時(shí)通訊;
groupchat:類似于聊天室里的群聊;
headline:用于發(fā)送alert和notification;
error:如果發(fā)送message出錯(cuò),發(fā)現(xiàn)錯(cuò)誤的實(shí)體會(huì)用這個(gè)類別來(lái)通知發(fā)送者出錯(cuò)了;
to屬性:標(biāo)識(shí)消息的接收方。
from屬性:指發(fā)送方的名字或標(biāo)示。為防止地址外泄,這個(gè)地址通常由發(fā)送者的server填寫(xiě),而不是發(fā)送者。
載荷(payload):例如body,subject
<message to="lily@jabber.org/contact" type="chat" > <body> 你好,在忙嗎</body> </message>
出席信息語(yǔ)義學(xué)presence
presence用來(lái)表明用戶的狀態(tài),如:online、away、dnd(請(qǐng)勿打擾)等。當(dāng)改變自己的狀態(tài)時(shí),就會(huì)在stream的上下文中插入一個(gè)Presence元素,來(lái)表明自身的狀態(tài)。要想接受presence消息,必須經(jīng)過(guò)一個(gè)叫做presence subscription的授權(quán)過(guò)程。
屬性:
type屬性,非必須。有以下類別
subscribe:訂閱其他用戶的狀態(tài)
probe:請(qǐng)求獲取其他用戶的狀態(tài)
unavailable:不可用,離線(offline)狀態(tài)
to屬性:標(biāo)識(shí)消息的接收方。
from屬性:指發(fā)送方的名字或標(biāo)示。
載荷(payload):
show:
chat:聊天中
away:暫時(shí)離開(kāi)
xa:eXtend Away,長(zhǎng)時(shí)間離開(kāi)
dnd:勿打擾
status:格式自由,可閱讀的文本。也叫做rich presence或者extended presence,常用來(lái)表示用戶當(dāng)前心情,活動(dòng),聽(tīng)的歌曲,看的視頻,所在的聊天室,訪問(wèn)的網(wǎng)頁(yè),玩的游戲等等。
priority:范圍-128~127。高優(yōu)先級(jí)的resource能接受發(fā)送到bare JID的消息,低優(yōu)先級(jí)的resource不能。優(yōu)先級(jí)為
<presence from="alice@wonderland.lit/pda">
<show>xa</show>
<status>down the rabbit hole!</status>
</presence>
IQ語(yǔ)義學(xué)
一種請(qǐng)求/響應(yīng)機(jī)制,從一個(gè)實(shí)體從發(fā)送請(qǐng)求,另外一個(gè)實(shí)體接受請(qǐng)求,并進(jìn)行響應(yīng)。例如,client在stream的上下文中插入一個(gè)元素,向Server請(qǐng)求得到自己的好友列表,Server返回一個(gè),里面是請(qǐng)求的結(jié)果。
主要的屬性是type。包括:
Get :獲取當(dāng)前域值。類似于http get方法。
Set :設(shè)置或替換get查詢的值。類似于http put方法。
Result :說(shuō)明成功的響應(yīng)了先前的查詢。類似于http狀態(tài)碼200。
Error: 查詢和響應(yīng)中出現(xiàn)的錯(cuò)誤。
<iq from="alice@wonderland.lit/pda"
id="rr82a1z7"
to="alice@wonderland.lit"
type="get">
<query xmlns="jabber:iq:roster"/>
</iq>
二.自定義Packet
由于服務(wù)器和客戶端使用的Packet不同,但是他們交互的數(shù)據(jù)格式都是xml,因此,這個(gè)過(guò)程我們理解xml實(shí)現(xiàn)過(guò)程即可。
1.定義Packet封裝對(duì)象
由于asmack標(biāo)簽解析的限制,我們不能自定義解析,除非修改源碼,這里出于簡(jiǎn)單,這里只能繼承現(xiàn)有標(biāo)簽之一。
我么按照項(xiàng)目代碼NotificationIQ為例,這里沒(méi)有繼承Packet,而是繼承了IQ
import org.jivesoftware.smack.packet.IQ;
/**
* This class represents a notifcatin IQ packet.
*
* @author Sehwan Noh (devnoh@gmail.com)
*/
public class NotificationIQ extends IQ {
private String id;
private String apiKey;
private String title;
private String message;
private String uri;
public NotificationIQ() {
}
@Override
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append("<").append("notification").append(" xmlns=\"").append(
"androidpn:iq:notification").append("\">");
if (id != null) {
buf.append("<id>").append(id).append("</id>");
}
buf.append("</").append("notification").append("> ");
return buf.toString();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getUri() {
return uri;
}
public void setUri(String url) {
this.uri = url;
}
}
其中,getChildElementXml()是IQ的子類,用來(lái)拼接成<iq>下的直接點(diǎn)。
public abstract class IQ extends Packet {
private Type type = Type.GET;
public IQ() {
super();
}
public IQ(IQ iq) {
super(iq);
type = iq.getType();
}
/**
* Returns the type of the IQ packet.
*
* @return the type of the IQ packet.
*/
public Type getType() {
return type;
}
/**
* Sets the type of the IQ packet.
*
* @param type the type of the IQ packet.
*/
public void setType(Type type) {
if (type == null) {
this.type = Type.GET;
}
else {
this.type = type;
}
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<iq ");
if (getPacketID() != null) {
buf.append("id=\"" + getPacketID() + "\" ");
}
if (getTo() != null) {
buf.append("to=\"").append(StringUtils.escapeForXML(getTo())).append("\" ");
}
if (getFrom() != null) {
buf.append("from=\"").append(StringUtils.escapeForXML(getFrom())).append("\" ");
}
if (type == null) {
buf.append("type=\"get\">");
}
else {
buf.append("type=\"").append(getType()).append("\">");
}
// Add the query section if there is one.
String queryXML = getChildElementXML();
if (queryXML != null) {
buf.append(queryXML);
}
// Add the error sub-packet, if there is one.
XMPPError error = getError();
if (error != null) {
buf.append(error.toXML());
}
buf.append("</iq>");
return buf.toString();
}
/**
* Returns the sub-element XML section of the IQ packet, or <tt>null</tt> if there
* isn't one. Packet extensions <b>must</b> be included, if any are defined.<p>
*
* Extensions of this class must override this method.
*
* @return the child element section of the IQ XML.
*/
public abstract String getChildElementXML();
/**
* Convenience method to create a new empty {@link Type#RESULT IQ.Type.RESULT}
* IQ based on a {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET}
* IQ. The new packet will be initialized with:<ul>
* <li>The sender set to the recipient of the originating IQ.
* <li>The recipient set to the sender of the originating IQ.
* <li>The type set to {@link Type#RESULT IQ.Type.RESULT}.
* <li>The id set to the id of the originating IQ.
* <li>No child element of the IQ element.
* </ul>
*
* @param iq the {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET} IQ packet.
* @throws IllegalArgumentException if the IQ packet does not have a type of
* {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET}.
* @return a new {@link Type#RESULT IQ.Type.RESULT} IQ based on the originating IQ.
*/
public static IQ createResultIQ(final IQ request) {
if (!(request.getType() == Type.GET || request.getType() == Type.SET)) {
throw new IllegalArgumentException(
"IQ must be of type 'set' or 'get'. Original IQ: " + request.toXML());
}
final IQ result = new IQ() {
public String getChildElementXML() {
return null;
}
};
result.setType(Type.RESULT);
result.setPacketID(request.getPacketID());
result.setFrom(request.getTo());
result.setTo(request.getFrom());
return result;
}
/**
* Convenience method to create a new {@link Type#ERROR IQ.Type.ERROR} IQ
* based on a {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET}
* IQ. The new packet will be initialized with:<ul>
* <li>The sender set to the recipient of the originating IQ.
* <li>The recipient set to the sender of the originating IQ.
* <li>The type set to {@link Type#ERROR IQ.Type.ERROR}.
* <li>The id set to the id of the originating IQ.
* <li>The child element contained in the associated originating IQ.
* <li>The provided {@link XMPPError XMPPError}.
* </ul>
*
* @param iq the {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET} IQ packet.
* @param error the error to associate with the created IQ packet.
* @throws IllegalArgumentException if the IQ packet does not have a type of
* {@link Type#GET IQ.Type.GET} or {@link Type#SET IQ.Type.SET}.
* @return a new {@link Type#ERROR IQ.Type.ERROR} IQ based on the originating IQ.
*/
public static IQ createErrorResponse(final IQ request, final XMPPError error) {
if (!(request.getType() == Type.GET || request.getType() == Type.SET)) {
throw new IllegalArgumentException(
"IQ must be of type 'set' or 'get'. Original IQ: " + request.toXML());
}
final IQ result = new IQ() {
public String getChildElementXML() {
return request.getChildElementXML();
}
};
result.setType(Type.ERROR);
result.setPacketID(request.getPacketID());
result.setFrom(request.getTo());
result.setTo(request.getFrom());
result.setError(error);
return result;
}
/**
* A class to represent the type of the IQ packet. The types are:
*
* <ul>
* <li>IQ.Type.GET
* <li>IQ.Type.SET
* <li>IQ.Type.RESULT
* <li>IQ.Type.ERROR
* </ul>
*/
public static class Type {
public static final Type GET = new Type("get");
public static final Type SET = new Type("set");
public static final Type RESULT = new Type("result");
public static final Type ERROR = new Type("error");
/**
* Converts a String into the corresponding types. Valid String values
* that can be converted to types are: "get", "set", "result", and "error".
*
* @param type the String value to covert.
* @return the corresponding Type.
*/
public static Type fromString(String type) {
if (type == null) {
return null;
}
type = type.toLowerCase();
if (GET.toString().equals(type)) {
return GET;
}
else if (SET.toString().equals(type)) {
return SET;
}
else if (ERROR.toString().equals(type)) {
return ERROR;
}
else if (RESULT.toString().equals(type)) {
return RESULT;
}
else {
return null;
}
}
private String value;
private Type(String value) {
this.value = value;
}
public String toString() {
return value;
}
}
}
最終可生成如下結(jié)構(gòu)的數(shù)據(jù)
<iq from=""> <nofitication xlns=""> <iq>
我們?cè)陧?xiàng)目中的使用很簡(jiǎn)單
xmppManager.getConnection().sendPacket(<NotificationIQ>niq)
當(dāng)然,上面只是實(shí)現(xiàn)了object->xml,接下來(lái)我們實(shí)現(xiàn)xml->data
2.實(shí)現(xiàn)IQProvider
先來(lái)看看IQProvider源碼
public interface IQProvider {
/**
* Parse the IQ sub-document and create an IQ instance. Each IQ must have a
* single child element. At the beginning of the method call, the xml parser
* will be positioned at the opening tag of the IQ child element. At the end
* of the method call, the parser <b>must</b> be positioned on the closing tag
* of the child element.
*
* @param parser an XML parser.
* @return a new IQ instance.
* @throws Exception if an error occurs parsing the XML.
*/
public IQ parseIQ(XmlPullParser parser) throws Exception;
}
實(shí)現(xiàn)自定義的解析工具
public class NotificationIQProvider implements IQProvider {
public NotificationIQProvider() {
}
@Override
public IQ parseIQ(XmlPullParser parser) throws Exception {
NotificationIQ notification = new NotificationIQ();
for (boolean done = false; !done;) {
int eventType = parser.next();
if (eventType == 2) {
if ("id".equals(parser.getName())) {
notification.setId(parser.nextText());
}
if ("apiKey".equals(parser.getName())) {
notification.setApiKey(parser.nextText());
}
if ("title".equals(parser.getName())) {
notification.setTitle(parser.nextText());
}
if ("message".equals(parser.getName())) {
notification.setMessage(parser.nextText());
}
if ("uri".equals(parser.getName())) {
notification.setUri(parser.nextText());
}
} else if (eventType == 3
&& "notification".equals(parser.getName())) {
done = true;
}
}
return notification;
}
}
項(xiàng)目中使用方法
ProviderManager.getInstance().addIQProvider("notification",
"androidpn:iq:notification",
new NotificationIQProvider());
在asmack中PacketParserUtils類中會(huì)進(jìn)行如下調(diào)用
Object provider = ProviderManager.getInstance().getIQProvider(elementName, namespace);
if (provider != null) {
if (provider instanceof IQProvider) {
iqPacket = ((IQProvider)provider).parseIQ(parser);
}
else if (provider instanceof Class) {
iqPacket = (IQ)PacketParserUtils.parseWithIntrospection(elementName,
(Class<?>)provider, parser);
}
}
相關(guān)文章
Android實(shí)現(xiàn)定時(shí)任務(wù)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)定時(shí)任務(wù)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Android Studio全局搜索快捷鍵(Ctrl+Shift+F)失效問(wèn)題及解決
這篇文章主要介紹了Android Studio全局搜索快捷鍵(Ctrl+Shift+F)失效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Android開(kāi)發(fā)中Flutter組件實(shí)用技巧
這篇文章主要為大家介紹了Android開(kāi)發(fā)中Flutter組件實(shí)用技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Android實(shí)現(xiàn)列表元素動(dòng)態(tài)效果
本文將利用AnimatedList組件實(shí)現(xiàn)列表元素的一些動(dòng)態(tài)效果,例如添加元素時(shí)的漸現(xiàn)效果,刪除元素逐漸消失的效果等,感興趣的小伙伴可以了解一下2022-03-03
Android搭建grpc環(huán)境過(guò)程分步詳解
本篇文章使用的IDE是Android Studio。這里先吐槽一句,安卓項(xiàng)目搭建grpc環(huán)境,不管是引入插件還是引入第三方庫(kù),對(duì)于版本的要求都極為苛刻,一旦版本不匹配就會(huì)報(bào)錯(cuò),所以對(duì)于版本的搭配一定要注意2023-04-04
Android adb logcat 命令查看日志詳細(xì)介紹
這篇文章主要介紹了Android adb logcat 命令詳細(xì)介紹的相關(guān)資料,這里對(duì)logcat 命令進(jìn)行了詳細(xì)介紹,并介紹了過(guò)濾日志輸出的知識(shí),需要的朋友可以參考下2016-12-12
android使用PopupWindow實(shí)現(xiàn)頁(yè)面點(diǎn)擊頂部彈出下拉菜單
這篇文章主要給大家介紹android使用PopupWindow實(shí)現(xiàn)頁(yè)面點(diǎn)擊頂部彈出下拉菜單,實(shí)現(xiàn)此功能主要通過(guò)PopupWindow方法,代碼也很簡(jiǎn)單,需要的朋友可以參考下2015-08-08

