如何在 ReactJS 中以安全的方式跨页面刷新建立持久的 Web 套接字连接?

How to make persistent web socket connection across page refresh in a secure way in ReactJS?

我正在使用网络套接字从 reactjs 客户端与后端服务器通信。当用户刷新页面时,套接字将关闭。这正在按预期发生。但是,我的要求是在页面刷新时保持持久的 Web 套接字连接。我目前正在使用 localstorage 来存储访问权限、刷新令牌。

我知道这不是解决问题的安全方法。我想知道是否有任何安全的方法可以实现我的目标。

我不确定这是否有效,因为即使用户不在您的页面上也能够让用户连接是一种安全漏洞。 (因为你在刷新的时候离开了页面,然后重新进入)。

你最好的选择可能是可以在后台 运行 的服务工作者:)

Since you are using Web Sockets it would be a good idea to use a SharedWorker to create a new thread for your Web Sockets. The difference between a normal WebWorker and a SharedWorker is that the web worker will create a new session in each tab or browser when loading the page, whereas the shared worker will use the same session in each tab. So all of your tabs or windows will have the same worker and same Web Socket connection to work with.

我引用了这个答案,它可能会为您指明正确的方向: