下面通过源码搭建的openfire服务,实现简单的A向B发消息,B收到后回复的简单聊天例子。
首先启动openfire服务器,添加testuser
用户,密码同样设置为nufront
。然后我们就用testuser和admin做一个简单的对话。代码如下
maven
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-core</artifactId>
<version>4.3.4</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-extensions</artifactId>
<version>4.3.4</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-tcp</artifactId>
<version>4.3.4</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-java7</artifactId>
<version>4.3.4</version>
</dependency>
MsgTestA
package com.nufront.openfire.client;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.chat2.Chat;
import org.jivesoftware.smack.chat2.ChatManager;
import org.jivesoftware.smack.chat2.IncomingChatMessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import java.io.IOException;
public class MsgTestA {
private static XMPPTCPConnectionConfiguration config;
private static AbstractXMPPConnection conn;
static {
try {
config = XMPPTCPConnectionConfiguration.builder()
.setXmppDomain("www.test.com")
.setHost("www.test.com")
.setPort(5222)
.setDebuggerFactory(SmackConfiguration.getDefaultSmackDebuggerFactory())
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.build();
conn = new XMPPTCPConnection(config);
conn.connect();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void login(String username,String password){
try {
if(conn!=null){
conn.login(username, password);
System.out.println("user "+username+" login successfully.");
}
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
public static void sendMessage(String username,String password,String msg){
//先登录
login(username, password);
//再构建聊天室
ChatManager cm = ChatManager.getInstanceFor(conn);
cm.addIncomingListener(new IncomingChatMessageListener() {
@Override
public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
System.out.println("message from : "+from+" : "+message.getBody());
}
});
try {
EntityBareJid jid = JidCreate.entityBareFrom("admin@www.test.com");
Chat chat = cm.chatWith(jid);
Message message = new Message();
message.setBody(msg);
chat.send(message);
while (true){}
} catch (XmppStringprepException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main( String[] args ){
sendMessage("testuser", "nufront", "hello,admin how are you");
}
}
用户testuser向admin发送一条消息。
MsgTestB
package com.nufront.openfire.client;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.chat2.Chat;
import org.jivesoftware.smack.chat2.ChatManager;
import org.jivesoftware.smack.chat2.IncomingChatMessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import java.io.IOException;
public class MsgTestB {
private static XMPPTCPConnectionConfiguration config;
private static AbstractXMPPConnection conn;
static {
try {
config = XMPPTCPConnectionConfiguration.builder()
.setXmppDomain("www.test.com")
.setHost("www.test.com")
.setPort(5222)
.setDebuggerFactory(SmackConfiguration.getDefaultSmackDebuggerFactory())
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.build();
conn = new XMPPTCPConnection(config);
conn.connect();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void login(String username,String password){
try {
if(conn!=null){
conn.login(username, password);
System.out.println("user "+username+" login successfully.");
}
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
public static void sendMessage(String username,String password,String msg){
//先登录
login(username, password);
//再构建聊天室
ChatManager cm = ChatManager.getInstanceFor(conn);
cm.addIncomingListener(new IncomingChatMessageListener() {
@Override
public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
System.out.println("message from : "+from+" : "+message.getBody());
//此时回复一条消息
Message resMsg = new Message();
resMsg.setBody("Hi,"+from.toString()+",I has received your msg");
try {
chat.send(resMsg);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
try {
EntityBareJid jid = JidCreate.entityBareFrom("testuser@www.test.com");
Chat chat = cm.chatWith(jid);
Message message = new Message();
message.setBody(msg);
chat.send(message);
while (true){}
} catch (XmppStringprepException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main( String[] args ){
sendMessage("admin", "nufront", "hello, testuser");
}
}
先启动B,此时B会向A发送一条问候消息,当然此时A还没启动不在线,由于没离线消息支持,这条消息丢了。然后我们启动A,A向B发送一条问候消息,此时B收到并回复,回复后,A在线了,就会收到。
B端admin用户收到了A端testuser的问候并发送回复消息的日志,
14:56:01 RECV (0): <message to="admin@www.test.com" id="9NFfj-11" from="testuser@www.test.com/9zit3dgk2a"><body>hello,admin how are you</body></message> message from : testuser@www.test.com : hello,admin how are you 14:56:01 SENT (0): <message to='testuser@www.test.com/9zit3dgk2a' id='M30D0-15'><body>Hi,testuser@www.test.com,I has received your msg</body></message><r xmlns='urn:xmpp:sm:3'/> 14:56:01 RECV (0): <a xmlns='urn:xmpp:sm:3' h='5' />
A端testuser收到了B端admin用户回复的日志
14:56:01 RECV (0): <message to="testuser@www.test.com/9zit3dgk2a" id="M30D0-15" from="admin@www.test.com/6uzi3xfjew"><body>Hi,testuser@www.test.com,I has received your msg</body></message> 14:56:01 RECV (0): <r xmlns='urn:xmpp:sm:3' /> 14:56:01 SENT (0): <a xmlns='urn:xmpp:sm:3' h='5'/> message from : admin@www.test.com : Hi,testuser@www.test.com,I has received your msg