A few improvements

This commit is contained in:
Evert Prants 2019-10-24 10:34:29 +03:00
parent 1d2858aa22
commit a2a80bd4c4
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
2 changed files with 47 additions and 42 deletions

44
app.js
View File

@ -449,7 +449,7 @@ app.get('/api/channel/:name', async (req, res) => {
let db = await dbPromise
let data = await db.get('SELECT user_uuid,name,live_at,last_stream FROM channels WHERE name=?', name)
if (!data) return res.jsonp({ error: 'No such channel!' })
let links = await db.all('SELECT * FROM link WHERE uuid = ?', data.user_uuid)
let links = await db.all('SELECT name,url FROM link WHERE uuid = ?', data.user_uuid)
delete data.user_uuid
data.live = data.live_at != null
@ -457,11 +457,7 @@ app.get('/api/channel/:name', async (req, res) => {
data.last_stream = new Date(parseInt(data.last_stream))
data.links = links || []
data.viewers = Object.keys(cache.viewers[name] || {}).length
for (let i in data.links) {
delete data.links[i].id
delete data.links[i].uuid
}
data.source = streamServer + name + '.m3u8'
res.jsonp(data)
})
@ -476,36 +472,42 @@ app.use((error, req, res, next) => {
wss.on('connection', (ws, request, client) => {
const userId = request.session.login || request.session.id
const username = request.session.username
let myChannels = []
dev && console.log(username || userId,'connected')
dev && console.log(userId, 'connected')
ws.on('message', (msg) => {
dev && console.log(userId,'said',msg)
if (msg.indexOf('watch ') === 0) {
let chan = msg.substring(6)
dev && console.log('adding a watcher to channel',chan)
dev && console.log(userId, 'said', msg)
let is = msg.toString().trim().split(' ')
let chan = is[1]
if (!chan) return
switch (is[0]) {
case 'watch':
dev && console.log('adding watcher', userId, 'to channel', chan)
if (cache.live.indexOf(chan) !== -1) {
if (!cache.viewers[chan]) cache.viewers[chan] = {}
cache.viewers[chan][userId] = username || 'A Friendly Guest'
if (myChannels.indexOf(chan) === -1) myChannels.push(chan)
}
} else if (msg.indexOf('stop ') === 0) {
let chan = msg.substring(5)
dev && console.log('removing a watcher from channel',chan)
break
case 'stop':
dev && console.log('removing watcher', userId, 'from channel', chan)
if (cache.live.indexOf(chan) !== -1) {
if (cache.viewers[chan] && cache.viewers[chan][userId]) delete cache.viewers[chan][userId]
if (myChannels.indexOf(chan) !== -1) myChannels.splice(myChannels.indexOf(chan), 1)
}
} else if (msg.indexOf('viewers ') === 0) {
let chan = msg.substring(8)
break
case 'viewers':
if (cache.viewers[chan] != null) ws.send('viewlist ' + Object.values(cache.viewers[chan]).join(','))
break
}
})
ws.on('close', () => {
dev && console.log(userId,'disconnected')
for (let chan in cache.viewers) {
dev && console.log(userId, 'disconnected')
for (let i in myChannels) {
let chan = myChannels[i]
let viewers = cache.viewers[chan]
if (viewers[userId]) {
delete cache.viewers[chan][userId]
}
if (viewers && viewers[userId]) delete cache.viewers[chan][userId]
}
})

View File

@ -129,18 +129,13 @@ function liveStatus (status) {
lstat.className = 'badge live'
clearTimeout(retryTimeout)
if (vid.paused) {
showBigBtn(true)
}
handleWebSocket(true)
if (vid.paused) showBigBtn(true)
} else {
lstat.innerHTML = 'offline'
lstat.className = 'badge live offline'
viewers.style.display = 'none'
handleWebSocket(false)
}
handleWebSocket()
}
function hide () {
@ -174,17 +169,11 @@ function toggleStream () {
if (!vid) return
if (!vidReady) return
if (vid.paused) {
if (ws) ws.send('watch ' + STREAM_NAME)
hls.startLoad(-1)
vid.play()
btn.innerHTML = '<i class="fa fa-pause fa-fw"></i>'
showBigBtn(false)
} else {
if (ws) ws.send('stop ' + STREAM_NAME)
hls.stopLoad()
vid.pause()
btn.innerHTML = '<i class="fa fa-play fa-fw"></i>'
showBigBtn(true)
}
}
@ -238,6 +227,18 @@ function toggleFullscreen () {
}
}
function handlePlay () {
if (ws) ws.send('watch ' + STREAM_NAME)
btn.innerHTML = '<i class="fa fa-pause fa-fw"></i>'
showBigBtn(false)
}
function handlePause () {
if (ws) ws.send('stop ' + STREAM_NAME)
btn.innerHTML = '<i class="fa fa-play fa-fw"></i>'
showBigBtn(true)
}
window.addEventListener('onblur', () => {
shouldHide = true
}, false)
@ -446,5 +447,7 @@ window.addEventListener('resize', function () {
})
vid.addEventListener('timeupdate', updateTime, false)
vid.addEventListener('play', handlePlay, false)
vid.addEventListener('pause', handlePause, false)
getStreamStatus()