找了一些demo,做了一些示例,演示了基于xmpp协议的openfire的客户端之间消息的发送。
代码需要两个包,smack.jar ,smackx.jar。
第一个代码,只是点对点发送消息的,不涉及广播。
package com.test; import java.io.BufferedReader; import java.io.InputStreamReader; import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.MessageListener; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.packet.Message; public class ChatTest { public static void main(String[] args) throws XMPPException { // TODO Auto-generated method stub XMPPConnection.DEBUG_ENABLED=true; XMPPConnection connection=new XMPPConnection("127.0.0.1"); connection.connect(); connection.login("zhang", "12345"); Chat chat=connection.getChatManager().createChat("liu@127.0.0.1", new MessageListener() { @Override public void processMessage(Chat chat, Message message) { // TODO Auto-generated method stub System.out.println(message.getFrom()+" "+new java.util.Date().toString()+" say:"+message.getBody()); } }); BufferedReader cmdl=new BufferedReader(new InputStreamReader(System.in)); for (; ; ) { try { String cmd=cmdl.readLine(); if ("!q".equalsIgnoreCase(cmd)) { break; } chat.sendMessage(cmd); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } connection.disconnect(); System.exit(0); } }
login方法里面的参数是当前要登录的账户,createchat方法中的,是聊天的对方的账号。运行时,liu在spark客户端登录,在本机的console就可以和liu交流了。
第二种涉及到广播,其实就一个方法。要使用广播功能,需要在openfire的服务器上安装一个广播的插件,不然是无法发送广播的。
package com.test; import java.util.Collection; import java.util.Scanner; import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.ChatManager; import org.jivesoftware.smack.MessageListener; import org.jivesoftware.smack.RosterEntry; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.packet.Message; public class IMTest { public static void main(String[] args) { // TODO Auto-generated method stub XMPPConnection.DEBUG_ENABLED=true; XMPPConnection connection=null; try { IMTest test=new IMTest(); connection=test.getConnection(); test.doLogin(connection); test.getRoster(connection); //test.sendMessage(connection); test.sendPacket(connection); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ connection.disconnect(); } } private XMPPConnection getConnection() throws XMPPException{ XMPPConnection con=new XMPPConnection("127.0.0.1"); con.connect(); return con; } private void doLogin(XMPPConnection connection) throws XMPPException{ connection.login("admin", "12345"); System.out.println(connection.getUser()+" has logined"); } private Collection<RosterEntry> getRoster(XMPPConnection connection){ Collection<RosterEntry> roster=connection.getRoster().getEntries(); for(RosterEntry entry:roster){ System.out.println("name :"+entry.getName()+",jid:"+entry.getUser()); } return roster; } private void sendMessage(XMPPConnection connection) throws XMPPException{ ChatManager chatManager=connection.getChatManager(); Chat chat=chatManager.createChat("liu@127.0.0.1", new MessageListener() { //获取对方消息 @Override public void processMessage(Chat chat, Message message) { // TODO Auto-generated method stub System.out.println(message.getFrom()+" say: "+message.getBody()); } }); //发送消息 Scanner inputScanner=new Scanner(System.in); while(true){ String messageString=inputScanner.nextLine(); System.out.println(connection.getUser()+" say:"+messageString); chat.sendMessage(messageString); } } private void sendPacket(XMPPConnection connection){ Message message=new Message(); message.setTo("all@broadcast.127.0.0.1"); message.setSubject("广播"); message.setBody("推送信息"); message.setType(Message.Type.normal);//离线支持 connection.sendPacket(message); connection.disconnect(); } }
发送广播的时候,setTo里面,@后面有broadcast标识,这个不能少。
不过目前广播消息,有时候会出现消息重复发送的情况。目前是打算采用上篇文章中所说的androidpn来做消息推送了,这里就没有再过多仔细研究。