Sharing websocket across browser tabs?

后端 未结 9 2080
半阙折子戏
半阙折子戏 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条回答
  •  旧时难觅i
    2020-12-04 18:17

    I'm still theorizing on this for a design I'm getting ready to start building. But, I'm thinking of combining

    -WebSockets -Local Storage and -Cross Window Messaging

    My theory is to create a socket engine in javascript that runs on every page load in every tab, but will shut down if one already has an established connection.

    On first hit to the site, I'll have it make a GUID and store it in local storage, this guid will uniquely identify that users browser/login to their PC.

    When the socket server accepts a connection it will have that guid, and any new request by that guid will return "999 Connection Already Established" or something like that.

    Once one is running, It will seed the other tabs with cross window messaging by converting the data I want to share accross tabs to a JSON blob and then converting that back to an object when received in other tabs. So whichever tab get's the connection, will be handling all incoming/outgoing messages with the socket server. Then it will receive/transmit with the other tabs over cross window messaging. And in theory this should work with Iframe's and Popup windows as well.

    This whole system will drive automatic data refreshes on loaded forms for a CRM like system we are building and a live chat system and ticket board.

    My dream scenario, is if User A is staring at Ticket 1000 and User B updates ticket 1000 I want user A's ticket to refresh, and if user A made changes before it refreshed I want to give them a data migration pop up to prevent blowing away User B's changed

    --User B Made a Conflicting Change while you are editing this record "UserB: FirstName -> Bob" [Take] "UserA: FirstName -> Robert" [Keep]

提交回复
热议问题