最近恰好有机会碰到XMPP,把一些学习心得记录在这边。html
XMPP(Extensible Messageing and Presence Protocol)是一种IM的通信协定,
其前身为Jabber,后于IETF标准化为RFC3920。mysql
除了 通常通信协定常有的Server与Client外,XMPP还另外定义了Gateway,
只要经过Gateway,即可以与其余的IM Protocol通话。web
XMPP最大的特点在于传输的内容。其传输的内容为XML;藉由XML的扩充性,
能够达到许多的扩展应用。不过也因为传输内容为XML,所以没法提供二进制的资料。
档案传输需藉由外部HTTP。若是不可避免,XMPP协议提供了Base64的方式传输带编码文件。sql
XMPP每一个用户在网路上都有个独特的Jabber ID,简称为JID。
JID由id, domain与resource3个部份组成。其格式为:
id@domain/resource。
resource有时能够省略。api
传输的内容大体以下:服务器
|-------------------- |
| <stream> |
|---------------------- |
| <presence> |
| <show> |
| </show> |
|----------------------|
| <message to="'foo' "> |
| |
| </message> |
|----------------------|
| <iq to="'bar'"> |
| <query > |
| </query> |
|----------------------|
| ... |
|------------ ----------|
| </iq> |
|----------------------|
<stream> </stream>所夹住的部份称为XML Stanza,如果加上<stream> </stream>
自己,则称为XML Stream。session
presence有点相似于广播机制,能够针对有特定subscribe的对象传送讯息;
message里的body是传输的本文,而iq相似于http的request-responce服务。dom
底下是RFC里所提供的一个简单的对话session范例tcp
Client:
<stream:stream to="'example.com'" xmlns="'jabber:client'"
stream="'http://etherx.jabber.org/streams'"
version="'1.0'">
Server :
<stream:stream from="'example.com'" id="'someid'"
xmlns="'jabber:client'"
stream="'http://etherx.jabber.org/streams'"
version=" '1.0'">
... encryption, authentication, and resource binding ...
Client: <message from="'juliet@example.com'"
to="'romeo@example.net'" lang="'en' ">
Client: Art thou not Romeo, and a Montague?
Client: </message>
Server: <message from="'romeo@example.net'"
to="'juliet@example.com'" lang="'en '">
Server: Neither, fair saint, if either thee dislike.
Server: </message>
Client: </stream:stream>
Slient: </stream:stream>
一开始两方先传送svn
Client:
<stream:stream to="'example.com'" xmlns="'jabber:client'"
stream="'http://etherx.jabber.org/streams'"
version="'1.0'">
Server :
<stream:stream from="'example.com'" id="'someid'"
xmlns="'jabber:client'"
stream="'http://etherx.jabber.org/streams'"
version=" '1.0'">
确立了XMPP通信的开始,然后开始XML Stream的传输,
在XML Stream传输完了之后结束对话。
XMPP也支援DNS动态解析出Server IP。
标准的XMPP client解析的流程为(以example.com为例)
解析"_xmpp-client._tcp.example.com" ﹐得到链接的IP和port;
若是失败﹐则解析"_jabber._tcp.timyang.net" ﹐这个主要针对老的服务器设定;
若是仍是失败﹐则客户端认为domain没有配置SRV记录﹐则直接解析"example.com"并使用预设port 5222链接。
在了解了XMPP的传输内容后,接下来就是XMPP伺服器的架设。
咱们以ejabberd为范例,让大 家了解如何设定ejabberd server。首先安装ejabberd:
sudo apt-get install ejabberd
因为ejabberd使用erlang所撰写而成,所以会相依许多erlang的模组;
尔后若是须要让ejabberd使用MySQL的资料库,还要上网去抓erlang的相关API。
http://darkrevival.com/blog/2009/05/22/setup-an-xmpp-server/
/etc/ejabberd/ejabberd.pem是ejabberd server的凭证。
若是您有本身的凭证,能够取代之。
ejabberd的相关设定档主要在/etc/ejabberd/ejabberd.cfg
注解为'%'
其中最重要的有几项:
设定Admin user:
{acl, admin, {user, "", ""}}.
例如:
{acl, admin, {user, "billy", "localhost"}}.
若是须要多个admin user,能够添加多列。
设定Hostname:
这边设定的Hostname就表明这个ejabberd本身的名称为什么。
若是设定为example.com,那么billy@example.com
就是在这台Server上面认证的。
{hosts, [""]}.
例如:
{hosts, ["localhost"]}.
若是有新用户注册要提醒谁:
{registration_watchers, ["@"]}.
例如:
{registration_watchers, ["billy@localhost"]}.
ejabberd预设是使用本身的资料库。
如果想要改用MySQL做为ejabberd的资料库,
那么要从mysql,config以及erlang的mysql api三方面下手。
首先加入erlang的mysql api到ejabberd的module目录底下:
svn co https://svn.process-one.net/ejabberd-modules/mysql/trunk mysql
cd mysql
./build.sh
sudo cp ebin/*.beam /usr/lib/ejabberd/ebin
再来创建ejabberd专用的database:
wget http://svn.process-one.net/ejabberd/trunk/src/odbc/mysql.sql
mysql -u root -p
在mysql中创建ejabberd专用的账户
GRANT ALL ON ejabberd.* TO 'ejabberd'@'localhost' IDENTIFIED by 'password';
创建ejabberd的资料库
CREATE DATABASE ejabberd;
汇入mysql的资料库
mysql -D ejabberd -p -u ejabberd <>
等到ejabberd设定好上线后,就能够用ejabberdctl来注册使用者。
sudo ejabberdctl register billy localhost P@ssw0rd
以后,就能够连线到
http://localhost:5280/admin,
若是ejabberd顺利执行的话,这边能够用admin的id@domain与password登入。
登入后能够看到各个设定画面。在这边也能够直接注册使用者。
使用pidgin连线伺服器
pidgin > 新增账户
通信协定选XMPP,
使用者填上id,域名填上本身ejabberd server的hostname(或是domain)
密码则填上注册的密码,成功的话就能够登入server了。
Python的XMPP模组有很多,而其中最多人推荐的是PyXMPP
PyXMPP的网站上就有很多范例。
http://pyxmpp.jajcus.net/svn/pyxmpp/trunk/examples/
其中echo_bot.py与send_message.py是很好用的范例。
pyxmpp.all.JID能够将JID字串组合成物件,
pyxmpp.interfaces.stanza能够解析许多传输的内容。
有兴趣的朋友能够仔细看看。
如下是使用echo_bot.py的结果。
Refrence:
http://hi.baidu.com/jabber
http://darkrevival.com/blog/2009/05/22/setup-an-xmpp-server/
http://zh.wikipedia.org/zh-tw /XMPP
http://www.sunbo.name/20080409/xmpp
http://xmpp.org/rfcs/rfc3920.html