Websocket 是否可以跨页面保持Socket.IO客户端套接字处于活动状态,或只是重新连接?

Websocket 是否可以跨页面保持Socket.IO客户端套接字处于活动状态,或只是重新连接?,websocket,socket.io,webrtc,socket.io-1.0,Websocket,Socket.io,Webrtc,Socket.io 1.0,是否有可能: Socket.IO连接设置在特定页面上 浏览到其他页面时: 或:从Cookie(或类似文件)中检索并重新使用套接字 或者:在另一页上,客户端重新连接到具有相同套接字ID的套接字(因为套接字ID链接到特定会话) 我需要该连接在我的应用程序的页面之间保持持久性(用于使用Socket.IO连接在多个设备之间同步行为) 如果根本不可能,WebRTC是否也有相同的限制?也许我可以使用WebRTC来解决这个问题?如果使用单页应用程序,在根元素中创建连接并在任何地方使用它非常简单。 如果

是否有可能:

  • Socket.IO连接设置在特定页面上
  • 浏览到其他页面时:
    • 或:从Cookie(或类似文件)中检索并重新使用套接字
    • 或者:在另一页上,客户端重新连接到具有相同套接字ID的套接字(因为套接字ID链接到特定会话)
我需要该连接在我的应用程序的页面之间保持持久性(用于使用Socket.IO连接在多个设备之间同步行为)


如果根本不可能,WebRTC是否也有相同的限制?也许我可以使用WebRTC来解决这个问题?

如果使用单页应用程序,在根元素中创建连接并在任何地方使用它非常简单。

如果不使用单页应用程序,一些解决方法如下:

e、 我们有a.html和b.html页面

第1种方式(使用iFrame):

第二种方式(使用浏览器扩展):

第三种方式(通过更改内容页):


如果使用单页应用程序,则在根元素中创建连接并在任何地方使用它非常简单。

如果不使用单页应用程序,一些解决方法如下:

e、 我们有a.html和b.html页面

第1种方式(使用iFrame):

第二种方式(使用浏览器扩展):

第三种方式(通过更改内容页):

  1. Wrap all your pages in some iframe let x
  2. Make socket connection in it let mySocketObj
  2. Case 1: a.html/b.html are on different origin:
        > give a xhr/fetch request of a.html/b.html
        > create an iframe tag inside x  let y(don't set any src so as default it will be about:blank)
        > open doc of y and put content of xhr/fetch inside it close doc of y
      Case 2: a.html/b.html are on same origin:
        > create an iframe tag inside x let y.
        > set source as a.html/b.html 
  3. add a dynamic script in y as below:
        window.top.mySocketObj = window. mySocketObj
  4. use window.mySocketObj for socket events.
  1. Make socket connection in extension environment let mySocketObj.
  2. Call/Message extension environment for socket events.
  1. Make socket connection in main page let mySocketObj.
  2. give a xhr/fetch request of a.html/b.html.
  3. put response content in current doc without changing doc/window objects.
  4. use mySocketObj for socket events.