我正在使用Ejabberd和Orbited,我遇到了一些JavaScript回调函数没有被调用的问题。下面是发生TCPSocket连接的JavaScript文件,我有两个回调函数,分别是
onSocketConnect:当轨道建立与XMPP服务器的5222端口的连接时调用
onLoginSuccess:在xmpp_client.login函数成功完成时调用
我面临的问题是连接成功,但我的回调仅在使用FireFox时调用,而不是在使用Safari或Chrome时调用。我完全不知道是什么原因导致了这个问题,但我确信xmpp_client.login方法确实会被调用,因为用户已经登录,并且在ejabberd管理控制台中显示为在线。
TCPSocket = Orbited.TCPSocket;
Orbited.settings.port = 8000;
Orbited.settings.hostname = 'localhost';
document.domain = document.domain;
<script src='http://localhost:8000/static/protocols/xmpp/xmpp.js'></script>
//xmpp.js file is included after this which is available with the Orbited. I have not included the code here.
<% if current_user %>
<script>
notifier = ' ';
user = "<%= current_user.jabber_id %>";
alert(user);
password = '123456';
domain = XMPPDOMAIN;
/* function onLoginSuccess(){
$('.status').html("Connected and Logged In");
xmpp_client.set_presence('available');
} */
var onLoginSuccess = function(){
$('.status').html("Connected and Logged In");
}
function onLoginFailure(){
alert('User could not be logged in');
}
function connectSuccess(){
$('.status').html("Connection Successful.");
}
function connectFailure(){
$('.status').html("Connection Failed!");
}
function onSetupNotification(){}
xmpp_client = new XMPPClient();
xmpp_client.connect('localhost',5222);
xmpp_client.onPresence = function(ntype, from) {
alert('Presence message' + ntype + ' From :' + from)
}
xmpp_client.onSocketConnect = function(domain, connectSuccess, connectFailure){
var domain = XMPPDOMAIN;
$('.status').html('Connected');
alert(user);
if(domain)
{
xmpp_client.connectServer(domain, connectSuccess, connectFailure);
xmpp_client.login(user, password, onLoginSuccess, onLoginFailure);
xmpp_client.set_presence('available');
}
}
function send_message(id, msg){
var j_id = id + '@' + 'siddharth-ravichandrans-macbook-pro.local';
alert('jid_id' + j_id);
var status = xmpp_client.msg(j_id, msg);
alert(status);
}
xmpp_client.onMessage = function(jid, username, text) {
alert('message-recieved');
if ( $('.discussion-area').length > 0 ){
$('.discussion-area').append('<div class=\'new-message\'>' + text + '</div>');
return false;
}
}
/* self.login = function(nick, pass, s, f) {
conn.onread = setUser;
success = s;
failure = f;
user = nick;
bare_jid = nick + "@" + domain;
full_jid = bare_jid + "/Orbited";
self.send(construct(LOGIN, [user, pass]));
}
self.set_presence = function(status, status_msg) {
self.send(EXT_PRESENCE[0] + full_jid + EXT_PRESENCE[1] + room_jid + EXT_PRESENCE[3] + status + EXT_PRESENCE[4] + status_msg + EXT_PRESENCE[5]);
}
*/
</script>
<% end %>这是我安装轨道时获得的xmpp.js的一部分,为了测试我的请求是否真的到达,我在登录方法中添加了一个警告,如下所示
...
...
self.login = function(nick, pass, s, f) {
conn.onread = setUser;
success = s;
failure = f;
user = nick;
bare_jid = nick + "@" + domain;
full_jid = bare_jid + "/Orbited";
self.send(construct(LOGIN, [user, pass]));
alert("bazingaa");
}
self.connectServer = function(d, s, f) {
success = s;
failure = f;
doma
...
...现在我不知道这是如何得到我的回调的,我真的希望在这方面能得到一些帮助。
谢谢
实际上,我发现移除xmpp_client.set_presence并将其移动到onLoginSuccess似乎已经完成了这项工作。在登录方法调用之后添加set_presence方法的那一刻,chrome中的功能就失败了。那么为什么这会阻止它在成功登录时调用回调呢?
var onLoginSuccess = function(){
$('.status').html("Connected and Logged In");
xmpp_client.set_presence('available');
}
xmpp_client.onSocketConnect = function(domain, connectSuccess, connectFailure){
var d = "";
$('.status').html('Connected');
xmpp_client.connectServer(d, connectSuccess, connectFailure);
xmpp_client.login(user, password, onLoginSuccess, onLoginFailure);
//xmpp_client.set_presence('available');
}帮助!
发布于 2010-08-05 04:30:40
我认为您的问题与Javascript的同源策略有关
https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript
我确信Safari/Chrome Web Inspector (启用了Javascript )会在javascript控制台中验证这一点
现代浏览器(即不是Internet Explorer!)通过实现跨域资源共享来解决此问题。但是,您的Javascript XMPP客户端库也必须实现CORS
http://metajack.im/2010/01/19/crossdomain-ajax-for-xmpp-http-binding-made-easy/
我并不熟悉Orbited,但我已经使用Strophejs库和ejabberd的XMPP BOSH连接管理器完成了这种类型的浏览器XMPP连接
要在所有浏览器中绕过同源策略,您需要使用代理
http://flxhr.flensed.com/ (客户端代理库)
如果使用apache,您可以在mod_proxy模块下使用带有ProxyPass指令的服务器端代理
https://stackoverflow.com/questions/3405237
复制相似问题