fix: removed multiple message events added to CustomWebsocket

This commit is contained in:
Roberto Tonino 2021-03-01 21:22:32 +01:00
parent c6a2f35fbf
commit cbf3a5c677
4 changed files with 1384 additions and 1334 deletions

View File

@ -7,5 +7,6 @@
"trailingComma": "none", "trailingComma": "none",
"printWidth": 120, "printWidth": 120,
"arrowParens": "avoid", "arrowParens": "avoid",
"vueIndentScriptAndStyle": false "vueIndentScriptAndStyle": false,
"endOfLine": "lf"
} }

File diff suppressed because one or more lines are too long

View File

@ -32,6 +32,30 @@ function startApp() {
i18n, i18n,
render: h => h(App) render: h => h(App)
}).$mount('#app') }).$mount('#app')
fetch('connect')
.then(response => response.json())
.then(data => {
store.dispatch('setAppInfo', data.update)
if (data.autologin) {
console.log('Autologin')
let arl = localStorage.getItem('arl')
let accountNum = localStorage.getItem('accountNum')
if (arl) {
arl = arl.trim()
let result
if (accountNum != 0) {
result = get('login', { arl: arl, force: true, child: accountNum || 0 })
} else {
result = get('login', { arl })
}
result.then(loggedIn)
}
}
})
} }
function initClient() { function initClient() {
@ -53,7 +77,7 @@ document.addEventListener('paste', pasteEvent => {
if (router.currentRoute.name === 'Link Analyzer') { if (router.currentRoute.name === 'Link Analyzer') {
socket.emit('analyzeLink', pastedText) socket.emit('analyzeLink', pastedText)
} else { } else {
if (pastedText.indexOf("\n") != -1) pastedText = pastedText.replace(/\n/g, ';'); if (pastedText.indexOf('\n') != -1) pastedText = pastedText.replace(/\n/g, ';')
sendAddToQueue(pastedText) sendAddToQueue(pastedText)
} }
} else { } else {
@ -122,27 +146,6 @@ function loggedIn(data){
} }
} }
fetch('connect').then(response => response.json()).then(data => {
store.dispatch('setAppInfo', data.update )
if (data.autologin) {
console.log("Autologin")
let arl = localStorage.getItem('arl')
let accountNum = localStorage.getItem('accountNum')
if (arl) {
arl = arl.trim()
let result
if (accountNum != 0) {
result = get('login', {arl: arl, force:true, child:accountNum || 0})
} else {
result = get('login', {arl})
}
result.then(loggedIn)
}
}
})
/* /*
socket.on('logging_in', function() { socket.on('logging_in', function() {
toast(i18n.t('toasts.loggingIn'), 'loading', false, 'login-toast') toast(i18n.t('toasts.loggingIn'), 'loading', false, 'login-toast')

View File

@ -1,22 +1,32 @@
import store from '@/store' import store from '@/store'
let wasEventListenerAdded = false
class CustomSocket extends WebSocket { class CustomSocket extends WebSocket {
constructor(args) { constructor(args) {
super(args) super(args)
} }
emit(key, data) { emit(key, data) {
if (this.readyState != WebSocket.OPEN) return false if (this.readyState !== WebSocket.OPEN) return false
this.send(JSON.stringify({ key: key, data: data })) this.send(JSON.stringify({ key: key, data: data }))
} }
on(key, cb) {
if (!wasEventListenerAdded) {
wasEventListenerAdded = true
on(key, callback) { this.addEventListener('message', event => {
this.addEventListener('message', function(event){ const messageData = JSON.parse(event.data)
let data = JSON.parse(event.data)
console.log(data) if (messageData.key === key) {
if (data.key == key) callback(data.data) cb(messageData.data)
}
}) })
} }
} }
off() {
console.log('off!')
// this.removeEventListener('message')
}
}
export const socket = new CustomSocket('ws://' + location.host + '/') export const socket = new CustomSocket('ws://' + location.host + '/')