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-nuke/src/ws/streams.js

52 lines
907 B
JavaScript

const { v4 } = require('uuid')
const crud = require('./crud')
const connections = {}
const subscribe = (ws, sid) => {
const id = v4()
connections[id] = {
connection: ws,
streamid: sid,
}
ws.send(JSON.stringify({
event: 'info',
data: 'Welcome to APL Nuke v1.1.0!',
}))
sendInitial(id)
ws.on('close', () => {
delete connections[id]
})
}
const cModel = {
'events': 'event',
'matches': 'matches',
'casters': 'casters',
'hosts': 'hosts',
}
const recvUpdate = async (channel, data) => {
Object.keys(connections).forEach((k) => {
sendInitial(k)
})
}
const sendInitial = async (id) => {
const c = connections[id]
if (c.streamid !== null) {
const stream = await crud['streams'].getById(c.streamid)
c.connection.send(JSON.stringify({
event: 'streams:read',
data: stream,
}))
}
}
module.exports = {
subscribe,
recvUpdate,
}