国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Asmack之旅(一)初識asmack源碼

重要鏈接網址

Asmack github地址:

https://github.com/Flowdalic/asmack

源碼jar下載地址

http://asmack.freakempire.de/

XMPP

http://xmpp.org/

Openfire smack地址

http://www.igniterealtime.org/

smack文檔

http://www.igniterealtime.org/builds/smack/docs/latest/documentation/index.html

簡單代碼操作參考

http://www.cnblogs.com/not-code/archive/2011/07/16/2108369.html


(——以下內容為smack文檔的個人部分翻譯以及見解,考慮到本人英語并不是很好,寫這篇文章僅僅作為記錄,如果有翻譯錯誤的請指正,共同進步,謝謝)


Getting Started

Configuration

Smack的初始化涉及到2個步驟

1 初始化系統(tǒng)屬性——通過SmackConfiguration進行系統(tǒng)屬性初始化。這些屬性可以通過getxxx()方法獲取

2 初始化啟動類——初始化類意味著在啟動時候實例化該類,如果繼承SmackInitializer則需要調用initialize()方法。如果不繼承SmackInitializer則初始化的操作必須在靜態(tài)代碼塊中,一旦加載類時自動執(zhí)行

Establishing a Connection創(chuàng)建連接

XmppTCPConnection類是被用來創(chuàng)建連接到xmpp服務器的

  1. // Create a connection to the jabber.org server._  
  2. XMPPConnection conn1 = new XMPPTCPConnection("jabber.org");  
  3. conn1.connect();  
  4.   
  5. // Create a connection to the jabber.org server on a specific port._  
  6. ConnectionConfiguration config = new ConnectionConfiguration("jabber.org", 5222);  
  7. XMPPConnection conn2 = new XMPPTCPConnection(config);  
  8. conn2.connect();  

ConectionConfiguration類提供一些控制操作,譬如是否加密。


Working with the Roster 花名冊

Roster可以讓你保持獲取其他用戶的presence狀態(tài)

用戶可以添加進組“Friend”或者“Co-workers”,你可以知曉用戶是否在線

檢索roster可以通過XMPPConnection.getRoster()方法,roster類允許你查看所有的roster enteries ,群組信息當前的登錄狀態(tài)


Reading and WritingPackets 讀寫數據包

XMPP服務器和客戶端間以XML傳遞的信息被稱為數據包。

org.jivesoftware.smack.packet包內有三種封裝好的基本的packet,分別是message,

presence和IQ。

比如Chat和GroupChat類提供了更高級別的結構用以創(chuàng)建發(fā)送packet,當然你也可以直接用packet。

  1. // Create a new presence. Pass in false to indicate we're unavailable._  
  2. Presence presence = new Presence(Presence.Type.unavailable);  
  3. presence.setStatus("Gone fishing");  
  4. // Send the packet (assume we have a XMPPConnection instance called "con").  
  5. con.sendPacket(presence);  

Smack提供兩種讀取packets

PacketListener PacketCollector
這兩種都通過PacketFilter進行packet加工
A packet listener is used for event style programming, while a packet collector has a result queue of packets that you can do polling and blocking operations on. 
(packet listener事件監(jiān)聽,而packet collector是一個packets的可以進行polling和blocking操作的結果集隊列。)
So, a packet listener is useful when you want to take some action whenever a packet happens to come in, while a packet collector is useful when you want to wait for a specific packet to arrive. 
(packet listener一旦數據包傳遞抵達的時候你可以進行處理,packet collector則被使用在你需要等待一個指定的packet傳遞抵達時候。)
Packet collectors and listeners can be created using an Connection instance.
(packet listener和packet collector在connection實例中被創(chuàng)建。)

  1. // Create a packet filter to listen for new messages from a particular  
  2. // user. We use an AndFilter to combine two other filters._  
  3. PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class),  
  4.         new FromContainsFilter("mary@jivesoftware.com"));  
  5. // Assume we've created a XMPPConnection name "connection".  
  6.   
  7. // First, register a packet collector using the filter we created.  
  8. PacketCollector myCollector = connection.createPacketCollector(filter);  
  9. // Normally, you'd do something with the collector, like wait for new packets.  
  10.   
  11. // Next, create a packet listener. We use an anonymous inner class for brevity.  
  12. PacketListener myListener = new PacketListener() {  
  13.         public void processPacket(Packet packet) {  
  14.             // Do something with the incoming packet here._  
  15.         }  
  16.     };  
  17. // Register the listener._  
  18. connection.addPacketListener(myListener, filter);  


Managing Connection

Connect and disConnect

  1. // Create the configuration for this new connection_  
  2. ConnectionConfiguration config = new ConnectionConfiguration("jabber.org", 5222);  
  3.   
  4. AbstractXMPPConnection connection = new XMPPTCPConnection(config);  
  5. // Connect to the server_  
  6. connection.connect();  
  7. // Log into the server_  
  8. connection.login("username", "password", "SomeResource");  
  9.   
  10. ...  
  11.   
  12. // Disconnect from the server_  
  13. connection.disconnect();  


Messaging using Chat

Chat

org.jivesoftware.smack.Chat

  1. // Assume we've created a XMPPConnection name "connection"._  
  2. ChatManager chatmanager = connection.getChatManager();  
  3. Chat newChat = chatmanager.createChat("jsmith@jivesoftware.com", new MessageListener() {  
  4.     public void processMessage(Chat chat, Message message) {  
  5.         System.out.println("Received message: " + message);  
  6.     }  
  7. });  
  8.   
  9. try {  
  10.     newChat.sendMessage("Howdy!");  
  11. }  
  12. catch (XMPPException e) {  
  13.     System.out.println("Error Delivering block");  
  14. }  
  15.   
  16. Message newMessage = new Message();  
  17. newMessage.setBody("Howdy!");  
  18. message.setProperty("favoriteColor", "red");  
  19. newChat.sendMessage(newMessage);  
  20.   
  21. // Assume a MessageListener we've setup with a chat._  
  22. public void processMessage(Chat chat, Message message) {  
  23.         // Send back the same text the other user sent us._  
  24.         chat.sendMessage(message.getBody());  
  25. }<span style="color:#ff0000;">  
  26. </span>  

incoming Chat

  1. _// Assume we've created a XMPPConnection name "connection"._  
  2. ChatManager chatmanager = connection.getChatManager().addChatListener(  
  3.     new ChatManagerListener() {  
  4.         @Override  
  5.         public void chatCreated(Chat chat, boolean createdLocally)  
  6.         {  
  7.             if (!createdLocally)  
  8.                 chat.addMessageListener(new MyNewMessageListener());;  
  9.         }  
  10.     });  


Roster and Presence

roster entries

包含xmpp地址,備注名,群組(假如該用戶不屬于任何一組,則調用“unfiled entry”)

  1. Roster roster = connection.getRoster();  
  2. Collection<RosterEntry> entries = roster.getEntries();  
  3. for (RosterEntry entry : entries) {  
  4.     System.out.println(entry);  
  5. }  

監(jiān)聽roster和presence更改

  1. Roster roster = con.getRoster();  
  2. roster.addRosterListener(new RosterListener() {  
  3.     // Ignored events public void entriesAdded(Collection<String> addresses) {}  
  4.     public void entriesDeleted(Collection<String> addresses) {}  
  5.     public void entriesUpdated(Collection<String> addresses) {}  
  6.     public void presenceChanged(Presence presence) {  
  7.         System.out.println("Presence changed: " + presence.getFrom() + " " + presence);  
  8.     }  
  9. });  

Provider architecture

smack provider是用于解析packet extension 和 IQ xml流的
有兩種類型的provider
IQProvider - parses IQ request into java objects
Extension Provider - parses XML sub-documents attached to packets into PacketExtension instances. By default, Smack only knows how to process a few standard packets and sub-packets that are in a few namespaces such as:
(解析packet的xml子元素到PacketExtension實例中。Smack默認僅知道處理少數的標準packets和少數的指定的namespaces下的子packets)
jabber:iq:auth
jabber:iq:roster
jabber:iq:register

(provider這塊翻譯目前就暫且如此,接著會寫IQ擴展將會提到這個,具體看IQ擴展)




本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存
猜你喜歡
類似文章
could not connect to XMPP server via smack:no response from server
基于xmpp openfire smack開發(fā)之smack類庫介紹和使用[2]
java實現簡單XMPP發(fā)送消息和文件的簡單例子
基于XMPP協(xié)議的Android IM研究
Android客戶端基于XMPP的IM(openfire+asmack)的聊天工具之環(huán)境搭建及與服務器建立連接(一)
XMPP Jabber practice 即時通訊開發(fā)實踐_博客_Xmpp_Creative
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服