freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

xmpp協(xié)議的使用(已修改)

2025-05-26 05:01 本頁面
 

【正文】 在android里面用的smack包其實(shí)叫做asmack,該包提供了兩種不同的連接方式:socket和client。該并且提供了很多操作xmpp協(xié)議的API,也方便各種不同自定義協(xié)議的擴(kuò)展。我們不需要自己重新去定義一套接收機(jī)制來擴(kuò)展新的協(xié)議,只需繼承然后在類里處理自己的協(xié)議就可以了。而本文今天主要說兩點(diǎn),一點(diǎn)就是消息是如何接收的,另一點(diǎn)就是消息是如何通知事件的。 總的思路 逐步擊破(聲明在看下面的文章時(shí),最好先理解一下smack的使用,這樣才能達(dá)到深入的理解)(謹(jǐn)記:上圖只顯示本文章解釋所要用到的類和方法,減縮了一些跟本文主題無關(guān)的代碼,只留一條貫穿著從建立連接到接收消息的線。)解析這塊東西打算從最初的調(diào)用開始作為入口,抽絲剝繭,逐步揭開。1.PacketListener packetListener = new PacketListener() { @Override public void processPacket(Packet packet) { .println(ActivityprocessPacket+ ())。 } }。 PacketFilter packetFilter = new PacketFilter() { @Override public booleanaccept(Packet packet) { (Activityaccept+ ())。 return true。 } }。 解釋:創(chuàng)建包的監(jiān)聽以及包的過濾,當(dāng)有消息到時(shí)就會(huì)廣播到所有注冊的監(jiān)聽,當(dāng)然前提是要通過packetFilter的過濾。2.connection = new XMPPConnection()。XMPPConnection在這構(gòu)造函數(shù)里面主要配置ip地址和端口(super(new ConnectionConfiguration(, 9991))。)3.(packetListener, packetFilter)。()。注冊監(jiān)聽,開始初始化連接。4.public void connect() { // Stablishes the connection, readers and writers connectUsingConfiguration(config)。}5.private void connectUsingConfiguration(ConnectionConfiguration config) { String host = ()。 intport = ()。 try{ = newSocket(host, port)。 } catch(UnknownHostException e) { ()。 } catch(IOException e) { ()。 } initConnection()。 }通過之前設(shè)置的ip和端口,建立socket對象6.protected void initDebugger() { Class? debuggerClass = null。 try{ debuggerClass = ()。 Constructor? constructor = ( , , )。 debugger= (SmackDebugger) (this, writer, reader)。 reader= ()。 } catch(ClassNotFoundException e1) { //TODOAutogenerated catch block ()。 } catch(Exception e) { throw newIllegalArgumentException( Can39。t initialize the configured debugger!, e)。 } }private void initReaderAndWriter() { try{ reader = newBufferedReader(newInputStreamReader(socket .getInputStream(), UTF8))。 } catch(UnsupportedEncodingException e) { //TODOAutogenerated catch block ()。 } catch(IOException e) { //TODOAutogenerated catch block ()。 } initDebugger()。}private void initConnection() { // Set the reader and writer instance variables initReaderAndWriter()。 packetReader = new PacketReader(this)。 addPacketListener((), null)。 // Start the packet reader. The startup() method will block until we // get an opening stream packet back from server. ()。}從三個(gè)方法可以看出,建立reader和writer的對象關(guān)聯(lián)到socket的InputStream,實(shí)例化ConsoleDebugger,該類主要是打印出接收到的消息,給reader設(shè)置了一個(gè)消息的監(jiān)聽。接著建立PacketReader對象,并啟動(dòng)。PacketReader主要負(fù)責(zé)消息的處理和通知7.public class PacketReader { Private ExecutorService listenerExecutor。 private boolean done。 Private XMPPConnection connection。 Private XmlPullParser parser。 Private Thread readerThread。 Protected PacketReader(finalXMPPConnection connection) { = connection。 ()。 } /** * Initializes the reader in order to be used. The reader is initialized * during the first connection and when reconnecting due to an abruptly * disconnection. */ protected void init() { done= false。 readerThread= newThread() { public voidrun() { parsePackets(this)。 } }。 (Smack Packet Reader )。 (true)。 // create an executor to deliver ining packets to listeners. // we will use a single thread with an unbounded queue. listenerExecutor= Executors .newSingleThreadExecutor(newThreadFactory() { @Override publicThread newThread(Runnable r) { Thread thread = newThread(r, smack listener processor)。 (true)。 returnthread。 } })。 resetParser()。 } /** * Starts the packet reader thread and returns once a connection to the * server has been established. A connection will be attempted for a maximum * of five seconds. An XMPPException will be thrown if the connection fails. * */ public voidstartup() { ()。 } /** * Shuts the packet reader down. */ public voidshutdown() { done= true。 // Shut down the listener executor. ()。 } private voidresetParser() { try{ parser= ().newPullParser()。 (, true)。 ()。 } catch(XmlPullParserException xppe) { ()。 } } /** * Parse toplevel packets in order to process them further. * *@paramthread * the thread that is being used by the reader to parse ining * packets. */ private void parsePackets(Thread thread) { try{ Int eventType = ()。 do{ if(eventType == ) { if(().equals(message)) { processPacket((parser))。 } (START_TAG)。 } else if(eventType == ) { (END_TAG)。 } eventType = ()。 } while(!doneamp。amp。 eventType != amp。amp。 thread == readerThread)。 } catch(Exception e) { ()。 if(!done) { } } }
點(diǎn)擊復(fù)制文檔內(nèi)容
規(guī)章制度相關(guān)推薦
文庫吧 www.dybbs8.com
公安備案圖鄂ICP備17016276號-1