From 9254b3b69c46fafcb45a9b5b47eff1be7b224972 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Sat, 3 Apr 2021 11:06:34 +0530 Subject: [PATCH] housekeeping, reconnect on ws break, change server --- package.json | 2 +- src/components/Streams.vue | 4 ++-- src/store.js | 38 +++++++++++++++++++++++++++++--------- src/views/Home.vue | 5 ++--- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index f5ad90f..dff6983 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "slapdash", - "version": "1.4.0", + "version": "1.5.0", "author": "Ayush Mukherjee", "description": "Dashboard for APL-Nuke cum APL-Boost data manager", "private": false, diff --git a/src/components/Streams.vue b/src/components/Streams.vue index bfc7ef4..00506ab 100644 --- a/src/components/Streams.vue +++ b/src/components/Streams.vue @@ -40,7 +40,7 @@ - +
{{ store.state.hosts.filter(x => x._id === p)[0]?.name }}{{ store.state.casters.filter(x => x._id === p)[0]?.name }}
@@ -70,7 +70,7 @@

Hosts

diff --git a/src/store.js b/src/store.js index e303308..70d7e59 100644 --- a/src/store.js +++ b/src/store.js @@ -4,6 +4,8 @@ import { handleMsg, sendSub } from './utils/websocket' const token = window.localStorage.getItem('token') +const server = 'slap.aplesports.com:5000' + const state = reactive({ name: '', token: token || '', @@ -22,10 +24,13 @@ const state = reactive({ server: false, }) +let interval = 0 + const methods = { authenticate: async (token) => { + state.session = null window.localStorage.setItem('token', token) - return axios.post('http://178.79.128.135:5000/api/v1', { + return axios.post(`http://${server}/api/v1`, { token, }) .then((res) => { @@ -35,14 +40,29 @@ const methods = { return true }) }, - connect: (uri) => { - state.ws = new WebSocket(`ws://178.79.128.135:5000/ws/${state.session}`) - state.ws.onopen = () => { - sendSub(state.ws) - } - state.ws.onmessage = (e) => { - const data = JSON.parse(e.data) - handleMsg(data) + connect: () => { + if (state.session !== null) { + state.ws = new WebSocket(`ws://${server}/ws/${state.session}`) + state.ws.onopen = () => { + clearInterval(interval) + sendSub(state.ws) + state.ws.onclose = () => { + console.warn('Lost connection to nuke. Attempting to reconnect every 5 seconds...') + interval = setInterval(() => { + methods.authenticate(state.token) + .then(() => { + methods.connect() + }) + .catch((_) => { + console.warn('Error reconnecting! Trying again') + }) + }, 5000) + } + } + state.ws.onmessage = (e) => { + const data = JSON.parse(e.data) + handleMsg(data) + } } } } diff --git a/src/views/Home.vue b/src/views/Home.vue index 0df329e..60b7fe6 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -6,8 +6,8 @@ - - + + APL Logo @@ -32,7 +32,6 @@ -