implemented per-stream data views
parent
2ca601bc02
commit
99467f229c
@ -0,0 +1,53 @@
|
||||
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) => {
|
||||
const streams = await crud['streams'].getAll()
|
||||
const stream = streams.filter(x => x[cModel[channel]]._id === data._id)[0]
|
||||
const c = connections.filter(x => x.streamid === stream._id)
|
||||
Object.keys(c).forEach((k) => {
|
||||
c[k].connection.send(JSON.stringify(stream))
|
||||
})
|
||||
}
|
||||
|
||||
const sendInitial = async (id) => {
|
||||
const c = connections[id]
|
||||
const stream = await crud['streams'].getById(c.streamid)
|
||||
c.connection.send(JSON.stringify({
|
||||
event: 'streams:read',
|
||||
data: stream,
|
||||
}))
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
subscribe,
|
||||
recvUpdate,
|
||||
}
|
Loading…
Reference in New Issue