是否可以将socket.io客户端套接字保持在各个页面上,或者简单地重新连接



是任何可能的:

  • socket.io连接在某个页面上设置
  • 浏览到另一页:
    • 或:从cookie(或类似(中检索插座并将其重复使用
    • 或:使用相同的套接字ID 将客户端重新连接到套接字(由于套接字ID在我的情况下链接到特定会话(,在另一页上。

我需要连接才能在我的应用程序上保持持久性(用于使用socket.io连接跨多个设备的行为同步(。

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

如果使用单页应用程序,则很简单create connection in root element and use it everywhere.

如果不使用单页申请,一些解决方法如下:

例如。我们有页面a.html,b.html

1st Way(使用IFRAMES(:

  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.

3rd Way(通过更改内容页面(:

  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.

最新更新