How does websocket stop the push of the last url after changing url

At the beginning of the

page, a websocket is requested

wsCurrentEntrust: function () { 
            if ("WebSocket" in window) {
                 websocket = new WebSocket(host + userId);
            }
            else {
                ...
            }
            websocket.onerror = function () {
                ...
            };
            websocket.onopen = function (event) {
                ...
            }
            websocket.onmessage = function (event) {
                ...
            }
            websocket.onclose = function () {
                ...
            }
        }

for example, the url of the ws I requested at first is host1, and then I click a button and then it becomes host2, but the message of host1 is still being pushed, resulting in a problem with the callback of the function. After solving how to change to host2, the message of host1 is no longer pushed ~

Mar.21,2021

websocket.onclose (), disconnects actively, and then recreates the websocket object based on the second url

Menu