第一次用openfire 服务器,用smack 发送消息

来源:互联网 发布:java虚拟机内存设置 编辑:程序博客网 时间:2024/05/17 01:12

 
public class SendMessage{

    private XMPPConnection con;
    private ChatManager chatmanager;

 

  public XMPPConnection login(String messageProducer, String messageProducerPassword) {
        ConnectionConfiguration config = new ConnectionConfiguration(this.serverDomain,
                this.serverPort);
        XMPPConnection con = new XMPPConnection(config);
        try {
            con.connect();
        } catch (XMPPException e) {
            throw new RuntimeException("Connect xmpp server", e);
        }
        try {
            con.login(this.messageProducer, this.messageProducerPassword);
        } catch (XMPPException e) {
            throw new RuntimeException("login failed: " + e.getMessage() + " with "
                    + messageProducer + ":" + messageProducerPassword);
        }
        return con;

    }

 

    private void sendMessage(String to, String message) {
        try {
            Chat newChat = chatmanager.createChat(to, new MessageListener() {
                public void processMessage(Chat chat, Message message) {
                }
            });
            newChat.sendMessage(message);
        } catch (XMPPException e) {
            log.error("Got error while sending message to " + to.getUsername(), e);
        }
    }

    private void layout() {
        if (con != null && con.isConnected()) {
            con.disconnect();
        }
    }

}

原创粉丝点击