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"

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

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

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

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

点击“run spark”,显示如下图:

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

3.在程序中添加调用:

 package com.rixin.spark;

 import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Message.Type;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Session; import com.rixin.user.model.User; /**
* <b>function:</b> 利用Smack框架完成 XMPP 协议通信
*
* @author hoojo
* @createDate 2012-5-22 上午10:28:18
* @file ConnectionServerTest.java
* @package com.hoo.smack.conn
* @project jwchat
* @blog http://blog.csdn.net/IBM_hoojo
* @email [email protected]
* @version 1.0
*/
public class SmackXMPP { private Connection connection;
private ConnectionConfiguration config;
/** openfire服务器address */
private final static String server = "192.168.1.230"; private final void fail(Object o) {
if (o != null) {
System.out.println(o);
}
} private final void fail(Object o, Object... args) {
if (o != null && args != null && args.length > 0) {
String s = o.toString();
for (int i = 0; i < args.length; i++) {
String item = args[i] == null ? "" : args[i].toString();
if (s.contains("{" + i + "}")) {
s = s.replace("{" + i + "}", item);
} else {
s += " " + item;
}
}
System.out.println(s);
}
} /**
* <b>function:</b> 初始Smack对openfire服务器链接的基本配置
*
* @author hoojo
* @createDate 2012-6-25 下午04:06:42
*/
public void init() {
try {
// connection = new XMPPConnection(server);
// connection.connect();
/**
* 9090是openfire服务器默认的通信端口,你可以登录http://192.168.1.230:9090/
* 到管理员控制台查看客户端到服务器端口
*/
config = new ConnectionConfiguration(server, 5222); /** 是否启用压缩 */
config.setCompressionEnabled(true);
/** 是否启用安全验证 */
config.setSASLAuthenticationEnabled(true);
/** 是否启用调试 */
config.setDebuggerEnabled(false);
// config.setReconnectionAllowed(true);
// config.setRosterLoadedAtLogin(true); /** 创建connection链接 */
connection = new XMPPConnection(config);
/** 建立连接 */
connection.connect();
} catch (XMPPException e) {
e.printStackTrace();
}
fail(connection);
fail(connection.getConnectionID());
} public void destory() {
if (connection != null) {
connection.disconnect();
connection = null;
}
} public static void main(String[] args) {
SmackXMPP smack = new SmackXMPP();
User fromUser = new User();
fromUser.setUsername("wd");
fromUser.setPassword("123");
smack.sendMsg(fromUser, "lhc", "你有一个待办,请办理。类型:请假。http://www.oa.rixin/jingjia");
} /**
* 发送即时通讯信息
*
* @param fromUser
* 发送人对象
* @param toUserName
* 接收人用户名
* @param msg
* 发送内容
*/
public void sendMsg(User fromUser, String toUserName, String msg) {
init();
try {
connection.login(fromUser.getUsername(), fromUser.getPassword());
} catch (XMPPException e) {
e.printStackTrace();
} /**
* 向[email protected] 发送聊天消息,此时你需要用Spark软件登陆jojo这个用户,
* 这样代码就可以向jojo这个用户发送聊天消息,Spark登陆的jojo用户就可以接收到消息
**/
/** Type.chat 表示聊天,groupchat多人聊天,error错误,headline在线用户; */
Message message = new Message(toUserName + "@" + server, Type.chat);
// Message message = new Message(sessid, Type.chat);
message.setBody(msg);
connection.sendPacket(message);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
destory();
} /**
* <b>function:</b> 消息监听器,用户监听对方发送的消息,也可以想对方发送消息
*
* @author hoojo
* @createDate 2012-6-25 下午05:05:31
* @file SmackXMPPTest.java
* @package com.hoo.smack
* @project jwchat
* @blog http://blog.csdn.net/IBM_hoojo
* @email [email protected]
* @version 1.0
*/
class MyMessageListeners implements MessageListener {
public void processMessage(Chat chat, Message message) {
try {
/** 发送消息 */
chat.sendMessage("dingding……" + message.getBody());
} catch (XMPPException e) {
e.printStackTrace();
}
/** 接收消息 */
fail("From: {0}, To: {1}, Type: {2}, Sub: {3}", message.getFrom(),
message.getTo(), message.getType(), message.toXML());
/*
* Collection<Body> bodys = message.getBodies(); for (Body body :
* bodys) { fail("bodies[{0}]", body.getMessage()); }
* //fail(message.getLanguage()); //fail(message.getThread());
* //fail(message.getXmlns());
*/
fail("body: ", message.getBody());
}
}
}

运行可以发送系统消息。

04-17 22:39