1.下載OpenFire服務器,進行安裝,參考http://m.survivalescaperooms.com/hoojo/archive/2012/05/17/2506769.html
2.程序運行客戶端:下載客戶端代碼并進行配置 選中項目-->點擊“run configurations”進行配置,如下圖所示:
Main Class:org.jivesoftware.launcher.Startup;
VM arguments:-Djava.library.path="${workspace_loc:spark}/target/build/lib/windows"




點擊“run spark”,顯示如下圖:

3.在程序中添加調用:
1 package com.rixin.spark; 2 3 import org.jivesoftware.smack.Chat; 4 import org.jivesoftware.smack.Connection; 5 import org.jivesoftware.smack.ConnectionConfiguration; 6 import org.jivesoftware.smack.MessageListener; 7 import org.jivesoftware.smack.XMPPConnection; 8 import org.jivesoftware.smack.XMPPException; 9 import org.jivesoftware.smack.packet.Message; 10 import org.jivesoftware.smack.packet.Message.Type; 11 import org.jivesoftware.smack.packet.PResence; 12 import org.jivesoftware.smack.packet.session; 13 14 import com.rixin.user.model.User; 15 16 /** 17 * <b>function:</b> 利用Smack框架完成 XMPP 協議通信 18 * 19 * @author hoojo 20 * @createDate 2012-5-22 上午10:28:18 21 * @file ConnectionServerTest.java 22 * @package com.hoo.smack.conn 23 * @project jwchat 24 * @blog http://blog.csdn.net/IBM_hoojo 25 * @email hoojo_@126.com 26 * @version 1.0 27 */ 28 public class SmackXMPP { 29 30 private Connection connection; 31 private ConnectionConfiguration config; 32 /** openfire服務器address */ 33 private final static String server = "192.168.1.230"; 34 35 private final void fail(Object o) { 36 if (o != null) { 37 System.out.println(o); 38 } 39 } 40 41 private final void fail(Object o, Object... args) { 42 if (o != null && args != null && args.length > 0) { 43 String s = o.toString(); 44 for (int i = 0; i < args.length; i++) { 45 String item = args[i] == null ? "" : args[i].toString(); 46 if (s.contains("{" + i + "}")) { 47 s = s.replace("{" + i + "}", item); 48 } else { 49 s += " " + item; 50 } 51 } 52 System.out.println(s); 53 } 54 } 55 56 /** 57 * <b>function:</b> 初始Smack對openfire服務器鏈接的基本配置 58 * 59 * @author hoojo 60 * @createDate 2012-6-25 下午04:06:42 61 */ 62 public void init() { 63 try { 64 // connection = new XMPPConnection(server); 65 // connection.connect(); 66 /** 67 * 9090是openfire服務器默認的通信端口,你可以登錄http://192.168.1.230:9090/ 68 * 到管理員控制臺查看客戶端到服務器端口 69 */ 70 config = new ConnectionConfiguration(server, 5222); 71 72 /** 是否啟用壓縮 */ 73 config.setCompressionEnabled(true); 74 /** 是否啟用安全驗證 */ 75 config.setSASLAuthenticationEnabled(true); 76 /** 是否啟用調試 */ 77 config.setDebuggerEnabled(false); 78 // config.setReconnectionAllowed(true); 79 // config.setRosterLoadedAtLogin(true); 80 81 /** 創建connection鏈接 */ 82 connection = new XMPPConnection(config); 83 /** 建立連接 */ 84 connection.connect(); 85 } catch (XMPPException e) { 86 e.printStackTrace(); 87 } 88 fail(connection); 89 fail(connection.getConnectionID()); 90 } 91 92 public void destory() { 93 if (connection != null) { 94 connection.disconnect(); 95 connection = null; 96 } 97 } 98 99 public static void main(String[] args) {100 SmackXMPP smack = new SmackXMPP();101 User fromUser = new User();102 fromUser.setUsername("wd");103 fromUser.setPassWord("123");104 smack.sendMsg(fromUser, "lhc", "你有一個待辦,請辦理。類型:請假。http://www.oa.rixin/jingjia");105 }106 107 /**108 * 發送即時通訊信息109 * 110 * @param fromUser111 * 發送人對象112 * @param toUserName113 * 接收人用戶名114 * @param msg115 * 發送內容116 */117 public void sendMsg(User fromUser, String toUserName, String msg) {118 init();119 try {120 connection.login(fromUser.getUsername(), fromUser.getPassword());121 } catch (XMPPException e) {122 e.printStackTrace();123 }124 125 /**126 * 向jojo@192.168.1.230 發送聊天消息,此時你需要用Spark軟件登陸jojo這個用戶,127 * 這樣代碼就可以向jojo這個用戶發送聊天消息,Spark登陸的jojo用戶就可以接收到消息128 **/129 /** Type.chat 表示聊天,groupchat多人聊天,error錯誤,headline在線用戶; */130 Message message = new Message(toUserName + "@" + server, Type.chat);131 // Message message = new Message(sessid, Type.chat);132 message.setBody(msg);133 connection.sendPacket(message);134 try {135 Thread.sleep(1);136 } catch (InterruptedException e) {137 e.printStackTrace();138 }139 destory();140 }141 142 /**143 * <b>function:</b> 消息監聽器,用戶監聽對方發送的消息,也可以想對方發送消息144 * 145 * @author hoojo146 * @createDate 2012-6-25 下午05:05:31147 * @file SmackXMPPTest.java148 * @package com.hoo.smack149 * @project jwchat150 * @blog http://blog.csdn.net/IBM_hoojo151 * @email hoojo_@126.com152 * @version 1.0153 */154 class MyMessageListeners implements MessageListener {155 public void processMessage(Chat chat, Message message) {156 try {157 /** 發送消息 */158 chat.sendMessage("dingding&hellxml());165 /*166 * Collection<Body> bodys = message.getBodies(); for (Body body :167 * bodys) { fail("bodies[{0}]", body.getMessage()); }168 * //fail(message.getLanguage()); //fail(message.getThread());169 * //fail(message.getXmlns());170 */171 fail("body: ", message.getBody());172 }173 }174 }
運行可以發送系統消息。
新聞熱點
疑難解答