You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
apl-slapdash/src/utils/websocket.js

96 lines
1.6 KiB
JavaScript

import store from '../store'
const events = [
'create',
'read',
'update',
'delete',
]
const channels = [
'rosters',
'events',
'matches',
'casters',
'hosts',
'streams',
'brackets',
]
const channelEvents = [
'info',
'streams:full',
]
channels.forEach((x) => {
events.forEach((y) => {
channelEvents.push(`${x}:${y}`)
})
})
export const sub = []
export const handleMsg = (msg) => {
if (channelEvents.indexOf(msg.event) !== -1) {
const ev = msg.event.split(':')
const c = ev[0]
const e = ev[1]
console.log(c, e)
if (e === 'read') {
store.state[c] = msg.data
console.log(msg.data)
} else if (e === 'full') {
store.state.streamsFull = msg.data
sub.forEach(x => {
x()
})
}
} else {
console.warn('received unknown event!', msg)
}
}
export const sendSub = (ws) => {
channels.forEach(c => {
const d = {
subscribe: `${c}:read`
}
ws.send(JSON.stringify(d))
})
ws.send(JSON.stringify({
subscribe: `streams:full`
}))
}
export const create = (channel, data) => {
if (channels.indexOf(channel) !== -1) {
store.state.ws.send(JSON.stringify({
event: `${channel}:create`,
data,
}))
}
}
export const update = (channel, id, data) => {
if (channels.indexOf(channel) !== -1) {
store.state.ws.send(JSON.stringify({
event: `${channel}:update`,
data: {
id: id,
data: data,
},
}))
}
}
export const del = (channel, id) => {
if (channels.indexOf(channel) !== -1) {
store.state.ws.send(JSON.stringify({
event: `${channel}:delete`,
data: {
id,
},
}))
}
}