Vue.js eliminates websocket duplicate subscriptions

On the a page of

vue.js, there is an a function that subscribes to websocket. Switch to another page and then switch back. Because the a function of a page is executed in mounted, it subscribes again, causing the client to receive duplicate messages. However, the requirement is that you cannot unsubscribe when you leave page a, so is there any way to make it not repeat subscriptions?


1. When you leave the page, send a message to the server. You need to negotiate with the backend to unsubscribe.
2. When you leave the page, ws has a close () method that you can call.


localStorage defines variables. Every time you open this page, first determine whether there is a value. If there is no value, connect socket and store the value. If you have a value, directly return ah
in `

.
mounted ()  {
    if ( !localStorage.getItem('aaa') ) {
        this.socket();
        localStorage.setItem('sss', 'aaa')
    }
}

`

Menu