6

我已经使用 strophe.js 和 jQuery 编写了一个基于 Web 的 MUC 客户端,我正在尝试向房间发送不可用的状态,并在窗口的 jquery 卸载事件中断开用户的连接。如果用户离开或关闭浏览器选项卡,他们应该退出 MUC。

我已经通过页面上的注销按钮测试了我在此事件中运行的代码,所以我很确定该节是正确的。如果浏览器窗口正在关闭,我认为 strophe 无法发送节。这里有什么解决方法吗?我也尝试过 onbeforeunload 事件(我知道它不完全跨浏览器兼容),但这似乎也不起作用。

非常感谢任何建议!

谢谢,约翰

4

2 回答 2

11

确保您切换到同步模式,并flush()在发送呼叫之前进行disconnect()呼叫。这是一个例子:

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();
  }
}

然后你可以绑定到onbeforeunload/onunload。(jQuery 示例)

var client = new Client;
client.connect();
$(window).unload(function() {
  client.disconnect();
});
于 2011-03-16T00:12:19.920 回答
2

不需要补丁,现在有一个内置的解决方案:

connection.flush();
connection._options.sync = true;    
connection.disconnect();   
于 2014-01-05T20:00:13.130 回答