Added client socket

This commit is contained in:
Thomas
2020-08-21 15:53:16 +02:00
parent 861105eb92
commit e2e4495f02
17 changed files with 200 additions and 55 deletions

View File

@@ -0,0 +1,48 @@
const connection = new WebSocket('ws://localhost:8181/socket')
// const connection = new WebSocket('wss://tronio.gltronic.ovh/socket')
export default function createSocketPlugin () {
return store => {
connection.onopen = function () {
console.log('[WS] connected')
store.dispatch('game/socketConnected')
}
connection.onclose = function () {
console.log('[WS] closed')
}
connection.onerror = function (error) {
console.log('[WS] error ' + error.message)
store.dispatch('game/error', 'Connection problem')
}
connection.onmessage = function (message) {
console.log('[WS] message', message.data)
var data = JSON.parse(message.data)
switch (data.type) {
case 'login':
store.dispatch('game/login', data.message)
break
case 'gameSettings':
store.dispatch('game/settings', data.message)
break
case 'gameUpdate':
store.dispatch('game/update', data.message)
break
case 'gamePlayerDead':
store.dispatch('game/dead')
break
default:
break
}
}
}
}
export function send (message) {
console.log('[WS] send', message)
connection.send(JSON.stringify(message))
}