Sharing websocket across browser tabs?

后端 未结 9 2067
半阙折子戏
半阙折子戏 2020-12-04 18:08

We want to have one socket per browser rather than one per tab in a browser. How can we achieve it? I read about shared web workers which was promising. A reference for that

9条回答
  •  爱一瞬间的悲伤
    2020-12-04 18:25

    I am using localStorage as a shared communication channel in order to send data between tabs using an interface identical to EventEmitters. Coupled with a leader election algorithm that decides which tab will be the one connected to the server, I relay all socket events to the leader tab from all follower tabs and vice versa. And finally, the leader tab forwards all events to the server, and broadcasts all received events to all other clients. Here's the code:

    • https://github.com/majestic3/athena-online-judge/blob/master/source/client/core/itc.js
    • https://github.com/majestic3/athena-online-judge/blob/master/source/client/core/socket.js

提交回复
热议问题