java+smack+openfire即時通訊Im(二)

java+smack+openfire即時通訊Im(二)

前提

上一篇的註冊登錄完成以後,就可以開始聊天了

發送聊天信息

XMPPTCPConnection  connection = initOpenfireConnect();
connection.login("賬號","密碼");
ChatManager chatManager = ChatManager.getInstanceFor(connection);
Chat chat = chatManager.createChat("對方的jid(也就是用戶名@你的服務器名稱)");
Message newMessage = new Message();
newMessage.setBody("hahahahahahah");
chat.sendMessage(newMessage);

這樣就可以完成聊天信息發送了,注意,要先登錄過後才能發送聊天。但是涉及到文件類型的信息發送,沒有找到可實現的方法。所以處理辦法是從自己本地服務器做中轉。
詳情可查看之前的一篇博文:
使用strophe.js連接openfire服務器實現web端即時通訊

獲取好友列表

注意:只有雙方都同意加好友了,才能獲取到。對於未同意添加好友的用戶也沒找到具體怎麼去獲取,如果有已經實現的大佬,歡迎指點一下哈。萬分感謝!
這裏有個地方需要提前準備一下,因爲好友的在線狀態沒有在好友列表裏面直接返回,所以需要單獨去請求才行。登錄你的openfire服務器
用戶在線狀態插件
要獲取用戶的在線狀態,是需要openfire提供接口的!登錄服務器以後,點擊插件——有效的插件,然後找到Presence Service這個插件,點擊後面的+號進行安裝,安裝完成以後,進入服務器——服務器設置
服務器配置
記住這個地址,需要用到。下面開始上代碼

/***
     *
     * <B>方法名稱:</B>openfireGetFriendList<BR>
     * <B>概要說明:</B>通過當前賬號信息獲取賬號的好友信息<BR>
     *
     * @param userName
     * @param password
     * @return void
     *
     * @author ZhangYH
     * @date 2020/4/1 14:07
     */
    public static List<Map<String,Object>> openfireGetFriendList(String userName, String password) {
        List<Map<String,Object>> friendList = new LinkedList<>();
        try {
        	//初始化連接
            XMPPTCPConnection  connection = initOpenfireConnect();
            //登錄
            connection.login(userName, password);
            Roster roster = Roster.getInstanceFor(connection);
            //這個判斷很重要,如果Roster還沒加載完就去獲取好友數據的話只能獲取到空
            if (!roster.isLoaded()) {
                try {
                    roster.reloadAndWait();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            Collection<RosterEntry> rosterEntries = roster.getEntries();
            for(RosterEntry rosterEntry : rosterEntries){
                Map<String,Object> dataMap = new HashMap<>();
                //jid
                dataMap.put("userJid",rosterEntry.getUser());
                if(StringUtils.isEmpty(rosterEntry.getName())){
                	//前端顯示的暱稱
                    dataMap.put("nickName",rosterEntry.getUser().split("@")[0]);
                }else{
                    dataMap.put("nickName",rosterEntry.getName());
                }
                //通過用戶的JID獲取用戶當前的在線狀態
                int status = openfireGetFriendOnlineStatus(rosterEntry.getUser());
                //如果在線狀態爲0,說明用戶不存在,跳過
                if (status == 0) {
                    continue;
                }
                if (status == 1) {
                	//在線
                    dataMap.put("status",1);
                }else{
                	//離線
                    dataMap.put("status",0);
                }
                friendList.add(dataMap);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return friendList;
    }
    /***
     *
     * <B>方法名稱:</B>openfireGetFriendOnlineStatus<BR>
     * <B>概要說明:</B>獲取用戶在線狀態<BR>
     *
     * @param userJid
     * @return int
     *
     * @author ZhangYH
     * @date 2020/4/1 16:13
     */
    public static int openfireGetFriendOnlineStatus(String userJid) {
        int shOnLineState = 0;    //-不存在-
        try {
        	//OPENFIRE_PRESENCE_URL就是你服務器配置的獲取在線狀態的哪兒地址,設置type=xml,如果不設置,地址返回的就是一張服務器默認的在線狀態的圖片
            String strUrl = OPENFIRE_PRESENCE_URL+"?jid="+userJid+"&type=xml";
            URL oUrl = new URL(strUrl);
            URLConnection  oConn = oUrl.openConnection();
            if(oConn!=null){
                BufferedReader oIn = new BufferedReader(new InputStreamReader(oConn.getInputStream()));
                if(null!=oIn){
                    String strFlag = oIn.readLine();
                    oIn.close();
                    if(strFlag.indexOf("type=\"unavailable\"")>=0){
                    	//通過匹配返回的xml信息,unavailable爲不在線
                        shOnLineState = 2;
                    }else if(strFlag.indexOf("type=\"error\"")>=0){
                    	//發生錯誤,說明用戶不存在或發生了其它異常
                        shOnLineState = 0;
                    }else if(strFlag.indexOf("priority")>=0){
                    	//成功的狀態
                        shOnLineState = 1;
                    }else{
                        shOnLineState = 1;
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return shOnLineState;
    }

歡迎各位大佬交流指正!

上一篇:java+smack+openfire即時通訊Im(一)
下一篇:java+smack+openfire即時通訊Im(三)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章