页面卸载时断开 Strope 连接

发布于 2024-10-20 18:26:05 字数 310 浏览 0 评论 0原文

我已经使用 strope.js 和 jQuery 编写了一个基于 Web 的 MUC 客户端,并且我正在尝试发送 无法进入房间并在 jquery 中断开用户连接 窗口的卸载事件。如果用户离开或关闭 浏览器选项卡中,他们应该从 MUC 中注销。

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

非常感谢任何建议!

谢谢, 约翰

I've written a web-based MUC client using strophe.js and jQuery and I'm trying to send an
unavailable presence to the room and disconnect the user in the jquery
unload event for the window. If a user navigates away or closes the
browser tab, they should be logged out of the MUC.

I've tested the code that I'm running in this event through a logout
button I have on the page, so I'm pretty sure the stanza is correct.
I think strophe is having trouble sending the stanza if the browser
window is closing. Is there any workaround here? I've also tried the
onbeforeunload event (I know it's not entirely cross-browser
compatible), but that doesn't seem to work either.

Any advice is much appreciated!

Thanks,
John

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

讽刺将军 2024-10-27 18:26:05

确保切换到同步模式,并在发送 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();
  }
}

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

var client = new Client;
client.connect();
$(window).unload(function() {
  client.disconnect();
});

Ensure you switch to sync mode, and call flush() before sending your disconnect() call. Here is an example:

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

Then you can bind to onbeforeunload/onunload. (jQuery example)

var client = new Client;
client.connect();
$(window).unload(function() {
  client.disconnect();
});
扛起拖把扫天下 2024-10-27 18:26:05

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

connection.flush();
connection._options.sync = true;    
connection.disconnect();   

No patches needed, there's now a built-in solution:

connection.flush();
connection._options.sync = true;    
connection.disconnect();   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文