Openfire和Strophejs网站 域名不同如何进行通信,这个问题总算解决,下面是解决步骤。
解决方案一:
Chrome浏览器默认支持跨域访问
IE浏览器需要做配置:点击IE浏览器的的“工具->Internet 选项->安全->自定义级别”将“其他”选项中的“通过域访问数据源”选中为“启用”或者“提示”,点击确定就可以了
解决方案二:
通过nginx做反向代理并配置跨域脚本,部署步骤如下面所述:
Openfire部署机器域名A HTTP协议 http://A:7070/http-bind/
Tomcat部署的网站域名B 网站 http://B:8080/*
1 Openfire服务器 添加nginx为方向代理
1.1 nginx.conf文件添加下面配置
location /http-bind/ {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://A:7070/http-bind/;
}
注:nginx 端口是80,配置后访问 http://A/http-bind/ 即等同访问 http://A:7070/http-bind/
1.2 nginx html文件夹中添加crossdomain.xml文件
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<site-control permitted-cross-domain-policies="all"/>
<allow-access-from domain="*" to-ports="5222,5223,7070,7443" secure="true"/>
</cross-domain-policy>
2 Web界面添加跨域JS
2.1添加js文件及相关插件
2.2html界面引用跨域js和strophejs
<script src='strophe/strophe.js'></script>
<script src='strophe/flXHR.js'></script>
<script src='strophe/strophe.flxhr.js'></script>
2.3具体应用
界面引用js
<script src='strophe/basic.js'></script>
basic.js具体运用内容如下:
var BOSH_SERVICE = 'http://A/http-bind/';
var domain= 'A';
var connection = null; $(document).ready(function () {
connection = new Strophe.Connection(BOSH_SERVICE);
connection.rawInput = rawInput;
connection.rawOutput = rawOutput; $('#connect').bind('click', function () {
var button = $('#connect').get(0);
if (button.value == 'connect') {
button.value = 'disconnect'; connection.connect($('#jid').get(0).value+"@"+domain,
$('#pass').get(0).value,
onConnect);
} else {
button.value = 'connect';
connection.disconnect();
}
});
});
具体下载:http://download.csdn.net/detail/caiping07/9388726