|
|
|
@ -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,10 +40,24 @@ const methods = {
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
connect: (uri) => {
|
|
|
|
|
state.ws = new WebSocket(`ws://178.79.128.135:5000/ws/${state.session}`)
|
|
|
|
|
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)
|
|
|
|
@ -46,6 +65,7 @@ const methods = {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
state,
|
|
|
|
|