XMPP

XMPP是一种基于XML的协议,它继承了在XML环境中灵活的发展性。因此,基于XMPP的应用具有超强的可扩展性。经过扩展以后的XMPP可以通过发送扩展的信息来处理用户的需求,以及在XMPP的顶端建立如内容发布系统和基于地址的服务等应用程 序。而且,XMPP包含了针对服务器端的软件协议,使之能与另一个进行通话,这使得开发者更容易建立客户应用程序或给一个配好系统添加功能。

定义

XMPP : The Extensible Messaging and Presence Protocol
中文全称:可扩展通讯和表示协议
简介:可扩展通讯和表示协议 (XMPP) 可用于服务类实时通讯、表示和需求响应服务中的XML数据元流式传输。XMPP以Jabber协议为基础,而Jabber是即时通讯中常用的开放式协议。XMPP is the IETF's formalization of the base XML streaming protocols for instant messaging and presence developed within the Jabber open-source community in 1999
XMPP(可扩展消息处理现场协议)是基于可扩展标记语言(XML)的协议,它用于即时消息(IM)以及在线现场探测。它在促进服务器之间的准即时操作。这个协议可能最终允许因特网用户向因特网上的其他任何人发送即时消息,即使其操作系统和浏览器不同。
XMPP的前身是Jabber,一个开源形式组织产生的网络即时通信协议。XMPP目前被IETF国际标准组织完成了标准化工作。标准化的核心结果分为两部分;
核心的XML流传输协议
基于XMLFreeEIM流传输的即时通讯扩展应用
XMPP的核心XML流传输协议的定义使得XMPP能够在一个比以往网络通信协议更规范的平台上。借助于XML易于解析和阅读的特性,使得XMPP的协议能够非常漂亮。
XMPP的即时通讯扩展应用部分是根据IETF在这之前对即时通讯的一个抽象定义的,与其他业已得到广泛使用的即时通讯协议,诸如AIM,QQ等有功能完整,完善等先进性。
XMPP的扩展协议Jingle使得其支持语音和视频。
XMPP的官方文档是RFC 3920.
基本网络结构

XMPP中定义了三个角色,客户端,服务器,网关。通信能够在这三者的任意两个之间双向发生。服务器同时承担了客户端信息记录,连接管理和信息的路由功能。网关承担着与异构即时通信系统的互联互通,异构系统可以包括SMS(短信),MSN,ICQ等。基本的网络形式是单客户端通过TCP/IP连接到单服务器,然后在之上传输XML。

功能

传输的是与即时通讯相关的指令。在以前这些命令要么用2进制的形式发送(比如QQ),要么用纯文本指令加空格加参数加换行符的方式发送(比如MSN)。而XMPP传输的即时通讯指令的逻辑与以往相仿,只是协议的形式变成了XML格式的纯文本。这不但使得解析容易了,人也容易阅读了,方便了开发和查错。而XMPP的核心部分就是一个在网络上分片段发送XML的流协议。这个流协议是XMPP的即时通讯指令的传递基础,也是一个非常重要的可以被进一步利用的网络基础协议。所以可以说,XMPP用TCP传的是XML流。

具体应用

举个例子看看所谓的XML流是什么样子的?
客户端:<?xml version='1.0'?>
<stream:stream
to='example_com'
xmlns='jabber:client'
xmlns:stream='http_etherx_jabber_org/streams'
version='1.0'>
服务器:<?xml version='1.0'?>
<stream:stream
from='example_com'
id='someid'
xmlns='jabber:client'
xmlns:stream='http_etherx_jabber_org/streams'
version='1.0'>
...其他通信...
客户端:<message from='juliet_example_com'
to='romeo_example_net'
xml:lang='en'>
客户端: <body>Art thou not Romeo, and a Montague?</body>
客户端:</message>
服务器:<message from='romeo_example_net'
to='juliet_example_com'
xml:lang='en'>
服务器:<body>Neither, fair saint, if either thee dislike.</body>
服务器:</message>
客户端:</stream:stream>
服务器:</stream:stream>
以文档的观点来看,客户端或服务器发送的所有XML文本连缀在一起,从<stream>到</stream>构成了一个完整的XML文档。其中的 stream标签就是所谓的XML Stream。在<stream>与</stream>中间的那些<message>...< /message>这样的XML元素就是所谓的XML Stanza(XML节)。XMPP核心协议通信的基本模式就是先建立一个stream,然后协商一堆安全之类的东西,中间通信过程就是客户端发送XML Stanza,一个接一个的。服务器根据客户端发送的信息以及程序的逻辑,发送XML Stanza给客户端。但是这个过程并不是一问一答的,任何时候都有可能从一方发信给另外一方。通信的最后阶段是</stream>关闭流,关闭TCP/IP连接。
目前不少IM应用系统如:Google公司的Google Talk以及Jive Messenger等开源应用,都是遵循XMPP协议集而设计实现的,这些应用具有很好的互通性。

系统特点

(1)客户机/服务器通信模式;(2)分布式网络;(3)简单的客户端;(4)XML的数据格式。

通俗解释

其实XMPP 是一种很类似于http协议的一种数据传输协议,它的过程就如同“解包装--〉包装”的过程,用户只需要明白它接收的类型,并理解它返回的类型,就可以很好的利用xmpp来进行数据通讯。

XMPP Protocol Support (Jabber!)

We have support for XMPP (Jabber) as a transport in ActiveMQ.

To use just add a connector as follows

<broker xmlns="http://activemq.org/config/1.0">
<transportConnectors>
<transportConnector name="openwire" uri="tcp://localhost:61616" discoveryUri="multicast://default"/>
<transportConnector name="stomp" uri="stomp://localhost:61613"/>
<transportConnector name="xmpp" uri="xmpp://localhost:61222"/>
</transportConnectors>
</broker>

And you can now use XMPP to connect to the broker & send and receive messages.

 
Some versions of the broker (5.0-5.2) does not include WoodStox library by default, which could impact ActiveMQ XMPP funcionality. You should have a library named like wstx-asl-x.x.x.jar in the lib/optional directory of your broker. If that's not the case, download it from here and put it in the classpath
 

XMPP Support in Apache ActiveMQ

ActiveMQ provides a bidirectional bridge between ActiveMQ messages and XMPP.

  • if a client joins a chatroom / conference room, the name of the conference room is mapped to a JMS topic of the same name
  • typing into a chatroom sends a message to the topic
  • presence in a chatroom maintains a subscription on the JMS topic (using noLocal to avoid getting copies of things you say) so that messages sent by other clients (via XMPP, the Web Console, the Examples or any other Cross Language Clients) are then sent to the chatroom.

Using a Jabber Client

Basically, you should be able to connect from any Jabber-compatible client to the broker. The below example is using Spark 2.0.0 version. For more details on connecting with different clients take a look at Jabber clients compatibility.

To connect to Apache ActiveMQ try the following...

1. Run the Web Console
2. Start Spark or whatever Jabber Client you wish
3. Login to localhost:61222

 
Some Jabber clients (like iChat) insist on usernames in forms of username@host, so in that case just append @localhost to your username
 

 
Some Jabber clients like to auto-discover the host and port. You need to explicitly disable this feature so you can explicitly configure localhost as the host and 61222as the port.e.g. on Spark go to the Advanced tab and disable the Automatically discover host and port
 

4. You should now see the following screen...

5. Now click on the Join Conference Room button (next to the Add Contact button) and the following dialog should appear

6. Now press the Create or Join Room button to get the following dialog. Enter a JMS topic name, in this case foo.bar and you're good to go...

7. Now your chat window should appear for talking and listening to the topic foo.bar. So start typing to test things out.

8. Now if you go to the Topic Console in the Web Console you should see the topic has been created

9. If you now click on the Send To link next to the foo.bar topic you can send a message to the topic from the web console.

10. Press send and you should see the chat appear on the chat window

Jabber clients compatibility

Here you can find specfic issues and workarounds for various Jabber clients. If you have some of your own, please post them here.

Spark

Url: http://www.igniterealtime.org/projects/spark/index.jsp
Spark 2.0.0 works fine with ActiveMQ; Click here to download: Spark 2.0.0 for Windows.
Spark 2.5.x connects fine, but it won't open the Join Conference Room dialog.

iChat

Url: http://www.apple.com/macosx/features/ichat.html
Tested version 4.0.7 works fine, but it insists you use username@host format for usernames

Adium

Url: http://www.adiumx.com/
Tested version 1.3.3 works fine. The only spotted issue is reconnecting to Command Agent topic. We recommend that you restart the Adium if you need to do this

  1. da shang
    donate-alipay
               donate-weixin weixinpay

发表评论↓↓