項目中有這麼一個需求: html
1. 間隔一分鐘,查詢XMPP服務器,獲取在某個聊天室中的成員信息 git
2. 獲取查詢結果,作相應處理 github
查詢XMPP協議,發現XEP-0045文檔有作相關的協議說明:http://xmpp.org/extensions/xep-0045.html#disco-roomitems ,用到的是多用戶聊天室(MUC)的discovery操做,對應的Namespace Uri爲: c#
http://jabber.org/protocol/disco#items
查詢消息定義: 服務器
<iq from='you jid' id='message id' to='roomJid' type='get'> <query xmlns='http://jabber.org/protocol/disco#items'/> </iq>正常的查詢結果消息格式定義:
<iq from='roomJid' id='message id' to='you jid' type='result'> <query xmlns='http://jabber.org/protocol/disco#items'> <item jid='roomJid/member1NickName'/> <item jid='roomJid/member2NickName'/> </query> </iq>若是該房間是私有房間,按照XEP-0030文檔協議,可能返回的消息是一個空查詢:
<iq from='roomJid' id='message id' to='you jid' type='result'> <query xmlns='http://jabber.org/protocol/disco#items' /> </iq>
通用的作法是拼接XML,而後發送,而後處理Iq到達消息。不一樣的庫封裝方法可能不同,可是底層發送出去時,必然都是同樣的XML文本。 dom
我用的XMPP庫是agsXMPP,具體的實現代碼以下: 學習
查詢: spa
Jid roomJid = new Jid("roomName","domain.com"); /// <summary> /// 查詢聊天室內成員 /// </summary> private void QueryMembers() { DiscoItemsIq discoItemIq = new DiscoItemsIq(IqType.get) discoItemIq.To = roomJid; xmppConnection.IqGrabber.SendIq(discoItemIq , new IqCB(QueryIqHandler), null); //更簡單的操做:DiscoManager discoM = new DiscoManager(xmppConnection); //discoM.DiscoverItems(roomJid, new IqCB(QueryIqHandler), null ); } /// <summary> /// 處理查詢結果 /// </summary> /// <param name="sender"></param> /// <param name="iq"></param> /// <param name="data"></param> private void QueryIqHandler(object sender ,IQ iq, object data) { if(iq.Type == IqType.result) { DiscoItems items = iq.SelectSingleElement<DiscoItems>(); foreach(DiscoItem item in items.GetDiscoItems()) { //roomJid = item.Jid.Bare; //memberNick = item.Jid.Resource; //other process... } } }
記錄XMPP學習過程.... code