日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

基于xmpp openfire smack开发之smack类库介绍和使用[2]

發布時間:2025/7/14 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于xmpp openfire smack开发之smack类库介绍和使用[2] 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

關于Smack編程庫,前面我們提到,它是面向Java端的api,主要在PC上使用,利用它我們可以向openfire服務器注冊用戶,發送消息,并且可以通過監聽器獲得此用戶的應答消息,以及構建聊天室,分組,個人通訊錄等等。

下面我們寫幾個程序小例子測試一下。
(1)登錄操作

?? ??? ??? ?PPConnection.DEBUG_ENABLED = true;
?? ??? ??? ?AccountManager accountManager;
?? ??? ??? ?final ConnectionConfiguration connectionConfig = new ConnectionConfiguration(
?? ??? ??? ??? ??? ?"192.168.1.78", Integer.parseInt("5222"), "csdn.shimiso.com");
??? ?
?? ??? ??? ?// 允許自動連接
?? ??? ??? ?connectionConfig.setReconnectionAllowed(true);
?? ??? ??? ?connectionConfig.setSendPresence(true);
??? ?
?? ??? ??? ?Connection connection = new XMPPConnection(connectionConfig);
?? ??? ??? ?try {
?? ??? ??? ??? ?connection.connect();// 開啟連接
?? ??? ??? ??? ?accountManager = connection.getAccountManager();// 獲取賬戶管理類
?? ??? ??? ?} catch (XMPPException e) {
?? ??? ??? ??? ?throw new IllegalStateException(e);
?? ??? ??? ?}
??? ?
?? ??? ??? ?// 登錄
?? ??? ??? ?connection.login("admin", "admin","SmackTest");
?? ??? ??? ?System.out.println(connection.getUser());
?? ??? ??? ?connection.getChatManager().createChat("shimiso@csdn.shimiso.com",null).sendMessage("Hello word!");

運行結果:

在login中一共有三個參數,登錄名,密碼,資源名,可能有人不明白資源名到底是什么意思,其實就是客戶端的來源,客戶端的名稱,如果不寫它默認就叫smack,如果你用相同的賬戶不同的資源名和同一個人發三條消息,那將會彈出三個窗口,而不是一個窗口。
同時smack還為我們提供了非常好的調試工具Smack Debug,利用該工具我們可以準確的捕獲詳細的往返報文信息。
(2)下面我們繼續寫個聊天的例子:

?? ??? ??? ?PPConnection.DEBUG_ENABLED = true;
?? ??? ??? ?AccountManager accountManager;
?? ??? ??? ?final ConnectionConfiguration connectionConfig = new ConnectionConfiguration(
?? ??? ??? ??? ??? ?"192.168.1.78", Integer.parseInt("5222"), "csdn.shimiso.com");
??? ?
?? ??? ??? ?// 允許自動連接
?? ??? ??? ?connectionConfig.setReconnectionAllowed(true);
?? ??? ??? ?connectionConfig.setSendPresence(true);
??? ?
?? ??? ??? ?Connection connection = new XMPPConnection(connectionConfig);
?? ??? ??? ?try {
?? ??? ??? ??? ?connection.connect();// 開啟連接
?? ??? ??? ??? ?accountManager = connection.getAccountManager();// 獲取賬戶管理類
?? ??? ??? ?} catch (XMPPException e) {
?? ??? ??? ??? ?throw new IllegalStateException(e);
?? ??? ??? ?}
??? ?
?? ??? ??? ?// 登錄
?? ??? ??? ?connection.login("admin", "admin","SmackTest3"); ?
?? ??? ??? ?ChatManager chatmanager = connection.getChatManager();
?? ??? ??? ?Chat newChat = chatmanager.createChat("shimiso@csdn.shimiso.com", new MessageListener() {
?? ??? ??? ??? ?public void processMessage(Chat chat, Message message) {
?? ??? ??? ??? ??? ?if (message.getBody() != null) {
?? ??? ??? ??? ??? ??? ?System.out.println("Received from 【"
?? ??? ??? ??? ??? ??? ??? ??? ?+ message.getFrom() + "】 message: "
?? ??? ??? ??? ??? ??? ??? ??? ?+ message.getBody());
?? ??? ??? ??? ??? ?}
??? ?
?? ??? ??? ??? ?}
?? ??? ??? ?});
?? ??? ??? ?Scanner input = new Scanner(System.in);
?? ??? ??? ?while (true) {
?? ??? ??? ??? ?String message = input.nextLine();
?? ??? ??? ??? ?newChat.sendMessage(message);
?? ??? ??? ?}

運行結果:

這里我們用Scanner來捕捉用戶在控制臺的鍵盤操作,將信息發出,同時創建了一個MessageListener監聽,在其中強制實現processMessage方法即可捕獲發回的信息,在初次使用上還是較為容易上手的,我們只要細心查看API即可逐步深入下去。
(3)除了聊天以外我們經常還能想到就是廣播

需要給所有在線的用戶發送一個通知,或者給所有在線和離線的用戶全發送,我們先演示如何給在線用戶發送一個廣播:

?? ??? ??? ?PPConnection.DEBUG_ENABLED = false;
?? ??? ??? ?AccountManager accountManager;
?? ??? ??? ?final ConnectionConfiguration connectionConfig = new ConnectionConfiguration(
?? ??? ??? ??? ??? ?"192.168.1.78", Integer.parseInt("5222"), "csdn.shimiso.com");
??? ?
?? ??? ??? ?// 允許自動連接
?? ??? ??? ?connectionConfig.setReconnectionAllowed(true);
?? ??? ??? ?connectionConfig.setSendPresence(true);
??? ?
?? ??? ??? ?Connection connection = new XMPPConnection(connectionConfig);
?? ??? ??? ?try {
?? ??? ??? ??? ?connection.connect();// 開啟連接
?? ??? ??? ??? ?accountManager = connection.getAccountManager();// 獲取賬戶管理類
?? ??? ??? ?} catch (XMPPException e) {
?? ??? ??? ??? ?throw new IllegalStateException(e);
?? ??? ??? ?}
?? ??? ??? ?connection.login("admin", "admin","SmackTest3");
?? ??? ??? ?Message newmsg = new Message();
?? ??? ??? ?newmsg.setTo("shimiso@csdn.shimiso.com");
?? ??? ??? ?newmsg.setSubject("重要通知");
?? ??? ??? ?newmsg.setBody("今天下午2點60分有會!");
?? ??? ??? ?newmsg.setType(Message.Type.headline);// normal支持離線
?? ??? ??? ?connection.sendPacket(newmsg);
?? ??? ??? ?connection.disconnect();

運行結果:

將參數設置為Message.Type.normal即可支持離線廣播,openfire系統會自動判斷該用戶是否在線,如果在線就直接發送出去,如果不在線則將信息存入ofoffline表,現在我將shimiso用戶退出登錄,再給它發消息,我們可以進入openfire庫的ofoffline表中,非常清楚看到里面躺著一條離線消息記錄是發給shimiso這個用戶的

(4)那么我們如何讓shimiso這個用戶一登陸就取到離線消息呢?

請看如下代碼

?? ??? ??? ?PPConnection.DEBUG_ENABLED = false;
?? ??? ??? ?AccountManager accountManager;
?? ??? ??? ?final ConnectionConfiguration connectionConfig = new ConnectionConfiguration(
?? ??? ??? ??? ??? ?"192.168.1.78", Integer.parseInt("5222"), "csdn.shimiso.com");
??? ?
?? ??? ??? ?// 允許自動連接
?? ??? ??? ?connectionConfig.setReconnectionAllowed(true);
?? ??? ??? ?connectionConfig.setSendPresence(false);//不要告訴服務器自己的狀態
?? ??? ??? ?Connection connection = new XMPPConnection(connectionConfig);
?? ??? ??? ?try {
?? ??? ??? ??? ?connection.connect();// 開啟連接
?? ??? ??? ??? ?accountManager = connection.getAccountManager();// 獲取賬戶管理類
?? ??? ??? ?} catch (XMPPException e) {
?? ??? ??? ??? ?throw new IllegalStateException(e);
?? ??? ??? ?}
?? ??? ??? ?connection.login("shimiso", "123","SmackTest");
?? ??? ??? ?OfflineMessageManager offlineManager = new OfflineMessageManager(
?? ??? ??? ??? ??? ?connection);
?? ??? ??? ?try {
?? ??? ??? ??? ?Iterator<org.jivesoftware.smack.packet.Message> it = offlineManager
?? ??? ??? ??? ??? ??? ?.getMessages();
??? ?
?? ??? ??? ??? ?System.out.println(offlineManager.supportsFlexibleRetrieval());
?? ??? ??? ??? ?System.out.println("離線消息數量: " + offlineManager.getMessageCount());
??? ?
?? ??? ??? ??? ?Map<String, ArrayList<Message>> offlineMsgs = new HashMap<String, ArrayList<Message>>();
??? ?
?? ??? ??? ??? ?while (it.hasNext()) {
?? ??? ??? ??? ??? ?org.jivesoftware.smack.packet.Message message = it.next();
?? ??? ??? ??? ??? ?System.out
?? ??? ??? ??? ??? ??? ??? ?.println("收到離線消息, Received from 【" + message.getFrom()
?? ??? ??? ??? ??? ??? ??? ??? ??? ?+ "】 message: " + message.getBody());
?? ??? ??? ??? ??? ?String fromUser = message.getFrom().split("/")[0];
??? ?
?? ??? ??? ??? ??? ?if (offlineMsgs.containsKey(fromUser)) {
?? ??? ??? ??? ??? ??? ?offlineMsgs.get(fromUser).add(message);
?? ??? ??? ??? ??? ?} else {
?? ??? ??? ??? ??? ??? ?ArrayList<Message> temp = new ArrayList<Message>();
?? ??? ??? ??? ??? ??? ?temp.add(message);
?? ??? ??? ??? ??? ??? ?offlineMsgs.put(fromUser, temp);
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
??? ?
?? ??? ??? ??? ?// 在這里進行處理離線消息集合......
?? ??? ??? ??? ?Set<String> keys = offlineMsgs.keySet();
?? ??? ??? ??? ?Iterator<String> offIt = keys.iterator();
?? ??? ??? ??? ?while (offIt.hasNext()) {
?? ??? ??? ??? ??? ?String key = offIt.next();
?? ??? ??? ??? ??? ?ArrayList<Message> ms = offlineMsgs.get(key);
??? ?
?? ??? ??? ??? ??? ?for (int i = 0; i < ms.size(); i++) {
?? ??? ??? ??? ??? ??? ?System.out.println("-->" + ms.get(i));
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
??? ?
?? ??? ??? ??? ?offlineManager.deleteMessages();
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ??? ?offlineManager.deleteMessages();//刪除所有離線消息
?? ??? ??? ?Presence presence = new Presence(Presence.Type.available);
?????????? ??? ??? ?nnection.sendPacket(presence);//上線了
?????????? ??? ??? ?nnection.disconnect();//關閉連接

運行結果:

這里我們需要特別當心的是先不要告訴openfire服務器你上線了,否則永遠也拿不到離線消息,用下面英文大概意思就是在你上線之前去獲取離線消息,這么設計是很有道理的。

The OfflineMessageManager helps manage offline messages even before the user has sent an available presence. When a user asks for his offline messages before sending an available presence then the server will not send a flood with all the offline messages when the user becomes online. The server will not send a flood with all the offline messages to the session that made the offline messages request or to any other session used by the user that becomes online.

拿到離線消息處理完畢之后刪除離線消息offlineManager.deleteMessages() 接著通知服務器上線了。
(5)下面我們來看看如何來發送文件

?? ??? ??? ?PPConnection.DEBUG_ENABLED = false;
?? ??? ??? ?AccountManager accountManager;
?? ??? ??? ?final ConnectionConfiguration connectionConfig = new ConnectionConfiguration(
?? ??? ??? ??? ??? ?"192.168.1.78", Integer.parseInt("5222"), "csdn.shimiso.com");
??? ?
?? ??? ??? ?// 允許自動連接
?? ??? ??? ?connectionConfig.setReconnectionAllowed(true);
?? ??? ??? ?connectionConfig.setSendPresence(true);
??? ?
?? ??? ??? ?Connection connection = new XMPPConnection(connectionConfig);
?? ??? ??? ?try {
?? ??? ??? ??? ?connection.connect();// 開啟連接
?? ??? ??? ??? ?accountManager = connection.getAccountManager();// 獲取賬戶管理類
?? ??? ??? ?} catch (XMPPException e) {
?? ??? ??? ??? ?throw new IllegalStateException(e);
?? ??? ??? ?}
?? ??? ??? ?? connection.login("admin", "admin","Rooyee");
?? ??? ??? ?? Presence pre = connection.getRoster().getPresence("shimiso@csdn.shimiso.com");
?? ??? ??? ??? ?System.out.println(pre);
?? ??? ??? ??? ?if (pre.getType() != Presence.Type.unavailable) {
?? ??? ??? ??? ??? ?// 創建文件傳輸管理器
?? ??? ??? ??? ??? ?FileTransferManager manager = new FileTransferManager(connection);
?? ??? ??? ??? ??? ?// 創建輸出的文件傳輸
?? ??? ??? ??? ??? ?OutgoingFileTransfer transfer = manager
?? ??? ??? ??? ??? ??? ??? ?.createOutgoingFileTransfer(pre.getFrom());
?? ??? ??? ??? ??? ?// 發送文件
?? ??? ??? ??? ??? ?transfer.sendFile(new File("E:\\Chrysanthemum.jpg"), "圖片");
?? ??? ??? ??? ??? ?while (!transfer.isDone()) {
?? ??? ??? ??? ??? ??? ?if (transfer.getStatus() == FileTransfer.Status.in_progress) {
?? ??? ??? ??? ??? ??? ??? ?// 可以調用transfer.getProgress();獲得傳輸的進度 
?? ??? ??? ??? ??? ??? ??? ?System.out.println(transfer.getStatus());
?? ??? ??? ??? ??? ??? ??? ?System.out.println(transfer.getProgress());
?? ??? ??? ??? ??? ??? ??? ?System.out.println(transfer.isDone());
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}

運行結果:

在這里我們需要特別注意的是,跨資源是無法發送文件的,看connection.login("admin", "admin","Rooyee");這個代碼就明白了,必須是“域名和資源名”完全相同的兩個用戶才可以互發文件,否則永遠都沒反應,如果不清楚自己所用的客戶端的資源名,可以借助前面提到的SmackDebug工具查看往返信息完整報文,在to和from中一定可以看到。

如果我們自己要寫文件接收例子的話,參考代碼如下:

??? FileTransferManager transfer = new FileTransferManager(connection);
??? transfer.addFileTransferListener(new RecFileTransferListener());
??? public class RecFileTransferListener implements FileTransferListener {
??? ?
?? ??? ?public String getFileType(String fileFullName) {
?? ??? ??? ?if (fileFullName.contains(".")) {
?? ??? ??? ??? ?return "." + fileFullName.split("//.")[1];
?? ??? ??? ?} else {
?? ??? ??? ??? ?return fileFullName;
?? ??? ??? ?}
??? ?
?? ??? ?}
??? ?
?? ??? ?@Override
?? ??? ?public void fileTransferRequest(FileTransferRequest request) {
?? ??? ??? ?System.out.println("接收文件開始.....");
?? ??? ??? ?final IncomingFileTransfer inTransfer = request.accept();
?? ??? ??? ?final String fileName = request.getFileName();
?? ??? ??? ?long length = request.getFileSize();
?? ??? ??? ?final String fromUser = request.getRequestor().split("/")[0];
?? ??? ??? ?System.out.println("文件大小:" + length + "? " + request.getRequestor());
?? ??? ??? ?System.out.println("" + request.getMimeType());
?? ??? ??? ?try {
??? ?
?? ??? ??? ??? ?JFileChooser chooser = new JFileChooser();
?? ??? ??? ??? ?chooser.setCurrentDirectory(new File("."));
??? ?
?? ??? ??? ??? ?int result = chooser.showOpenDialog(null);
??? ?
?? ??? ??? ??? ?if (result == JFileChooser.APPROVE_OPTION) {
?? ??? ??? ??? ??? ?final File file = chooser.getSelectedFile();
?? ??? ??? ??? ??? ?System.out.println(file.getAbsolutePath());
?? ??? ??? ??? ??? ?new Thread() {
?? ??? ??? ??? ??? ??? ?public void run() {
?? ??? ??? ??? ??? ??? ??? ?try {
??? ?
?? ??? ??? ??? ??? ??? ??? ??? ?System.out.println("接受文件: " + fileName);
?? ??? ??? ??? ??? ??? ??? ??? ?inTransfer
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?.recieveFile(new File(file
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?.getAbsolutePath()
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?+ getFileType(fileName)));
??? ?
?? ??? ??? ??? ??? ??? ??? ??? ?Message message = new Message();
?? ??? ??? ??? ??? ??? ??? ??? ?message.setFrom(fromUser);
?? ??? ??? ??? ??? ??? ??? ??? ?message.setProperty("REC_SIGN", "SUCCESS");
?? ??? ??? ??? ??? ??? ??? ??? ?message.setBody("[" + fromUser + "]發送文件: "
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?+ fileName + "/r/n" + "存儲位置: "
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?+ file.getAbsolutePath()
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?+ getFileType(fileName));
?? ??? ??? ??? ??? ??? ??? ??? ?if (Client.isChatExist(fromUser)) {
?? ??? ??? ??? ??? ??? ??? ??? ??? ?Client.getChatRoom(fromUser)
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?.messageReceiveHandler(message);
?? ??? ??? ??? ??? ??? ??? ??? ?} else {
?? ??? ??? ??? ??? ??? ??? ??? ??? ?ChatFrameThread cft = new ChatFrameThread(
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?fromUser, message);
?? ??? ??? ??? ??? ??? ??? ??? ??? ?cft.start();
??? ?
?? ??? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ?} catch (Exception e2) {
?? ??? ??? ??? ??? ??? ??? ??? ?e2.printStackTrace();
?? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}.start();
?? ??? ??? ??? ?} else {
??? ?
?? ??? ??? ??? ??? ?System.out.println("拒絕接受文件: " + fileName);
??? ?
?? ??? ??? ??? ??? ?request.reject();
?? ??? ??? ??? ??? ?Message message = new Message();
?? ??? ??? ??? ??? ?message.setFrom(fromUser);
?? ??? ??? ??? ??? ?message.setBody("拒絕" + fromUser + "發送文件: " + fileName);
?? ??? ??? ??? ??? ?message.setProperty("REC_SIGN", "REJECT");
?? ??? ??? ??? ??? ?if (Client.isChatExist(fromUser)) {
?? ??? ??? ??? ??? ??? ?Client.getChatRoom(fromUser).messageReceiveHandler(message);
?? ??? ??? ??? ??? ?} else {
?? ??? ??? ??? ??? ??? ?ChatFrameThread cft = new ChatFrameThread(fromUser, message);
?? ??? ??? ??? ??? ??? ?cft.start();
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
??? ?
?? ??? ??? ??? ?/*
?? ??? ??? ??? ? * InputStream in = inTransfer.recieveFile();
?? ??? ??? ??? ? *
?? ??? ??? ??? ? * String fileName = "r"+inTransfer.getFileName();
?? ??? ??? ??? ? *
?? ??? ??? ??? ? * OutputStream out = new FileOutputStream(new
?? ??? ??? ??? ? * File("d:/receive/"+fileName)); byte[] b = new byte[512];
?? ??? ??? ??? ? * while(in.read(b) != -1) { out.write(b); out.flush(); }
?? ??? ??? ??? ? *
?? ??? ??? ??? ? * in.close(); out.close();
?? ??? ??? ??? ? */
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
??? ?
?? ??? ??? ?System.out.println("接收文件結束.....");
??? ?
?? ??? ?}
??? ?
??? }

(6)用戶列表

?? ??? ?**
?? ??? ? * 返回所有組信息 <RosterGroup>
?? ??? ? *
?? ??? ? * @return List(RosterGroup)
?? ??? ? */
?? ??? ?public static List<RosterGroup> getGroups(Roster roster) {
?? ??? ??? ?List<RosterGroup> groupsList = new ArrayList<RosterGroup>();
?? ??? ??? ?Collection<RosterGroup> rosterGroup = roster.getGroups();
?? ??? ??? ?Iterator<RosterGroup> i = rosterGroup.iterator();
?? ??? ??? ?while (i.hasNext())
?? ??? ??? ??? ?groupsList.add(i.next());
?? ??? ??? ?return groupsList;
?? ??? ?}
??? ?
?? ??? ?/**
?? ??? ? * 返回相應(groupName)組里的所有用戶<RosterEntry>
?? ??? ? *
?? ??? ? * @return List(RosterEntry)
?? ??? ? */
?? ??? ?public static List<RosterEntry> getEntriesByGroup(Roster roster,
?? ??? ??? ??? ?String groupName) {
?? ??? ??? ?List<RosterEntry> EntriesList = new ArrayList<RosterEntry>();
?? ??? ??? ?RosterGroup rosterGroup = roster.getGroup(groupName);
?? ??? ??? ?Collection<RosterEntry> rosterEntry = rosterGroup.getEntries();
?? ??? ??? ?Iterator<RosterEntry> i = rosterEntry.iterator();
?? ??? ??? ?while (i.hasNext())
?? ??? ??? ??? ?EntriesList.add(i.next());
?? ??? ??? ?return EntriesList;
?? ??? ?}
??? ?
?? ??? ?/**
?? ??? ? * 返回所有用戶信息 <RosterEntry>
?? ??? ? *
?? ??? ? * @return List(RosterEntry)
?? ??? ? */
?? ??? ?public static List<RosterEntry> getAllEntries(Roster roster) {
?? ??? ??? ?List<RosterEntry> EntriesList = new ArrayList<RosterEntry>();
?? ??? ??? ?Collection<RosterEntry> rosterEntry = roster.getEntries();
?? ??? ??? ?Iterator<RosterEntry> i = rosterEntry.iterator();
?? ??? ??? ?while (i.hasNext())
?? ??? ??? ??? ?EntriesList.add(i.next());
?? ??? ??? ?return EntriesList;
?? ??? ?}

(7)用戶頭像的獲取

使用VCard,很強大,具體自己看API吧,可以看看VCard傳回來XML的組成,含有很多信息的

?? ??? ?**
?? ??? ? * 獲取用戶的vcard信息
?? ??? ? * @param connection
?? ??? ? * @param user
?? ??? ? * @return
?? ??? ? * @throws XMPPException
?? ??? ? */
?? ??? ?public static VCard getUserVCard(XMPPConnection connection, String user) throws XMPPException
?? ??? ?{
?? ??? ??? ?VCard vcard = new VCard();
?? ??? ??? ?vcard.load(connection, user);
?? ??? ??? ?
?? ??? ??? ?return vcard;
?? ??? ?}
??? ?
?? ??? ?/**
?? ??? ? * 獲取用戶頭像信息
?? ??? ? */
?? ??? ?public static ImageIcon getUserImage(XMPPConnection connection, String user) {
?? ??? ??? ?ImageIcon ic = null;
?? ??? ??? ?try {
?? ??? ??? ??? ?System.out.println("獲取用戶頭像信息: "+user);
?? ??? ??? ??? ?VCard vcard = new VCard();
?? ??? ??? ??? ?vcard.load(connection, user);
?? ??? ??? ??? ?
?? ??? ??? ??? ?if(vcard == null || vcard.getAvatar() == null)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?return null;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?ByteArrayInputStream bais = new ByteArrayInputStream(
?? ??? ??? ??? ??? ??? ?vcard.getAvatar());
?? ??? ??? ??? ?Image image = ImageIO.read(bais);
?? ??? ?
?? ??? ??? ??? ?
?? ??? ??? ???? ic = new ImageIcon(image);
?? ??? ??? ??? ?System.out.println("圖片大小:"+ic.getIconHeight()+" "+ic.getIconWidth());
?? ??? ??? ?
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ??? ?return ic;
?? ??? ?}

(8)組操作和用戶分組操作

?? ??? ?**
?? ??? ? * 添加一個組
?? ??? ? */
?? ??? ?public static boolean addGroup(Roster roster,String groupName)
?? ??? ?{
?? ??? ??? ?try {
?? ??? ??? ??? ?roster.createGroup(groupName);
?? ??? ??? ??? ?return true;
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?/**
?? ??? ? * 刪除一個組
?? ??? ? */
?? ??? ?public static boolean removeGroup(Roster roster,String groupName)
?? ??? ?{
?? ??? ??? ?return false;
?? ??? ?}
?? ??? ?
?? ??? ?/**
?? ??? ? * 添加一個好友? 無分組
?? ??? ? */
?? ??? ?public static boolean addUser(Roster roster,String userName,String name)
?? ??? ?{
?? ??? ??? ?try {
?? ??? ??? ??? ?roster.createEntry(userName, name, null);
?? ??? ??? ??? ?return true;
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?/**
?? ??? ? * 添加一個好友到分組
?? ??? ? * @param roster
?? ??? ? * @param userName
?? ??? ? * @param name
?? ??? ? * @return
?? ??? ? */
?? ??? ?public static boolean addUser(Roster roster,String userName,String name,String groupName)
?? ??? ?{
?? ??? ??? ?try {
?? ??? ??? ??? ?roster.createEntry(userName, name,new String[]{ groupName});
?? ??? ??? ??? ?return true;
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?/**
?? ??? ? * 刪除一個好友
?? ??? ? * @param roster
?? ??? ? * @param userName
?? ??? ? * @return
?? ??? ? */
?? ??? ?public static boolean removeUser(Roster roster,String userName)
?? ??? ?{
?? ??? ??? ?try {
?? ??? ??? ??? ?
?? ??? ??? ??? ?if(userName.contains("@"))
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?userName = userName.split("@")[0];
?? ??? ??? ??? ?}
?? ??? ??? ??? ?RosterEntry entry = roster.getEntry(userName);
?? ??? ??? ??? ?System.out.println("刪除好友:"+userName);
?? ??? ??? ??? ?System.out.println("User: "+(roster.getEntry(userName) == null));
?? ??? ??? ??? ?roster.removeEntry(entry);
?? ??? ??? ??? ?
?? ??? ??? ??? ?return true;
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ?}

(9)用戶查詢

??? public static List<UserBean> searchUsers(XMPPConnection connection,String serverDomain,String userName) throws XMPPException
?? ??? ?{
?? ??? ??? ?List<UserBean> results = new ArrayList<UserBean>();
?? ??? ??? ?System.out.println("查詢開始..............."+connection.getHost()+connection.getServiceName());
?? ??? ??? ?
?? ??? ??? ?UserSearchManager usm = new UserSearchManager(connection);
?? ??? ??? ?
?? ??? ??? ?
??????????? Form searchForm = usm.getSearchForm(serverDomain);
??????????? Form answerForm = searchForm.createAnswerForm();
??????????? answerForm.setAnswer("Username", true);
??????????? answerForm.setAnswer("search", userName);
??????????? ReportedData data = usm.getSearchResults(answerForm, serverDomain);
?? ??? ??? ?
?? ??? ??? ? Iterator<Row> it = data.getRows();
?? ??? ??? ? Row row = null;
?? ??? ??? ? UserBean user = null;
?? ??? ??? ? while(it.hasNext())
?? ??? ??? ? {
?? ??? ??? ??? ? user = new UserBean();
?? ??? ??? ??? ? row = it.next();
?? ??? ??? ??? ? user.setUserName(row.getValues("Username").next().toString());
?? ??? ??? ??? ? user.setName(row.getValues("Name").next().toString());
?? ??? ??? ??? ? user.setEmail(row.getValues("Email").next().toString());
?? ??? ??? ??? ? System.out.println(row.getValues("Username").next());
?? ??? ??? ??? ? System.out.println(row.getValues("Name").next());
?? ??? ??? ??? ? System.out.println(row.getValues("Email").next());
?? ??? ??? ??? ? results.add(user);
?? ??? ??? ??? ? //若存在,則有返回,UserName一定非空,其他兩個若是有設,一定非空
?? ??? ??? ? }
?? ??? ??? ?
?? ??? ??? ? return results;
?? ??? ?}

(10)修改自身狀態

包括上線,隱身,對某人隱身,對某人上線

?? ??? ?ublic static void updateStateToAvailable(XMPPConnection connection)
?? ??? ?{
?? ??? ??? ?Presence presence = new Presence(Presence.Type.available);
?????????? ??? ??? ?nnection.sendPacket(presence);
??????? ??? ?
?? ??? ?
?? ??? ?public static void updateStateToUnAvailable(XMPPConnection connection)
?? ??? ?{
?? ??? ??? ?Presence presence = new Presence(Presence.Type.unavailable);
?????????? ??? ??? ?nnection.sendPacket(presence);
?????? ??? ?}
?? ??? ?
?? ??? ?public static void updateStateToUnAvailableToSomeone(XMPPConnection connection,String userName)
?? ??? ?{
?? ??? ??? ?Presence presence = new Presence(Presence.Type.unavailable);
?? ??? ??? ?presence.setTo(userName);
?????????? ??? ??? ?nnection.sendPacket(presence);
?? ??? ?}
?? ??? ?public static void updateStateToAvailableToSomeone(XMPPConnection connection,String userName)
?? ??? ?{
?? ??? ??? ?Presence presence = new Presence(Presence.Type.available);
?? ??? ??? ?presence.setTo(userName);
?????????? ??? ??? ?nnection.sendPacket(presence);
??? ?
?? ??? ?}

(11)心情修改

?? ??? ?**
?? ??? ? * 修改心情
?? ??? ? * @param connection
?? ??? ? * @param status
?? ??? ? */
?? ??? ?public static void changeStateMessage(XMPPConnection connection,String status)
?? ??? ?{
?? ??? ??? ?Presence presence = new Presence(Presence.Type.available);
?? ??? ??? ?presence.setStatus(status);
?? ??? ??? ?connection.sendPacket(presence);
?? ??? ?
?? ??? ?}

(12)修改用戶頭像

有點麻煩,主要是讀入圖片文件,編碼,傳輸之

??? public static void changeImage(XMPPConnection connection,File f) throws XMPPException, IOException{
?? ??? ?
?? ??? ??? ?VCard vcard = new VCard();
?? ??? ??? ?vcard.load(connection);
?? ??? ??? ?
?? ??? ???????? byte[] bytes;
?? ??? ????? ?
?? ??? ???????????? bytes = getFileBytes(f);
?? ??? ???????????? String encodedImage = StringUtils.encodeBase64(bytes);
?? ??? ???????????? vcard.setAvatar(bytes, encodedImage);
?? ??? ???????????? vcard.setEncodedImage(encodedImage);
?? ??? ???????????? vcard.setField("PHOTO", "<TYPE>image/jpg</TYPE><BINVAL>"
?? ??? ???????????????????? + encodedImage + "</BINVAL>", true);
?? ??? ??????????? ?
?? ??? ??????????? ?
?? ??? ???????????? ByteArrayInputStream bais = new ByteArrayInputStream(
?? ??? ??? ??? ??? ??? ??? ?vcard.getAvatar());
?? ??? ??? ??? ??? ?Image image = ImageIO.read(bais);
?? ??? ??? ??? ??? ?ImageIcon ic = new ImageIcon(image);
?? ??? ??? ??? ??? ?
?? ??? ?????? ?
?? ??? ????? ?
?? ??? ???????? vcard.save(connection);
?? ??? ?????? ?
?? ??? ?}
?? ??? ?
????????? private static byte[] getFileBytes(File file) throws IOException {
?????????? ??? ??? ?BufferedInputStream bis = null;
?????????? ??? ?try {
??????????????? bis = new BufferedInputStream(new FileInputStream(file));
??????????????? int bytes = (int) file.length();
??????????????? byte[] buffer = new byte[bytes];
??????????????? int readBytes = bis.read(buffer);
??????????????? if (readBytes != buffer.length) {
??????????????????? throw new IOException("Entire file not read");
??????????????? }
??????????????? return buffer;
??????????? } finally {
??????????????? if (bis != null) {
??????????????????? bis.close();
??????????????? }
??????????? }
??? }

(13)用戶狀態的監聽

即對方改變頭像,狀態,心情時,更新自己用戶列表,其實這里已經有smack實現的監聽器

?? ??? ??? ?nal Roster roster = Client.getRoster();
?? ??? ??? ?
?? ??? ??? ?roster.addRosterListener(
?? ??? ??? ??? ?new RosterListener() {
??? ?
?? ??? ??? ??? ??? ??? ?@Override
?? ??? ??? ??? ??? ??? ?public void entriesAdded(Collection<String> arg0) {
?? ??? ??? ??? ??? ??? ??? ?// TODO Auto-generated method stub
?? ??? ??? ??? ??? ??? ??? ?System.out.println("--------EE:"+"entriesAdded");
?? ??? ??? ??? ??? ??? ?}
??? ?
?? ??? ??? ??? ??? ??? ?@Override
?? ??? ??? ??? ??? ??? ?public void entriesDeleted(Collection<String> arg0) {
?? ??? ??? ??? ??? ??? ??? ?// TODO Auto-generated method stub
?? ??? ??? ??? ??? ??? ??? ?System.out.println("--------EE:"+"entriesDeleted");
?? ??? ??? ??? ??? ??? ?}
??? ?
?? ??? ??? ??? ??? ??? ?@Override
?? ??? ??? ??? ??? ??? ?public void entriesUpdated(Collection<String> arg0) {
?? ??? ??? ??? ??? ??? ??? ?// TODO Auto-generated method stub
?? ??? ??? ??? ??? ??? ??? ?System.out.println("--------EE:"+"entriesUpdated");
?? ??? ??? ??? ??? ??? ?}
??? ?
?? ??? ??? ??? ??? ??? ?@Override
?? ??? ??? ??? ??? ??? ?public void presenceChanged(Presence arg0) {
?? ??? ??? ??? ??? ??? ??? ?// TODO Auto-generated method stub
?? ??? ??? ??? ??? ??? ??? ?System.out.println("--------EE:"+"presenceChanged");
?? ??? ??? ??? ??? ??? ?}? ?
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ?});


?

?

?

SmackTest例子下載

?
https://blog.csdn.net/shimiso/article/details/8816540

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的基于xmpp openfire smack开发之smack类库介绍和使用[2]的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日韩第一页在线观看 | 日韩电影在线一区二区 | 亚洲国产视频一区二区 | 久久亚洲一区二区三区四区五区 | 天堂av在线中文 | 91视频免费播放 | 美女久久久久久久久久 | 美女扒开下面让男人捅 | 国产精品九 | 超碰在线一区 | 日本一区二区高清免费 | 无码少妇一级AV片在线观看 | 欧美日本高清视频 | av老司机久久 | 亚洲av永久无码精品三区在线 | 福利国产片 | 久草网视频在线观看 | 国产欧美日韩精品一区 | 日本中文字幕不卡 | 日韩一区二区三区在线看 | 2019中文字幕在线观看 | 人体av | 吞精囗交69激情欧美 | 毛片av免费 | 天天躁日日躁aaaa视频 | 青青青在线视频观看 | 亚洲av成人精品日韩在线播放 | 成人av一区二区在线观看 | 人av在线| 国产h在线观看 | 中文字幕高清在线免费播放 | 无码免费一区二区三区 | 丰满圆润老女人hd | 中文字幕在线亚洲 | 婷婷99 | 青青草视频在线免费观看 | 91看片黄 | 亚洲黄在线| 欧美特黄| 裸体一区二区 | 99日韩精品| 久久wwww | 不卡中文字幕在线观看 | 日韩男女视频 | 91中文字幕永久在线 | 中文字幕激情视频 | 中国在线观看免费高清视频播放 | 久久黄色免费网站 | 波多野结衣精品在线 | 91av毛片| 国产精品无码AV | 爱的色放3| youjizz国产| 亚洲永久无码精品 | 美女av免费在线观看 | 九九热精品视频在线播放 | 欧美岛国国产 | 熟妇无码乱子成人精品 | 国产9区| 国产视频一二区 | 国产日韩欧美另类 | 欧美无砖砖区免费 | 国产v在线 | 国产手机在线播放 | 亚洲AV无码久久精品浪潮 | 杨幂国产精品一区二区 | av在线伊人 | 亚洲激情一区 | 婷婷五综合 | 天堂av在线网 | 99热久久这里只有精品 | 男人的天堂99 | 抱着老师的嫩臀猛然挺进视频 | 中文字幕三级视频 | 久久免费在线 | 亚洲视频一二三四 | 69国产 | 91在线精品一区二区三区 | 亚洲一级片免费 | 日本www视频在线观看 | 国产精品高潮呻吟久久av黑人 | 五十路毛片| 伊人影院综合在线 | 99ri在线| 免费成人小视频 | 少妇无码一区二区三区 | 免费观看黄网站 | 国产福利91精品 | 亚洲国产精品成人va在线观看 | 日韩一区二区不卡 | 天天综合在线视频 | 亚洲AV不卡无码一区二区三区 | 小萝莉末成年一区二区 | 白石茉莉奈中文字幕在 | 午夜一级免费 | 国产一二视频 | 亚洲精品乱码久久久久久蜜桃欧美 | 国产重口老太伦 | 朋友人妻少妇精品系列 |