基於JABBER的IM通訊

1.JABBER的協議模型

     XMPP
      |
     SASL
      |
     TSL
      |
     TCP

2.JABBER的通訊模型
  

                 SERVER S1  --------  SERVER S2
               /            /                    |
              /              /                   |
    CLINET A            CLIENT B       CLIENT C


3.JABBER 服務端
    現在使用最廣泛的就是Openfire: http://www.igniterealtime.org/
    用JAVA寫的,用WEB進行管理,方便而且簡單.
    當然,使用這個服務端,一定要知道它實現了哪些協議,軟件安裝好後,在Document目錄下面有
   有一個Protocol Support的Html文件.看看就知道了.

4.JABBER 客戶端
    JABBER客戶端軟件很多,比如現在比較流行的Pandion和Exodus.後者是開源的是用Delphi寫的;
  前者是FreeWare(免費軟件),但是不開源.
    還有一個是咱們中國人寫的,以前叫MYICQ,後來改名爲LinQ了,地址爲:http://cosoft.org.cn/projects/myicq ,看了P2P的實現那一小塊,感覺不錯。
    當然還有就是實現了XMPP協議的CODE LIBRARY.
    我現在用來測試的CODE LIBRARY爲gloox,用C++寫的,在WINDOWS 下面直接用VC6編譯生成DLL和LIB
 文件,然後在自己的工程中就可使用它的庫來開發程序了,當然也可以放在一起編譯,在調試的時候還能跟蹤進去.代碼不是很大,我就把GLOOX的代碼放上來了,見文章末.
  

5.參考   
   JABBER工程站點地址:http://www.jabber.org/
 
    在JABBER的中文站點,XMPP-CORE文檔在如下地址:
http://wiki.jabbercn.org/space/XMPP%E6%96%87%E6%A1%A3%E5%88%97%E8%A1%A8
/XMPP%E6%AD%A3%E5%BC%8FRFC%E6%A0%87%E5%87%86/RFC3920
如果要使用已經實現的CODE LIBRARY 等等,也應該知道這個協議.


6.客戶端如何直接傳輸語音和視頻
      有一文在網上看到了,是"兵工自動化"雜誌上的文章
      "2007年第1期摘錄:基於Jabber協議的P2P通訊系統",
      沒看到內容,介紹裏面稍微說了一下這方面的內容吧,但就目前JABBER服務器提供的功能來說,好象不提供UDP/TCP Hole Punching.如果是擴展服務器COMPONENT的話,應該可以.不知道作者是怎麼實現的.
     現在的服務器支持FILE TRANSFER,但是這是通過SERVER中轉的,傳輸文本文件還行,要傳輸一個幾百M的軟件過去,我看服務器夠嗆的.
     可以參考我放在本BLOG上的P2P技術的文章,相信這樣實現也是很簡單的.

7.一個通信實現的XML例子
   基於gloox的example下的register_example程序和roster_example程序.
   前一個是註冊的,後一個是通信的.
  下面S指的是消息從SERVER發出,C爲消息從CLIENT發出.

TCP連接建立:connecting to a245427228c4407 (192.168.0.7:5222)
1.會話協商
C: <?xml version='1.0' ?>
    <stream:stream to='a245427228c4407'
         xmlns='jabber:client'
         xmlns:stream='http://etherx.jabber.org/streams' 
         xml:lang='en' version='1.0'>
S: <stream:stream xmlns:stream='http://etherx.jabber.org/streams'
         xmlns='jabber:client' from='a245427228c4407' id='33450978'
         xml:lang='en' version='1.0'/>
2.服務端詢問CLIENT支持什麼安全認證
S: <stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>
         <mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
             <mechanism>DIGEST-MD5</mechanism>
             <mechanism>PLAIN</mechanism>
             <mechanism>ANONYMOUS</mechanism>
             <mechanism>CRAM-MD5</mechanism>
         </mechanisms>
         <compression xmlns='http://jabber.org/features/compress'>
             <method>zlib</method>
         </compression>
         <auth xmlns='http://jabber.org/features/iq-auth'/>
        <register xmlns='http://jabber.org/features/iq-register'/>
    </stream:features>
3.CLIENT給服務端請求要註冊用戶
C: <iq type='get' id='uid1'><query xmlns='jabber:iq:register'/></iq>
S: <iq type='result' id='uid1'>
        <query xmlns='jabber:iq:register'>
            <username/>
            <password/>
            <email/>
            <name/>
            <x xmlns='jabber:x:data' type='form'>
                <title>XMPP Client Registration</title>
                <instructions>Please provide the following information</instructions>
                <field var='FORM_TYPE' type='hidden'>
                   <value>jabber:iq:register</value>
                </field>
                <field label='Username' var='username' type='text-single'>
                   <required/>
                </field>
                <field label='Full name' var='name' type='text-single'/>
                <field label='Email' var='email' type='text-single'/>
                <field label='Password' var='password' type='text-private'>
                    <required/>
                </field>
             </x>
           </query>
        </iq>
4.CLIENT根據服務端的要求提交用戶信息
C: <iq id='uid2' type='set'>
        <query xmlns='jabber:iq:register'>
          <username>testuser</username>
          <password>testpwd</password>
          <name/>
          <email/>
         </query>
     </iq>
S: <iq type='result' id='uid2' to='a245427228c4407/33450978'/>
5.CLIENT關閉註冊的STREAM元素
C: </stream:stream>

roster_example例:
   
      ROSTER過程
1.TCP連接
connecting to a245427228c4407 (192.168.0.7:5222)
2.會話協商
C: <?xml version='1.0' ?><stream:stream to='a245427228c4407'
      xmlns='jabber:client'
      xmlns:stream='http://etherx.jabber.org/streams' 
      xml:lang='en' version='1.0'>
S: <stream:stream xmlns:stream='http://etherx.jabber.org/streams'
      xmlns='jabber:client' from='a245427228c4407' id='28cf95e9'
      xml:lang='en' version='1.0'/>
3.安全協商
S: <stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>
      <mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
        <mechanism>DIGEST-MD5</mechanism>
        <mechanism>PLAIN</mechanism>
        <mechanism>ANONYMOUS</mechanism>
        <mechanism>CRAM-MD5</mechanism>
      </mechanisms>
      <compression xmlns='http://jabber.org/features/compress'>
        <method>zlib</method>
      </compression>
      <auth xmlns='http://jabber.org/features/iq-auth'/>
      <register xmlns='http://jabber.org/features/iq-register'/>
    </stream:features>
C: <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'/>
4.服務端就CLIENT的安全協議,發送測試數據(BASE64)
S: <challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>c
mVhbG09ImEyNDU0MjcyMjhjNDQwNyIsbm9uY2U9IkV6UzZXV2t1YzJVMkVoeEd3dGs4QjdwSll1cHpMV
HNxcU9WN0ZjdkIiLHFvcD0iYXV0aCIsY2hhcnNldD11dGYtOCxhbGdvcml0aG09bWQ1LXNlc3M=</cha
llenge>
C: <response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>d
XNlcm5hbWU9InRlc3R1c2VyIixyZWFsbT0iYTI0NTQyNzIyOGM0NDA3Iixub25jZT0iRXpTNldXa3VjM
lUyRWh4R3d0azhCN3BKWXVwekxUc3FxT1Y3RmN2QiIsY25vbmNlPSIwMDAwMDAyOTAwMDA0ODIzMDAwM
DE4YmUwMDAwNjc4NCIsbmM9MDAwMDAwMDEscW9wPWF1dGgsZGlnZXN0LXVyaT0ieG1wcC9hMjQ1NDI3M
jI4YzQ0MDciLHJlc3BvbnNlPWZmMTY0NTM2NmM5ZWI0NmQ5ZDE0Y2Y0ODk4ODMzNzgzLGNoYXJzZXQ9d
XRmLTg=</response>
5.服務端安全測試成功
S: <success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cnN
wYXV0aD1lY2U3NDBhMzg0NDVhMzRlNDJiZjg2MjQ2YmU0OWQzNA==</success>

6.PRESENCE的SCENARIO開始,STREAM從新開啓
C: <?xml version='1.0' ?><stream:stream to='a245427228c4407'
     xmlns='jabber:client'
     xmlns:stream='http://etherx.jabber.org/streams' 
     xml:lang='en' version='1.0'>
S: <stream:stream xmlns:stream='http://etherx.jabber.org/streams'
      xmlns='jabber:client' from='a245427228c4407' id='28cf95e9'
      xml:lang='en' version='1.0'/>
7.PRESENCE的協商
S: <stream:features>
        <compression mlns='http://jabber.org/features/compress'>
           <method>zlib</method>
        </compression>
        <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>
        <session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>
    </stream:features>
C: <iq type='set' id='bind'>
    <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>
   </iq>
S: <iq type='result' id='bind' to='a245427228c4407/28cf95e9'>
     <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
       <jid>testuser@a245427228c4407/28cf95e9</jid>
     </bind>
    </iq>
C:  <iq type='set' id='session'><session xmlns='urn:ietf
:params:xml:ns:xmpp-session'/></iq>
S:  <iq type='result' id='session' to='testuser@a24542722
8c4407/28cf95e9'>
       <session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>
     </iq>
C: <iq id='uid1' type='get'><query xmlns='jabber:iq:pri
vate'><roster xmlns='roster:delimiter'/></query></iq>
C: <iq type='get' id='uid2'><query xmlns='jabber:iq:ros
ter'/></iq>
S: <iq type='result' id='uid1' to='testuser@a245427228c4
407/28cf95e9'><query xmlns='jabber:iq:private'><roster xmlns='roster:delimiter'/
></query></iq>
S: <iq type='result' id='uid2' to='testuser@a245427228c4
407/28cf95e9'><query xmlns='jabber:iq:roster'/></iq>
8.在線顯示
C: <presence><priority>0</priority></presence>
S: <presence type='subscribe' to='testuser@a245427228c44
07' from='user@a245427228c4407'/>

C: <iq type='set' id='uid3'><query xmlns='jabber:iq:ros
ter'><item jid='user@a245427228c4407'/></query></iq>
C: <presence type='subscribe' to='user@a245427228c4407'
 from='testuser@a245427228c4407/28cf95e9'/>
C: <presence type='subscribed' from='testuser@a24542722
8c4407' to='user@a245427228c4407'/>

S: <iq type='result' id='uid3' to='testuser@a245427228c4
407/28cf95e9'/>
S: <iq type='set' id='809-13' to='testuser@a245427228c44
07/28cf95e9'><query xmlns='jabber:iq:roster'><item jid='user@a245427228c4407' su
bscription='to'/></query></iq>

9.使服務端接受PRESENCE情況
C: <iq id='809-13' type='result'/>

10.從用戶user發送過來的消息,希望加爲好友
S: <presence type='subscribed' to='testuser@a245427228c4
407' from='user@a245427228c4407'/>
S: <presence from='user@a245427228c4407/Pandion' to='tes
tuser@a245427228c4407/28cf95e9'><x xmlns='jabber:x:avatar'><hash>fea759d5f9f52b7
95d35dae169dbfcd0b8e5585b</hash></x><priority>8</priority></presence>
S: <iq type='set' id='950-14' to='testuser@a245427228c44
07/28cf95e9'><query xmlns='jabber:iq:roster'><item jid='user@a245427228c4407' su
bscription='both'/></query></iq>

11.接受請求
C: <iq id='950-14' type='result'/>
S: <iq type='get' id='sd31' to='testuser@a245427228c4407
/28cf95e9' from='user@a245427228c4407/Pandion'><query xmlns='jabber:iq:version'/
></iq>
C: <iq id='sd31' from='testuser@a245427228c4407/28cf95e
9' to='user@a245427228c4407/Pandion' type='result'><query xmlns='jabber:iq:versi
on'><name>rosterTest</name><version>0.9.3</version><os/></query></iq>

12.接收從user發來的消息"hello"
S: <message type='chat' xml:lang='zh-cn' id='sd32' to='t
estuser@a245427228c4407/28cf95e9' from='user@a245427228c4407/Pandion'><body>hell
o</body><html xmlns='http://jabber.org/protocol/xhtml-im'><body xmlns='http://ww
w.w3.org/1999/xhtml'><span style='font-weight: normal; font-size: 9pt; color: #0
04200; font-style: normal; font-family: arial'>hello</span></body></html><x xmln
s='jisp:x:jep-0038'><name>shinyicons</name></x><active xmlns='http://jabber.org/
protocol/chatstates'/></message>
     消息: hello

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