Переподключение вебсокетов при обрыве

This commit is contained in:
saber-nyan 2020-04-06 21:40:03 +03:00
parent 4c5522bbcc
commit c9732eb5b4
No known key found for this signature in database
GPG Key ID: 7B069E9C295E6B4A

View File

@ -42,16 +42,22 @@
},
methods: {
connectWs() {
if (this.websocket !== null) return;
this.websocket = new SockJS(this.$http.defaults.baseURL + '/ws');
this.websocket.onopen = () => {
console.info('[WS] Connected');
};
this.websocket.onclose = (ev) => {
console.info('[WS] Disconnected', ev.code, ev.reason);
this.websocket = null;
if (ev.code === 1008) {
console.info('[WS] Security timeout, reconnecting...');
this.connectWs();
}
if (ev.code !== 1000) { // Normal closure
setTimeout(this.connectWs, 3000);
console.info('[WS] Reconnecting...');
}
};
this.websocket.onmessage = (ev) => {
const parsed = JSON.parse(ev.data);