当客户端刷新或打开新页面时,Socket.io 在客户端保持连接 [英] Socket.io keep the connection on client side when client refresh or open new page

查看:57
本文介绍了当客户端刷新或打开新页面时,Socket.io 在客户端保持连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户打开新页面或刷新页面,是否有办法在客户端保持相同的 socket.io 连接,也许将套接字存储在会话中,或者这是不可能的?

Have some way to keep the same socket.io connection on client side if user open a new page or refresh the page, maybe store the socket in session or it's impossible?

推荐答案

这是不可能的.当页面更改或刷新时,您不能保持相同的 socket.io 或 webSocket 客户端连接.浏览器根本不会这样做.当加载新页面或刷新当前页面时,浏览器会关闭并释放上一页面的所有资源,包括 socket.io/webSocket 连接.

It's impossible. You cannot keep the same socket.io or webSocket client connection when the page is changed or refreshed. The browser simply does not do that. When a new page is loaded or the current page is refreshed, all resources from the previous page are closed and freed by the browser, including socket.io/webSocket connections.

因此,您的服务器必须期待来自新加载页面的新 socket.io 连接.如果您使用 cookie 或服务器端会话对象,您可以在服务器上识别何时连接来自您之前见过的客户端,然后服务器可以采取相应的行动来意识到这只是之前的客户端重新连接在新页面上.

So, your server has to expect a new socket.io connection from the newly loaded page. If you use cookies or a server-side session object, you can identify, on the server, when a connection is coming from a client that you have previously seen and the server can then act accordingly to realize that this is just a previous client reconnecting on a new page.

这篇关于当客户端刷新或打开新页面时,Socket.io 在客户端保持连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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