我正在使用Ejabberd和Orbited,我遇到一些问题,我的JavaScript回调函数没有被调用。以下是我的JavaScript文件,其中TCPSocket连接发生,我有两个回调函数,即
onSocketConnect
:当Orbited与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');
}
帮助!
答案 0 :(得分:1)
我认为您的问题与Javascript的同源政策有关
https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript
我确定Safari / Chrome网络检查器(启用了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指令的服务器端代理