分享

Smack4.1.1 聊天推送

 WindySky 2016-03-10

基于openfire_3_10_0、smack_4_1_1 的聊天推送demo.
前提:在最新版本中通过smack类库实现聊天内容的推送方式和旧版本变化有点大。
《注意》
在导入smack本身的jar包之外,还需要以下第三方库的支持,请事先准备下载:
1. commons-logging-1.2.jar
2. httpclient-4.4.jar
3. jxmpp-core-0.4.1.jar
4. jxmpp-util-cache-0.4.1.jar
5. minidns-0.1.1.jar
6. xpp3-1.1.4.jar

如需要自定义扩展聊天内容可参照 MessageExtension类实现
AbstractXMPPConnection conn = null;
try {
Builder builder = XMPPTCPConnectionConfiguration.builder();
builder.setSecurityMode(SecurityMode.disabled);
XMPPTCPConnectionConfiguration config = builder
.setUsernameAndPassword("admin","12345678")
.setServiceName("192.168.1.118").setHost("192.168.1.118")
.setPort(5222).build();

conn = new XMPPTCPConnection(config);

conn.connect();
conn.login();
Message msg = new Message();
msg.setTo("feng123@192.168.1.118");
msg.setBody("admin给 feng123发消息1112=====");
// 自定义扩展message
MessageExtension ext = new MessageExtension();
ext.setProperty("qstId","aac18a578d1c4c1fb88101a43fb92bdc");
ext.setProperty("messageType","0");
msg.addExtension(ext);
conn.sendStanza(msg);



} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.disconnect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;

import org.jivesoftware.smack.packet.ExtensionElement;

/**
*
 * @author QSI
*
*/
public class MessageExtension implements ExtensionElement {
private Properties propertys = new Properties();
// 信息元素名称
private String propertiesName ="properties";
// Element元素名称
private String propertyElement ="property";
// name元素名称
private String nameElement ="name";
// value元素名称
private String valueElement ="value";

@Override
public String getElementName() {
return propertiesName;
}

/**
 * 27 * 返回扩展的xml字符串 28 * 此字符串作为message元素的子元素 29
*/
@Override
public String toXML() {
StringBuilder sb = new StringBuilder();

sb.append("<");
sb.append(propertiesName);
sb.append(">");

Iterator<Entry<Object, Object>> it = this.propertys.entrySet()
.iterator();
while (it.hasNext()) {
Entry<Object, Object> entry = it.next();
Object key = entry.getKey();
Object value = entry.getValue();
sb.append("<");
sb.append(propertyElement);
sb.append(">");

sb.append("<");
sb.append(nameElement);
sb.append(">");
sb.append(key);
sb.append("</");
sb.append(nameElement);
sb.append(">");

sb.append("<");
sb.append(valueElement);
sb.append(">");
sb.append(value);
sb.append("</");
sb.append(valueElement);
sb.append(">");

sb.append("</");
sb.append(propertyElement);
sb.append(">");
}

sb.append("</");
sb.append(propertiesName);
sb.append(">");

return sb.toString();
}

public void setProperty(String name, String value) {
this.propertys.setProperty(name, value);

}

/**
 * 62 * 可忽略 63
*/
@Override
public String getNamespace() {
return"";
}



}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多