WebSockets API - 可以保持连接或资源/套接字标识吗? [英] WebSockets API - Persist connections or resource/socket identification possible?

查看:102
本文介绍了WebSockets API - 可以保持连接或资源/套接字标识吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人知道是否可以使用HTML WebSocket规范捕获资源或连接ID之类的内容,以便您可以重新使用或重新打开上一页的连接。用例是我在一个页面上建立一个Web套接字连接,然后用户导航到一个单独的页面,但我不想重新建立新连接,而是使用前一个连接。您可能会问为什么让用户完全刷新页面?这是一个合理的问题但不适用于此用例:)

Does anybody know if it is possible to capture something like a resource or connection id using the HTML WebSocket specification so that you can re-use or reopen a connection from a previous page. Use case would be I establish a web socket connection while on one page, then the user navigates to a separate page but I don't want to re-establish a new connection but rather use the previous one. You may be asking 'why let the user completely refresh the page?' and it's a legitimate question but not for this use case :)

谢谢。

推荐答案

如果您在浏览器上下文中打开一个新选项卡并且原始文件仍然连接,您可以使用例如 HTML5存储,用于在标签之间传递消息。使用此方法,只有第一个选项卡将通过WebSockets与服务器连接,并且通信可以存储到本地存储。其他选项卡将处理存储事件,因此不需要连接它们。

If you open a new tab in your browser context and the original is still connected you can use for example HTML5 storage to pass messages between tabs. With this approach only the first tab will be connected with server through WebSockets and communication can be stored to local storage. Other tabs would handle storage events and thus they don't need to be connected.

这篇关于WebSockets API - 可以保持连接或资源/套接字标识吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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