Openfire Bosh 配合strophe.js使用

这次试了一下Openfire的Bosh协议以及strophe.js,记录一下。

 

 

有两个问题要注意:

1:直接使用javascript调用bosh往往存在同源原则的问题,例如localhost:80端口下的页面脚本要访问Bosh服务localhost:7070,那么这个ajax访问就会失败,因为违背了同源法则。(这使得我又回去看了一下cookie的问题https://blog.ureshika.com/web/2012/08/26/e5-86-8d-e8-b0-88p3p-e4-b8-8e-e7-ac-ac-e4-b8-89-e6-96-b9cookie/,cookie保存却又不区分端口)因此,我们要么使用跨域插件fkXHR要么在服务器上配置代理来使bosh服务与发起请求页面“同源”。

例如apache里面在 httpd.conf 中加入下面几行:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

 

ProxyRequests Off
ProxyPass /xmpp-http http://127.0.0.1:7070
ProxyPassReverse /xmpp-http http://127.0.0.1:7070
<proxy http://127.0.0.1:7070>
    AddDefaultCharset off
    AllowOverride None
    Order Deny,Allow    
    Allow from all
</proxy>

 

上面的配置将apache监听端口下/xmpp-http 反向代理到了http://127.0.0.1:7070

 

2: 这其实和上面的第一个问题相关,如果遇到400 Bad Request的问题,你要检查你的代理配置和你请求的url, 很可能是掉了最后的反斜杠/,这个是反向代理常犯的错误。以上面的配置为例,则bosh的url应该为localhost/xmpp-http/http-bind/

[为方便国外鸟:]

if you encounter the error response from the xmpp BOSH server, you should check your url with your proxy configuration, it is you miss the last backslash of the BOSH service’s url in your javascript in most case. for example, if you make the proxy like “ProxyPassReverse /xmpp-http http://127.0.0.1:7070” then your BOSH url should be localhost/xmpp-http/http-bind/, note the last backslash.

 

使用示例官方下载包example文件夹中有。

 

这个strophe有个奇怪的行为,其Strophe.Connection.connect方法当传入的不带域名时,会尝试匿名登录。而如果传入bareJid(像abc@localhost),如果密码错了或是用户名不存在,则一直没返回。

 

参见:

http://mineral.iteye.com/blog/448260

http://www.ibm.com/developerworks/cn/xml/tutorials/x-realtimeXMPPtut/section6.html


Total views.

© 2013 - 2024. All rights reserved.

Powered by Hydejack v6.6.1