我有一个案例,客户端连接到运行 nowjs 的 Node.js 服务器并保持连接相当长的时间(大约 30 分钟)。在某些浏览器上,虽然连接会在一段时间后断开并且客户端会断开连接。
我在客户端实现了一个断开连接处理程序,如下所示:
now.core.on('disconnect', function () {
// we should reconnect here, maybe after a short timeout
});
我不清楚的是如何准确触发重新连接。对于有经验的用户来说,这可能是显而易见的事情,但我没有设法弄清楚这一点。
now.js 脚本在页面加载时初始化,之后我可以使用 now 对象,但我不知道如何在不重新加载页面的情况下重复此过程。
谢谢!
i have a case where clients connect to a node.js server running nowjs and remain connected for a fairly long time (about 30 minutes). on some browsers though the connection gets dropped after a while and the client disconnects.
i implemented a disconnect handler on the client side like this:
now.core.on('disconnect', function () {
// we should reconnect here, maybe after a short timeout
});
what i am unclear about is how exactly to trigger a reconnect. this might be something blatantly obvious to experienced users but i didn't manage to figure this out.
the now.js script initializes on page load and after that i can use the now object, but i can't figure out how to repeat this process without reloading the page.
thanks!
发布评论
评论(2)
是的,从 v0.6.1 开始,NowJS 支持自动重新连接。
该网站需要更新,但是有一篇博客文章详细介绍了 NowJS 中重新连接的实现此处。
Yes, automatic reconnects are supported with NowJS, as of v0.6.1.
The website needs to be updated, but there's a blog post detailed the implementation of reconnects in NowJS here.
该网站上的文档有点过时了——从 0.6.1 版本(6 月 10 日发布;npm 上的当前版本)开始,支持自动重新连接。它主要只是利用 Socket.IO v0.6.18 中的自动重新连接功能,尽管还需要进行一些其他杂项调整(显然有一篇关于它的博客文章 blog.nowjs.com)。
The documentation on the site is a bit outdated -- as of version 0.6.1 (released on June 10; current version on npm), automatic reconnects are supported. It mostly just takes advantage of the automatic reconnect feature in Socket.IO v0.6.18, although there were a few other miscellaneous tweaks that had to be made (there's apparently a blog post about it at blog.nowjs.com).