XMPP是:是一种以XML为基础的开放式实时通信协议,是经由互联网工程工作小组(IETF)通过的互联网标准。XMPP因为被Google Talk应用而被广大网民所接触。
优点:
1、开放
XMPP协议是自由、开放、公开的,并且易于了解。而且在客户端、服务器、组件、源码库等方面,都已经各自有多种实现。
2、标准
<dl style="line-height: 28px; color: rgb(0, 0, 0); font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">
<dd style="line-height: 28px;">互联网工程工作小组(IETF)已经将Jabber的核心XML流协议以XMPP之名,正式列为认可的实时通信及Presence技术。而XMPP的技术规格已被定义在RFC 3920及RFC 3921。任何IM供应商在遵循XMPP协议下,都可与Google Talk实现连接。</dd>
</dl>
<dl style="line-height: 28px; color: rgb(0, 0, 0); font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">
<dt style="line-height: 28px;">3、证实可用</dt>
<dd style="line-height: 28px;">第一个Jabber(现在XMPP)技术是Jeremie Miller在1998年开发的,现在已经相当稳定;数以百计的开发者为XMPP技术而努力。今日的互联网上有数以万计的XMPP服务器运作著,并有数以百万计的人们使用XMPP实时传讯软件。</dd>
</dl>
<dl style="line-height: 28px; color: rgb(0, 0, 0); font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">
<dt style="line-height: 28px;">4、分散式</dt>
<dd style="line-height: 28px;">XMPP网络的架构和电子邮件十分相像;XMPP核心协议通信方式是先创建一个stream,XMPP以TCP传递XML数据流,没有中央主服务器。任何人都可以运行自己的XMPP服务器,使个人及组织能够掌控他们的实时传讯体验。</dd>
</dl>
<dl style="line-height: 28px; color: rgb(0, 0, 0); font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">
<dt style="line-height: 28px;">5、安全</dt>
<dd style="line-height: 28px;">任何XMPP协议的服务器可以独立于公众XMPP网络(例如在企业内部网络中),而使用SASL及TLS等技术的可靠安全性,已内置于核心XMPP技术规格中。</dd>
</dl>
<dl style="line-height: 28px; color: rgb(0, 0, 0); font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">
<dt style="line-height: 28px;">6、可扩展</dt>
<dd style="line-height: 28px;">XML命名空间的威力可使任何人在核心协议的基础上建造定制化的功能;为了维持通透性,常见的扩展由XMPP标准基金会。</dd>
</dl>
<dl style="line-height: 28px; color: rgb(0, 0, 0); font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">
<dt style="line-height: 28px;">7、弹性佳</dt>
<dd style="line-height: 28px;">XMPP除了可用在实时通信的应用程序,还能用在网络管理、内容供稿、协同工具、文件共享、游戏、远程系统监控等。</dd>
</dl>
<dl style="line-height: 28px; color: rgb(0, 0, 0); font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">
<dt style="line-height: 28px;">8、多样性</dt>
<dd style="line-height: 28px;">用XMPP协议来建造及布署实时应用程序及服务的公司及开放源代码计划分布在各种领域;用XMPP技术开发软件,资源及支持的来源是多样的,使得使你不会陷于被“绑架”的困境。</dd>
</dl>
缺点:
<dt style="line-height: 28px; color: rgb(0, 0, 0); font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">1、数据负载太重 </dt>
<dd style="line-height: 28px; color: rgb(0, 0, 0); font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">随着通常超过70%的XMPP协议的服务器的数据流量的存在和近60%的被重复转发,XMPP协议目前拥有一个大型架空中存在的数据提供给多个收件人。新的议定书正在研究,以减轻这一问题。(个人观点:一直不是很火的原因就是这个,希望早点改进)</dd>
<dt style="line-height: 28px; color: rgb(0, 0, 0); font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">2、没有二进制数据 </dt>
<dd style="line-height: 28px; color: rgb(0, 0, 0); font-family: 'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">XMPP协议的方式被编码为一个单一的长的XML文件,因此无法提供修改二进制数据。因此, 文件传输协议一样使用外部的HTTP。如果不可避免,XMPP协议还提供了带编码的文件传输的所有数据使用的Base64。至于其他二进制数据加密会话(encrypted conversations)或图形图标(graphic icons)以嵌入式使用相同的方法。
有了上述的基本了解之后相信各位也有自己的主意,是否要看接下来的内容:
即时通讯(Instant Messenger),就是现在的QQ、MSN、Gtalk等软件都是即时通许软件。启动gtalk就是其中基于xmpp协议实现的一款即时通讯软件。但是开发一款这种软件还是任重而道远的。
实现XMPP 即时通许首要要了解openfire(服务端)、客户端(下载潘迪安、spark、如意通等软件,前期辅助学习)、smack(它实现了xmpp的协议)。上述如何使用和配置我就不多讲了,可以访问如下:
http://www.cnblogs.com/xiaodao/archive/2013/04/05/3000554.html 或 http://blog.csdn.net/shimiso/article/details/8816558(感谢内容提供者,我也是跟着他们学习,加以总结,加深自己的印象,把自己遇到问题的地方和大家分享一下)。
现在我相信同学都做好了相应的配置了,直接进入正题:
我用的mac本,潘迪安和如意通我是不用想了,我下载是spark。然后进入openfire的管理端
[图片上传失败...(image-d7a7c7-1551512249155)]
选择用户/组,然后选择新建用户(同学们的数据都做关联了吧),出了管理员之外在建立两个用户:
[图片上传失败...(image-df86d2-1551512249155)]
然后打开spark,登陆界面如下:输入你的用户名和密码(各位不要选择自动登陆啊,否则你要改服务器和用户名的时候就有你头疼的了):
[图片上传失败...(image-c54bba-1551512249155)]
点击spark的高级,做如下配置:
[图片上传失败...(image-5b0adf-1551512249155)]
确定之后你就可以登陆了。如果登陆不了可能有两个原因:
第一:数据库没起
第二:openfire没起(注:openfire在mac自动启动,建议每次想用的时候都重启了,会避免很多问题,如后管无法登陆,新建用户不生效等问题就出必杀:重启)。
接下来用Eclipse建立一个java的工程,细节不多说了,代码如下:
ConnectionConfiguration connectionConfig = new ConnectionConfiguration("127.0.0.1", Integer.parseInt("5222"),"127.0.0.1");
// 允许自动连接
connectionConfig.setReconnectionAllowed(true);
connectionConfig.setSendPresence(true);
Connection connection = new XMPPConnection(connectionConfig);
try {
connection.connect();// 开启连接
} catch (XMPPException e) {
throw new IllegalStateException(e);
}
connection.login("bjsujb1", "123456"); //第二个用户和密码
System.out.println(connection.getUser());
connection.getChatManager().createChat("bjsujb@127.0.0.1",null).sendMessage("Hello word!");
ConnectionConfiguration这个类初始化的时候三个参数分辨为第一个:服务器地址 第二个:对应端口号 第三个:代理服务器地址(可不写,展示也可随便写)。
现在代码你会发现spark收到了一条bjsujb1所发来的消息,内容是Hello word