基于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

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值