国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
使用Strophe連接xmpp,輕松構(gòu)建web即時聊天工具

使用Strophe連接xmpp,輕松構(gòu)建web即時聊天工具。

實現(xiàn)原理
實現(xiàn)webim方法有很多,最關(guān)鍵的問題是保持和服務(wù)端的連接。如何保障會話的即時性和連通性。常用的有poll, long poll,  comet等;; 其中poll的方式效果最差,一般都會有1到2秒的延遲。long poll和comet技術(shù)比較新,因為http無狀態(tài)的原因,這種長連接方式要保持,一般都需要服務(wù)端額外代碼提供支持。像gtalk采用的就是long poll的方式。服務(wù)端常會使用異步IO等方式來支持高并發(fā)。
本文使用的是XEP標(biāo)準(zhǔn)擴(kuò)展規(guī)范,XEP-0124 提供的方法實現(xiàn)WebIM. 思路即使用一個javascript的xep-0124的實現(xiàn),直接連接xmpp server端。目前xep-0124的實現(xiàn),在大部分的xmpp server實現(xiàn)中都支持,本文使用的是OpenFire 3.6.4 作為Xmpp Server。
XEP 0124規(guī)范定義了一個比較完整和安全的協(xié)議,具體請參閱相當(dāng)文檔。本文使用javascript端的XEP-0124的實現(xiàn)為Strophe的js實現(xiàn)。
另外因為瀏覽器javascript跨域問題,需要使webim里調(diào)用javascript也是80端口,并同一子域,所以使用Apache或者Nginx 在80端口,并轉(zhuǎn)發(fā)/http-bind請求至Xmpp Server的http-binding端口。本機使用Nginx. xmpp server 使用使用7070端口。

一、安裝openfire:http://blog.csdn.net/e421083458/article/details/38037373

二、配置apache


    我們下載安裝的是httpd-2.2.17-win32-x86-no_ssl.msi,安裝完成后,我們需要配置一下,由于jwchat是用javacript去和openfire進(jìn)行通訊的,所以他們之間的通訊是基于http的,但是由于瀏覽器為了安全性是不允許javascript跨域訪問的。我們必須通過別的技術(shù)來繞過這限制,所以我們采取apache服務(wù)器的重定向功能去突破這個限制。

     安裝完成后進(jìn)入到apache的安裝目找到conf文件夾下的httpd.conf文件,用記事本打開,把下列幾個配置項放開(默認(rèn)被注釋掉了)

1、LoadModule rewrite_module modules/mod_rewrite.so
2、LoadModule proxy_module modules/mod_proxy.so
3、LoadModule proxy_http_module modules/mod_proxy_http.so

然后再在本配置文件的末尾加入如下幾行配置

  ServerName blzc.com
  <Directory /var/jwchat>
    Options  +Indexes +MultiViews
  </Directory>
  AddDefaultCharset UTF-8
  RewriteEngine on
  ProxyPass /jwchat/http-bind/ http://blzc.com:7070/http-bind/   

在此有必要對proxyPass的參數(shù)做些說明

“/jwchat/http-bind/”:jwchat就通過訪問http://域名/jwchat/http-bind/地址去和openfire通訊,apache接到請求后就會重新定向到http://blzc.com:7070/http-bind/

“http://blzc.com:7070/http-bind/”:被重新定向的地址,也就是我們的openfire的http訪問地址。7070端口是openfire的默認(rèn)訪問端口,當(dāng)然我們也可以進(jìn)入openfire進(jìn)行配置。

三、openfire的配置
安裝好openfire后進(jìn)入web式的管理界面,選擇服務(wù)器-》服務(wù)器管理器-》系統(tǒng)屬性
在里邊添加兩個屬性(記得選擇不加密)
xmpp.httpbind.client.requests.polling = 0
xmpp.httpbind.client.requests.wait = 10


四、創(chuàng)建聊天腳本

創(chuàng)建index.html頁面:

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <title>Latest content</title>  
  5.         <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>  
  6.         <script type="text/javascript"    
  7. src="strophejs/strophe.js"></script>  
  8.         <script type="text/javascript"   
  9. src="pingstream.js"></script>  
  10.     </head>  
  11.     <body>  
  12.       <div id='login' style='text-align: left'>  
  13.         <form name='cred'>  
  14.           <label for='jid'>JID:</label>  
  15.           <input type='text' id='jid' value="richard@csdn.shimiso.com" /><br/>  
  16.           <label for='pass'>Password:</label>  
  17.           <input type='password' id='pass' value="niuyufu123" /><br/>  
  18.           <input type='button' id='connect' value='connect' />  
  19.         </form>  
  20.       </div>  
  21.       <hr />  
  22.       <div style="text-align: left">  
  23.           <label for='tojid'>tojid</label>  
  24.           <input type='text' id='tojid' value="" /><br/>  
  25.           <label for='msg'>msg:</label>  
  26.           <textarea id="msg">Hello world!!!</textarea>  
  27.           <br/>  
  28.           <input type='button' id='replay' value='replay' />  
  29.       </div>  
  30.       <hr />  
  31.       <div id='log'></div>  
  32.     </body>  
  33. </html>  

創(chuàng)建pingstream.js

[javascript] view plain copy
print?
  1. var BOSH_SERVICE = '/http-bind/';  
  2. var connection = null;  
  3.   
  4. function log(msg)   
  5. {  
  6.     $('#log').append('<div></div>').append(document.createTextNode(msg));  
  7. }  
  8.   
  9. /** 
  10.  * 連接綁定方法 
  11.  * @param status 
  12.  */  
  13. function onConnect(status)  
  14. {  
  15.     if (status == Strophe.Status.CONNECTING) {  
  16.     log('Strophe is connecting.');  
  17.     } else if (status == Strophe.Status.CONNFAIL) {  
  18.     log('Strophe failed to connect.');  
  19.     $('#connect').get(0).value = 'connect';  
  20.     } else if (status == Strophe.Status.DISCONNECTING) {  
  21.     log('Strophe is disconnecting.');  
  22.     } else if (status == Strophe.Status.DISCONNECTED) {  
  23.     log('Strophe is disconnected.');  
  24.     $('#connect').get(0).value = 'connect';  
  25.     } else if (status == Strophe.Status.CONNECTED) {  
  26.     log('Strophe is connected.');  
  27.     log('ECHOBOT: Send a message to ' + connection.jid +   
  28.         ' to talk to me.');  
  29.       
  30.     connection.addHandler(onMessage, null, 'message', null, null,  null);   
  31.     connection.send($pres().tree());  
  32.     }  
  33. }  
  34.   
  35. /** 
  36.  * 獲取消息時的方法 
  37.  * @param msg 
  38.  * @returns {Boolean} 
  39.  */  
  40. function onMessage(msg) {  
  41.     var to = msg.getAttribute('to');  
  42.     var from = msg.getAttribute('from');  
  43.     var type = msg.getAttribute('type');  
  44.     var elems = msg.getElementsByTagName('body');  
  45.   
  46.     if (type == "chat" && elems.length > 0) {  
  47.     var body = elems[0];  
  48.   
  49.     log('ECHOBOT: I got a message from ' + from + ': ' +   
  50.         Strophe.getText(body));  
  51.       
  52. /*  關(guān)閉echo機器的自動回復(fù) 
  53.     var reply = $msg({to: from, from: to, type: 'chat'}) 
  54.             .cnode(Strophe.copyElement(body)); 
  55.     connection.send(reply.tree()); 
  56.  
  57.     log('ECHOBOT: I sent ' + from + ': ' + Strophe.getText(body));*/  
  58.       
  59.       
  60.     }  
  61.     return true;  
  62. }  
  63.   
  64. /** 
  65.  * 發(fā)信息 
  66.  * @param toId 
  67.  * @param fromId 
  68.  * @param msg 
  69.  */  
  70. function sendMsg(toId,fromId,msg) {  
  71.     var reply = $msg({to: toId, from:fromId , type: 'chat'}).cnode(Strophe.xmlElement('body', '' ,msg));  
  72.     connection.send(reply.tree());  
  73.     log('ECHOBOT: I sent ' + toId + ': ' + msg);  
  74. }  
  75.   
  76. /** 
  77.  * 事件監(jiān)聽 
  78.  */  
  79. $(document).ready(function () {  
  80.     connection = new Strophe.Connection(BOSH_SERVICE);  
  81.   
  82.     // Uncomment the following lines to spy on the wire traffic.  
  83.     connection.rawInput = function (data) { console.log('RECV: ' + data); };  
  84.     connection.rawOutput = function (data) { console.log('SEND: ' + data); };  
  85.   
  86.     // Uncomment the following line to see all the debug output.  
  87.     //Strophe.log = function (level, msg) { log('LOG: ' + msg); };  
  88.   
  89.     $('#connect').bind('click', function () {  
  90.     var button = $('#connect').get(0);  
  91.     if (button.value == 'connect') {  
  92.         button.value = 'disconnect';  
  93.         connection.connect($('#jid').get(0).value,  
  94.                    $('#pass').get(0).value,  
  95.                    onConnect);  
  96.     } else {  
  97.         button.value = 'connect';  
  98.         connection.disconnect();  
  99.     }  
  100.     });  
  101.       
  102.     $('#replay').bind('click', function () {  
  103.         toId = $('#tojid').val();  
  104.         fromId = $('#jid').val();  
  105.         msg=$('#msg').val();  
  106.         sendMsg(toId,fromId,msg);  
  107.     });  
  108. });  

用pandian與聊天頁并行測試。

成功后的截圖:

代碼下載地址:http://download.csdn.net/detail/e421083458/7672297


本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Android基于XMPP Smack Openfire開發(fā)IM(2)登錄openfire
XMPP協(xié)議學(xué)習(xí)筆記五(Openfire消息處理流程)
基于XMPP協(xié)議(openfire服務(wù)器)的消息推送實現(xiàn)
基于XMPP協(xié)議的Android IM研究
XMPP 搭建Openfire 服務(wù)器
消息推送ABC
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服