javascript COMET 请求 onunload

发布于 2024-11-05 00:41:28 字数 638 浏览 1 评论 0原文

我最近接手了一个项目,该项目使用 COMET 来执行一些协作工作并处理一个简单的聊天室。最初写这个东西的人在 STOMP 和 Oribited 之上创建了一些类来处理所有实际的聊天、消息传递和日志记录。

问题是,如果用户关闭窗口或导航到不同的页面或出于任何其他原因终止连接,所有其他用户需要一段时间才能看到他已注销。其他用户必须等待退出用户的最后一次 ping 的时间戳超过一定的持续时间,然后才能注册该用户不再连接到系统。

我能想到的解决方案需要在用户离开的onuload事件中发出通知,这样就可以通知所有其他用户,而不必等待超时。这样做的问题是,因为 onunload 会在连接完成之前立即终止连接。据我了解,这也是 AJAX 的问题。

现在,我还了解到卸载中的同步请求将延迟窗口关闭/导航,直到请求完成。

所以,我的问题是:有谁知道一种方法可以暂时使彗星请求在选定的实例中同步,以便它有时间在终止之前完成请求?或者还有其他我没有想到的方法来解决这个问题?感谢您的帮助。

哦,还有, onbeforeunload 不起作用,因为如果它发送请求并且用户选择“不,我想留在此页面”,它将已经通知其他用户他已退出聊天。

tl;dr:需要一种方法在 Unload 事件中成功触发 COMET 请求。我们使用 STOMP 和 Orbited 来处理 COMET 的内容。

I've recently taken over a project that uses COMET to perform some collaborative work and handle a simple chat room. The guys who originally wrote this thing made up some classes on top of STOMP and Oribited to handle all the actual chatting and messaging and logging.

The problem is that if a user closes the window or navigates to a different page or terminates the connection for whatever other reason, it takes a while for all the other users to see that he has logged off. The other users have to wait for the timestamp of the exited-user's last ping to exceed a certain duration before it registers that the user is no longer connected to the system.

The solution that I can think of requires sending out a notification in the onuload event that the user has left, so that it would notify all the other users without having to wait for a timeout. The problem with this is that since onunload will immediately terminate the connection before it's completed. From what I understand this is a problem with AJAX as well.

Now, I also have read that a Synchronous request in unload will delay the window-close/navigation until the request has finished.

So, my questions is this: does anyone know of a way to temporarily make the comet request synchronous in selected instances so it has time to finish the request before terminating? Or is there another way to solve this problem that I'm not thinking of? Thanks for your help.

Oh, also, onbeforeunload won't work because if it sends the request and the user selects "No, I want to stay on this page" it will have already have notified the other users that he has exited the chat.

tl;dr: Need a way to successfully fire a COMET request in the Unload event. We're using STOMP and Orbited for the COMET stuff.

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

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

发布评论

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

评论(2

幸福丶如此 2024-11-12 00:41:28

仅当“onbeforeunload”函数返回某个值时,它才会生成是/否对话框。因此,您需要做的是在 onbeforeunload 函数内使用同步 XMLHttpRequest (AJAX) 请求,而不返回任何内容。并且您必须将请求的异步标志设置为 false,如下面所示的 AJAX GET 请求所示:-

  AJAXObject.open("GET", 'http://yourdomain/logout?somevar=something', false);
    AJAXObject.send(null);

它将阻止浏览器关闭,直到请求完成为止,并且如下所示我记得,Opera 不支持“onbeforeunload”,所以它不适用于 Opera。但它在 IE、FF、Chrome 上运行良好。

The 'onbeforeunload' function produces a yes-no dialog only if some value is returned from it. So what you have to do is to use a SYNCHRONOUS XMLHttpRequest (AJAX) request inside the onbeforeunload function without returning anything. And you have to set the asynchronous flag of the request to false as seen in the AJAX GET request shown below:-

  AJAXObject.open("GET", 'http://yourdomain/logout?somevar=something', false);
    AJAXObject.send(null);

It will prevent the browser from closing until request completes and as I remember, Opera doesn't support 'onbeforeunload', so it won't work for Opera. But it works fine on IE,FF,Chrome.

给我一枪 2024-11-12 00:41:28

如果你使用comet,那么你应该控制服务器。 Comet 的想法是它不是不断轮询服务器。每个客户端都应该与服务器保持持续的开放连接。因此,当连接关闭时,服务器应该能够向其他客户端发送通知。

If you are using comet, then you should control the server. The idea with comet is that it is not constant polling of the server. Every client should have a constant open connection to the server. As such, when the connection closes, the server should be able to send out a notification to the other clients.

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