我已经使用 strope.js 和 jQuery 编写了一个基于 Web 的 MUC 客户端,并且我正在尝试发送一个 无法进入房间并在 jquery 中断开用户连接 窗口的卸载事件。如果用户离开或关闭 浏览器选项卡中,他们应该从 MUC 中注销。
我已经通过注销测试了在此事件中运行的代码 按钮我在页面上,所以我很确定该节是正确的。 我认为如果浏览器在发送节时出现问题 窗口正在关闭。这里有什么解决方法吗?我也尝试过 onbeforeunload 事件(我知道它不完全是跨浏览器的 兼容),但这似乎也不起作用。
非常感谢任何建议!
谢谢, 约翰
最佳答案
确保切换到同步模式,并在发送 disconnect()
调用之前调用 flush()
。这是一个例子:
var Client = {
connect: function(spec) {
this.connection = new Strophe.Connection(spec.url);
},
disconnect: function() {
this.connection.options.sync = true; // Switch to using synchronous requests since this is typically called onUnload.
this.connection.flush();
this.connection.disconnect();
}
}
然后就可以绑定(bind)onbeforeunload/onunload了。 (jQuery 示例)
var client = new Client;
client.connect();
$(window).unload(function() {
client.disconnect();
});
关于jquery - 页面卸载时断开 Strope 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5198410/