/**
*发送消息
*@param con 用户连接对象
*@param user 用户JID
*@param msg 发送内容
*/
@Override
public synchronized void sendMessages(XMPPConnection con,String user, String msg) {
if (null != user && !"@".equals(user))
{
user = user + "@" +con.getServiceName();
}
Chat chat=con.getChatManager().createChat(user, null);
try {
chat.sendMessage(msg);
} catch (XMPPException e) {
e.printStackTrace();
}
}
/**
* 接收消息
* @param con 用户连接对象
*/
@Override
public void recieveMessage(final XMPPConnection con) {
final Map<String, String> params = new HashMap<String, String>();;
//final StringBuilder sb=new StringBuilder();
con.getChatManager().addChatListener(new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean arg1) {
chat.addMessageListener(new MessageListener() {
@Override
public void processMessage(Chat arg0, Message message) {
System.out.println(message.getFrom()+"-->"+message.getTo()+"\n"+message.getBody());
}
});
}
});
//return sb.toString();
}
private static void addListener(XMPPConnection con) {
// 包的过滤器
PacketFilter filterMessage = new PacketTypeFilter(Message.class);
// 创建包的监听器
PacketListener myListener = new PacketListener() {
public void processPacket(Packet packet) {
// 以XML格式输出接收到的消息
System.out.println("Body: " + ((Message) packet).getBody());
}
};
// 给连接注册一个包的监听器
con.addPacketListener(myListener, filterMessage);
}