通过页面刷新和页面更改持久化套接字 [英] Persist Sockets over page refresh and page changes

查看:47
本文介绍了通过页面刷新和页面更改持久化套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 NodeJS 和 Socket.IO 服务器运行良好,但是我注意到每次刷新浏览器页面或导航到不同页面时都会发生套接字断开和重新连接.

I have my NodeJS and Socket.IO server running perfectly, however I notice that a socket disconnect and reconnect occurs every time I refresh the browswer page or navigate to a different page.

如何使套接字在页面之间持续存在?

How do I make it so sockets persist between pages?

谢谢,我想到了这一点,但想避免它,因为这对我正在制作的系统来说是一个相当大的变化,而且工作量很大.

Thanks, I thought of that but wanted to avoid it as it's quite a big change for the system I'm making and would be a fair bit of work.

套接字连接/断开连接唯一会影响的是用户登录"计数器.但我认为我可以通过 Node 服务器上的 setTimeout 解决这个问题,所以如果套接字在 2 秒内断开连接并重新连接,计数器不会改变.就这个计数器而言,这是最好的解决方法吗?

The only thing the socket connects/disconnects will effect is a "users logged in" counter. But I think I can get around that with a setTimeout on the Node server, so if a socket disconnects and reconnects within 2 seconds, the counter won't change. Is that the best work around in terms of this counter?

推荐答案

您不能真正做到这一点:网络连接无法在页面导航或刷新后继续存在.如果您真的很在意,您基本上需要构建一个单页应用程序.

You can't really do that: network connections do not survive page navigation or refreshes. If you really really care you'll basically need to build a single page application.

这篇关于通过页面刷新和页面更改持久化套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆