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");	

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现消息的接收,需要使用Openfire提供的XMPP协议和Smack API。 首先,需要在代码中创建一个XMPP连接,连接到Openfire服务器。可以使用Smack API提供的XMPPTCPConnection类来创建连接,并传入Openfire服务器的IP地址、端口号和用户名密码等信息。 ```java XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder() .setServiceName(serverName) .setHost(serverIP) .setPort(serverPort) .setUsernameAndPassword(username, password) .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) .setCompressionEnabled(false); AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build()); ``` 然后,需要注册一个消息监听器,以便在收到消息时进行处理。可以使用Smack API提供的StanzaListener接口来实现监听器。 ```java connection.addAsyncStanzaListener(new StanzaListener() { @Override public void processStanza(Stanza stanza) throws SmackException.NotConnectedException, InterruptedException { if (stanza instanceof Message) { Message message = (Message) stanza; String fromUser = message.getFrom().toString(); String messageBody = message.getBody(); // 处理收到的消息 } } }, new StanzaFilter() { @Override public boolean accept(Stanza stanza) { return stanza instanceof Message; } }); ``` 最后,需要调用连接对象的connect()方法和login()方法,建立连接并登录到Openfire服务器。 ```java connection.connect(); connection.login(); ``` 这样,当有消息发送到当前用户时,就会触发消息监听器的processStanza()方法,从而实现消息的接收。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值