spark结合 Openfire服务器,发送聊天消息

1.下载OpenFire服务器,进行安装,参考http://www.cnblogs.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 fromUser
111      *            发送人对象
112      * @param toUserName
113      *            接收人用户名
114      * @param msg
115      *            发送内容
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 hoojo
146      * @createDate 2012-6-25 下午05:05:31
147      * @file SmackXMPPTest.java
148      * @package com.hoo.smack
149      * @project jwchat
150      * @blog http://blog.csdn.net/IBM_hoojo
151      * @email hoojo_@126.com
152      * @version 1.0
153      */
154     class MyMessageListeners implements MessageListener {
155         public void processMessage(Chat chat, Message message) {
156             try {
157                 /** 发送消息 */
158                 chat.sendMessage("dingding……" + message.getBody());
159             } catch (XMPPException e) {
160                 e.printStackTrace();
161             }
162             /** 接收消息 */
163             fail("From: {0}, To: {1}, Type: {2}, Sub: {3}", message.getFrom(),
164                     message.getTo(), message.getType(), message.toXML());
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 }


运行可以发送系统消息。

 

posted @ 2015-03-19 10:50  lhconglhc  阅读(992)  评论(0编辑  收藏  举报