基于XMPP的Smack4.16与OpenFire建立连接并实现实时监听、发送与接收消息等基础功能

基于XMPP的Smack4.16与OpenFire建立连接并实现实时监听、发送与接收消息

import org.jivesoftware.smack.*;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.chat.ChatManagerListener;
import org.jivesoftware.smack.chat.ChatMessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.sasl.javax.SASLDigestMD5Mechanism;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;


import java.io.IOException;
public class ConnectionUtil  extends Thread{
    public  static  String jid = null;
    public boolean connect(String username,String password,String serverName) {
        boolean target=false;
        //Smack连接的基本配置
        XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder()
        .setCompressionEnabled(false)
        .setDebuggerEnabled(false)
        .setSendPresence(true)
         //用户名、密码
        .setUsernameAndPassword(username, password)
         //IP地址
        .setHost(serverName)
         //服务器名称
        .setServiceName("asmis-app")
         //端口
        .setPort(5222)
        .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled).setCompressionEnabled(false);
        SASLAuthentication.registerSASLMechanism(new SASLDigestMD5Mechanism());
        XMPPTCPConnection connection = new XMPPTCPConnection(config.build());
        try {
            connection.connect();
            target = connection.isConnected();
            if(target){
                System.out.println("XMPP 服务器连接成功");
                connection.login(username,password);
                System.out.println(username + "用户登录成功成功");
                System.out.println("Authenticated = " + connection.isAuthenticated());
                ChatManager chatmanager  = ChatManager.getInstanceFor(connection);
                 //实时监听
                chatmanager.addChatListener(new ChatManagerListener() {
                    @Override
                    public void chatCreated(Chat chat, boolean createdLocally) {
                        chat.addMessageListener(new ChatMessageListener() {
                            @Override
                            public void processMessage(Chat chat, Message message) {
                                //监听获取消息
                                System.out.println(message.getFrom());
                                System.out.println(message.getBody());
                                jid = message.getFrom().substring(0, message.getFrom().indexOf("/"));
                                //给发送者jid反馈消息
                                Chat newChat = chatmanager.createChat(jid);

                                try{
                                    newChat.sendMessage("对方已经收到你发送的消息:"+message.getBody());
                                }catch (Exception e){

                                }

                            }
                        });
                        //System.out.println(chat.toString());

                    }
                });
                //发送消息给指定对象
                //Chat newChat = chatmanager.createChat(jid);
                //newChat.sendMessage("我来咯");
      }else{
                System.out.println("XMPP 服务器连接失败");
            }
        } catch (SmackException | IOException | XMPPException e) {
            System.out.println(e.getMessage());
        }
        return target;
    }


}

你可能感兴趣的:(基于XMPP的Smack4.16与OpenFire建立连接并实现实时监听、发送与接收消息等基础功能)