以任何可能的方式:
我需要连接才能在我的应用程序的页面之间保持持久性(使用Socket.IO连接在多个设备之间同步行为)。
如果根本不可能,WebRTC是否也有相同的限制?也许我可以使用WebRTC来解决这个问题?
答案 0 :(得分:0)
如果使用单页面应用程序,则非常简单create connection in root element and use it everywhere.
如果不使用单页面应用程序,则有一些解决方法如下:
e.g。我们有页面a.html,b.html
第一种方式(使用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.