Javascript回调在Firefox中工作,但在Chrome或Safari中不工作
我正在与Ejabberd和Orbited合作,我的JavaScript回调函数没有被调用,这让我有一些问题。下面是我的JavaScript文件,其中发生了TCPSocket连接,我有两个回调函数,即Javascript回调在Firefox中工作,但在Chrome或Safari中不工作,javascript,google-chrome,webkit,xmpp,orbited,Javascript,Google Chrome,Webkit,Xmpp,Orbited,我正在与Ejabberd和Orbited合作,我的JavaScript回调函数没有被调用,这让我有一些问题。下面是我的JavaScript文件,其中发生了TCPSocket连接,我有两个回调函数,即 onSocketConnect:当轨道与XMPP服务器的端口5222建立连接并 onloginsucess:在成功完成xmpp\u client.login功能时调用 我面临的问题是连接成功,但我的回调只有在使用FireFox时才会被调用,而不是在使用Safari或Chrome时。我完全不知道是什么
onSocketConnect
:当轨道与XMPP服务器的端口5222建立连接并
onloginsucess
:在成功完成xmpp\u 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_client.set_presence并将其移动到onloginsucess似乎已经做到了这一点。当我在登录方法调用后添加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');
}
救命啊 我认为您的问题与Javascript的同源策略有关 我相信Safari/ChromeWeb检查器(启用Javascript)会在Javascript控制台中验证这一点 现代浏览器(即不是Internet Explorer!)通过实现跨源资源共享来解决这个问题。但是,Javascript XMPP客户端库也必须实现CORS 我不熟悉Orbited,但我使用Strophejs库和ejabberd的XMPP BOSH连接管理器完成了这种类型的浏览器XMPP连接 要在所有浏览器中使用相同的源策略,您需要使用代理 (客户端代理库)
如果使用apache,您可以在mod_proxy模块下使用带有ProxyPass指令的服务器端代理“我确信会调用方法
xmpp_client.login
”–您确定会调用它吗?你在里面设置了断点吗?document.domain=document.domain有什么用代码>?document.domain=document.domain只有在我将其作为子域在生产中运行时才起作用。在这里,它没有多大作用。是的,我设置了警报消息,只在Firefox中调用。另外,在登录时,用户出现在我的ejabberd web管理控制台中,因此我知道它已被调用。因此,根据管理控制台,在Chrome中,用户正在登录,但是xmpp_客户端。登录不被调用?对吗?听起来像是xmpp.js中的一个问题。xmpp_client.login确实会被调用,成功登录或失败的回调(即onLoginsAccess和onLoginFailure)不会被调用,后续的xmpp_client.set_presence方法也不会被调用,我觉得这是问题的根本原因。您有一个示例页面,我们可以自己测试吗?听起来很棒。我不明白的是,我所有的请求都来自同一个主机、端口和协议,所以为什么您怀疑这可能是问题所在?您建议的客户端代理使用flash,这是我选择Orbited和Ejabberd时想要避免的事情之一。
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');
}