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

60
app.js
View File

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

View File

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