我能够在页面加载时创建 XMPP 连接。但是,每当我移动到另一个页面时,我都想使用相同的连接来删除客户端中的重复通知。我使用了以下代码。
$(document).bind('connect', function (ev, data) {
var jid = $.jStorage.get('JID', null);
var sid = $.jStorage.get('SID', null);
var rid = $.jStorage.get('RID', null);
if ((jid != null) && (sid != null) && (rid != null)) {
var conn = new Strophe.Connection("http://localhost:5280/xmpp-httpbind");
conn.attach(jid, sid, rid, function () {
alert('Connection attach success.');
Gab.connection = conn;
});
}
else {
var conn = new Strophe.Connection("http://localhost:5280/xmpp-httpbind");
conn.connect(data.jid, data.password, function (status) {
if (status === Strophe.Status.CONNECTED) {
$(document).trigger('connected');
} else if (status === Strophe.Status.DISCONNECTED) {
$(document).trigger('disconnected');
}
});
Gab.connection = conn;
}
});
并在卸载中:
$(window).unload(function () {
if (Gab.connection != null) {
Gab.connection.pause();
$.jStorage.set('JID', Gab.connection.jid);
$.jStorage.set('SID', Gab.connection.sid);
$.jStorage.set('RID', Gab.connection.rid);
} else {
$.jStorage.flush();
}
// Gab.connection = null;
alert('paused/disconnected');
})
它附加到连接,但是一旦附加,它就会在 Firebug 控制台中显示 (POST http://localhost:5280/xmpp-httpbind 404 Not Found 36ms)。有什么想法吗?
提前致谢。
最佳答案
你不应该相信卸载。而是在 xmpp 服务器的每个 cb 上存储/更新您的 RID。确保您的 RID 在每次调用时也增加。
关于connection - XMPP strophe 连接附加过程失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7400180/