XMPP是:是一種以XML為基礎(chǔ)的開(kāi)放式實(shí)時(shí)通信協(xié)議,是經(jīng)由互聯(lián)網(wǎng)工程工作小組(IETF)通過(guò)的互聯(lián)網(wǎng)標(biāo)準(zhǔn)。XMPP因?yàn)楸?a target="_blank" rel="nofollow">Google Talk應(yīng)用而被廣大網(wǎng)民所接觸。
優(yōu)點(diǎn):
1、開(kāi)放
XMPP協(xié)議是自由、開(kāi)放、公開(kāi)的,并且易于了解。而且在客戶端、服務(wù)器、組件、源碼庫(kù)等方面,都已經(jīng)各自有多種實(shí)現(xiàn)。
2、標(biāo)準(zhǔn)
<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;">互聯(lián)網(wǎng)工程工作小組(IETF)已經(jīng)將Jabber的核心XML流協(xié)議以XMPP之名,正式列為認(rèn)可的實(shí)時(shí)通信及Presence技術(shù)。而XMPP的技術(shù)規(guī)格已被定義在RFC 3920及RFC 3921。任何IM供應(yīng)商在遵循XMPP協(xié)議下,都可與Google Talk實(shí)現(xiàn)連接。</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、證實(shí)可用</dt>
<dd style="line-height: 28px;">第一個(gè)Jabber(現(xiàn)在XMPP)技術(shù)是Jeremie Miller在1998年開(kāi)發(fā)的,現(xiàn)在已經(jīng)相當(dāng)穩(wěn)定;數(shù)以百計(jì)的開(kāi)發(fā)者為XMPP技術(shù)而努力。今日的互聯(lián)網(wǎng)上有數(shù)以萬(wàn)計(jì)的XMPP服務(wù)器運(yùn)作著,并有數(shù)以百萬(wàn)計(jì)的人們使用XMPP實(shí)時(shí)傳訊軟件。</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網(wǎng)絡(luò)的架構(gòu)和電子郵件十分相像;XMPP核心協(xié)議通信方式是先創(chuàng)建一個(gè)stream,XMPP以TCP傳遞XML數(shù)據(jù)流,沒(méi)有中央主服務(wù)器。任何人都可以運(yùn)行自己的XMPP服務(wù)器,使個(gè)人及組織能夠掌控他們的實(shí)時(shí)傳訊體驗(yàn)。</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協(xié)議的服務(wù)器可以獨(dú)立于公眾XMPP網(wǎng)絡(luò)(例如在企業(yè)內(nèi)部網(wǎng)絡(luò)中),而使用SASL及TLS等技術(shù)的可靠安全性,已內(nèi)置于核心XMPP技術(shù)規(guī)格中。</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、可擴(kuò)展</dt>
<dd style="line-height: 28px;">XML命名空間的威力可使任何人在核心協(xié)議的基礎(chǔ)上建造定制化的功能;為了維持通透性,常見(jiàn)的擴(kuò)展由XMPP標(biāo)準(zhǔn)基金會(huì)。</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除了可用在實(shí)時(shí)通信的應(yīng)用程序,還能用在網(wǎng)絡(luò)管理、內(nèi)容供稿、協(xié)同工具、文件共享、游戲、遠(yuǎn)程系統(tǒng)監(jiān)控等。</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協(xié)議來(lái)建造及布署實(shí)時(shí)應(yīng)用程序及服務(wù)的公司及開(kāi)放源代碼計(jì)劃分布在各種領(lǐng)域;用XMPP技術(shù)開(kāi)發(fā)軟件,資源及支持的來(lái)源是多樣的,使得使你不會(huì)陷于被“綁架”的困境。</dd>
</dl>
缺點(diǎn):
<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、數(shù)據(jù)負(fù)載太重 </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);">隨著通常超過(guò)70%的XMPP協(xié)議的服務(wù)器的數(shù)據(jù)流量的存在和近60%的被重復(fù)轉(zhuǎn)發(fā),XMPP協(xié)議目前擁有一個(gè)大型架空中存在的數(shù)據(jù)提供給多個(gè)收件人。新的議定書(shū)正在研究,以減輕這一問(wèn)題。(個(gè)人觀點(diǎn):一直不是很火的原因就是這個(gè),希望早點(diǎn)改進(jìn))</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、沒(méi)有二進(jìn)制數(shù)據(jù) </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協(xié)議的方式被編碼為一個(gè)單一的長(zhǎng)的XML文件,因此無(wú)法提供修改二進(jìn)制數(shù)據(jù)。因此, 文件傳輸協(xié)議一樣使用外部的HTTP。如果不可避免,XMPP協(xié)議還提供了帶編碼的文件傳輸?shù)乃袛?shù)據(jù)使用的Base64。至于其他二進(jìn)制數(shù)據(jù)加密會(huì)話(encrypted conversations)或圖形圖標(biāo)(graphic icons)以嵌入式使用相同的方法。
有了上述的基本了解之后相信各位也有自己的主意,是否要看接下來(lái)的內(nèi)容:
即時(shí)通訊(Instant Messenger),就是現(xiàn)在的QQ、MSN、Gtalk等軟件都是即時(shí)通許軟件。啟動(dòng)gtalk就是其中基于xmpp協(xié)議實(shí)現(xiàn)的一款即時(shí)通訊軟件。但是開(kāi)發(fā)一款這種軟件還是任重而道遠(yuǎn)的。
實(shí)現(xiàn)XMPP 即時(shí)通許首要要了解openfire(服務(wù)端)、客戶端(下載潘迪安、spark、如意通等軟件,前期輔助學(xué)習(xí))、smack(它實(shí)現(xiàn)了xmpp的協(xié)議)。上述如何使用和配置我就不多講了,可以訪問(wèn)如下:
http://www.cnblogs.com/xiaodao/archive/2013/04/05/3000554.html 或 http://blog.csdn.net/shimiso/article/details/8816558(感謝內(nèi)容提供者,我也是跟著他們學(xué)習(xí),加以總結(jié),加深自己的印象,把自己遇到問(wèn)題的地方和大家分享一下)。
現(xiàn)在我相信同學(xué)都做好了相應(yīng)的配置了,直接進(jìn)入正題:
我用的mac本,潘迪安和如意通我是不用想了,我下載是spark。然后進(jìn)入openfire的管理端
[圖片上傳失敗...(image-d7a7c7-1551512249155)]
選擇用戶/組,然后選擇新建用戶(同學(xué)們的數(shù)據(jù)都做關(guān)聯(lián)了吧),出了管理員之外在建立兩個(gè)用戶:
[圖片上傳失敗...(image-df86d2-1551512249155)]
然后打開(kāi)spark,登陸界面如下:輸入你的用戶名和密碼(各位不要選擇自動(dòng)登陸啊,否則你要改服務(wù)器和用戶名的時(shí)候就有你頭疼的了):
[圖片上傳失敗...(image-c54bba-1551512249155)]
點(diǎn)擊spark的高級(jí),做如下配置:
[圖片上傳失敗...(image-5b0adf-1551512249155)]
確定之后你就可以登陸了。如果登陸不了可能有兩個(gè)原因:
第一:數(shù)據(jù)庫(kù)沒(méi)起
第二:openfire沒(méi)起(注:openfire在mac自動(dòng)啟動(dòng),建議每次想用的時(shí)候都重啟了,會(huì)避免很多問(wèn)題,如后管無(wú)法登陸,新建用戶不生效等問(wèn)題就出必殺:重啟)。
接下來(lái)用Eclipse建立一個(gè)java的工程,細(xì)節(jié)不多說(shuō)了,代碼如下:
ConnectionConfiguration connectionConfig = new ConnectionConfiguration("127.0.0.1", Integer.parseInt("5222"),"127.0.0.1");
// 允許自動(dòng)連接
connectionConfig.setReconnectionAllowed(true);
connectionConfig.setSendPresence(true);
Connection connection = new XMPPConnection(connectionConfig);
try {
connection.connect();// 開(kāi)啟連接
} catch (XMPPException e) {
throw new IllegalStateException(e);
}
connection.login("bjsujb1", "123456"); //第二個(gè)用戶和密碼
System.out.println(connection.getUser());
connection.getChatManager().createChat("bjsujb@127.0.0.1",null).sendMessage("Hello word!");
ConnectionConfiguration這個(gè)類初始化的時(shí)候三個(gè)參數(shù)分辨為第一個(gè):服務(wù)器地址 第二個(gè):對(duì)應(yīng)端口號(hào) 第三個(gè):代理服務(wù)器地址(可不寫(xiě),展示也可隨便寫(xiě))。
現(xiàn)在代碼你會(huì)發(fā)現(xiàn)spark收到了一條bjsujb1所發(fā)來(lái)的消息,內(nèi)容是Hello word