openfire系列之一 实现消息的发送

 代码如下:

/**
 * @author alex
 * @Date 2014-6-14 11:05:49
 * openfire通信管理类.
 * **/
public class OpenfireManager {
	
	private  XMPPConnection connection;
	public XMPPConnection getConnection() {
		return connection;
	}

	private String userName="admin";
	private String pwd="qq603665610";  
	
	private PacketListener listener;  
	
	private static OpenfireManager instance= new OpenfireManager();
	
	
	public static OpenfireManager getInstance()
	{
		return instance;
	}
	
	public void init(PacketListener listener,String userName,String pwd)
	{
		this.listener = listener;
		this.userName = userName;
		this.pwd = pwd;
		ConnectionConfiguration configuration = new ConnectionConfiguration("192.168.1.83", 5222);
        configuration.setDebuggerEnabled(true);
        configuration.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
        configuration.setSASLAuthenticationEnabled(false);
        configuration.setCompressionEnabled(false);
        configuration.setReconnectionAllowed(true);
        configuration.setSendPresence(false);
        connection = new XMPPConnection(configuration);
	}
	
	public void addConnectionListener(ConnectionListener l)
	{
		connection.addConnectionListener(l);
	}
	
	private OpenfireManager(){
		
	}
	
	
	synchronized public Chat createChat(String to) {
        Chat chat = null;
        if (isConnected()) {
            chat = connection.getChatManager().createChat(to, null);
        }
        return chat;
    }
	
	synchronized public boolean isConnected()
	{
		return connection!=null && connection.isConnected();
	}
	
	synchronized public boolean isLogin()
	{
		return connection!=null && connection.isAuthenticated();
	}
	
	synchronized public void sendMessage(final Chat chat, String body, String subject) throws Exception {
        final Message message = new Message();
        message.setBody(body);
        message.setSubject(subject);
        chat.sendMessage(message);
    }
	
	synchronized public void connect() throws XMPPException
	{
//		if(isConnected()) return;
         connection.connect();
         
         System.out.println("openfire is connected");
	}
	
	synchronized public void login() throws XMPPException
	{
		connection.login(userName, pwd);
		PacketFilter filter = new PacketTypeFilter(Message.class);
        connection.addPacketListener(listener, filter);
	}
	
	synchronized public void setOnLine() throws XMPPException
	{
		connection.sendPacket(new Presence(Presence.Type.available));
        System.out.println("openfire login");
		
	}

	synchronized public void disconnect()
	{
		if(connection!=null && connection.isConnected())
		{
			connection.removePacketListener(listener);
//			connection.sendPacket(new Presence(Presence.Type.unavailable));
			new Thread(){
				
				public void run()
				{
					try
					{
						connection.disconnect(new Presence(Presence.Type.unavailable));
					}
					catch(Exception e)
					{
						
					}
				}
				
				
			}.start();
			
		}
	}



}

 

实现消息发送:

try {
					if(OpenfireManager.getInstance().getConnection()==null){
						OpenfireManager.getInstance().init(UserAction.this, "admin", "qq603665610");
						OpenfireManager.getInstance().connect();
						
					}
					connection=OpenfireManager.getInstance().getConnection();
					if(!OpenfireManager.getInstance().isLogin()){
						OpenfireManager.getInstance().login();
					}
					Chat chat=createChat("client");
					String aa=connection.getUser();
					System.out.println(connection.getUser());
					OpenfireManager.getInstance().sendMessage(chat, "123456", "regCode");	

 

你可能感兴趣的:(openfire)