search clear, revert socketio bs
This commit is contained in:
parent
79a7177714
commit
69b65fb8ec
@ -181,11 +181,24 @@ tr:nth-child(even) {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
min-height: fit-content;
|
||||
position: relative;
|
||||
}
|
||||
.inline-flex input {
|
||||
flex-grow: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
#search-clear {
|
||||
display: none;
|
||||
}
|
||||
.btn-clear {
|
||||
cursor: pointer;
|
||||
position: absolute;
|
||||
right: 10px;
|
||||
top: 12px;
|
||||
font-size: 2em;
|
||||
opacity: 0.5;
|
||||
padding: 0 10px;
|
||||
}
|
||||
.queue-tag {
|
||||
padding: 10px;
|
||||
background-color: #007d32;
|
||||
|
@ -17,6 +17,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<input type="text" id="search" placeholder="Search" class="flex-row">
|
||||
<span class="btn-clear" id="search-clear">x</span>
|
||||
</div>
|
||||
<div class="table-container flex-row">
|
||||
<table width="100%">
|
||||
|
@ -1,6 +1,7 @@
|
||||
(function () {
|
||||
var table = document.getElementById('ttable')
|
||||
var input = document.getElementById('search')
|
||||
var clear = document.getElementById('search-clear')
|
||||
var audio = document.getElementById('player')
|
||||
var playing = document.getElementById('playing')
|
||||
|
||||
@ -307,6 +308,7 @@
|
||||
searching = false
|
||||
pagePrev = 0
|
||||
pages = pagesPrev
|
||||
clear.style.display = 'none'
|
||||
}
|
||||
|
||||
if (page > pages) return
|
||||
@ -330,6 +332,7 @@
|
||||
pagePrev = pageNum
|
||||
pagesPrev = pages
|
||||
searching = true
|
||||
clear.style.display = 'block'
|
||||
}
|
||||
|
||||
httpGet('/api/tracks/search?q=' + query + '&page=' + page).then(function (data) {
|
||||
@ -401,6 +404,11 @@
|
||||
|
||||
audio.addEventListener('ended', playNext, false)
|
||||
|
||||
clear.addEventListener('click', function () {
|
||||
input.value = ''
|
||||
showTracks(pagePrev !== 0 ? pagePrev : 1)
|
||||
})
|
||||
|
||||
document.getElementById('player-next').addEventListener('click', playNext, false)
|
||||
|
||||
document.getElementById('player-prev').addEventListener('click', function (e) {
|
||||
|
@ -2,7 +2,6 @@ import path from 'path'
|
||||
import sqlite from 'sqlite'
|
||||
import Promise from 'bluebird'
|
||||
import express from 'express'
|
||||
import sockets from './server/sockets'
|
||||
|
||||
require('express-async-errors')
|
||||
|
||||
@ -18,9 +17,6 @@ const port = process.env.PORT || 3000
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
const http = require('http').Server(app)
|
||||
const io = require('socket.io')(http)
|
||||
|
||||
router.get('/tracks', async (req, res) => {
|
||||
let page = parseInt(req.query.page) || 1
|
||||
if (isNaN(page)) {
|
||||
@ -156,7 +152,8 @@ app.use('/api', router)
|
||||
app.use('/file/track', express.static(path.resolve(values.directory)))
|
||||
app.use('/', express.static(path.join(process.cwd(), 'public')))
|
||||
|
||||
http.listen(port, '127.0.0.1', function () {
|
||||
sockets.start(io)
|
||||
const host = process.env.NODE_ENV === 'development' ? '0.0.0.0' : '127.0.0.1'
|
||||
|
||||
app.listen(port, host, function () {
|
||||
console.log(`app running on port ${port}`)
|
||||
})
|
||||
|
@ -1,141 +0,0 @@
|
||||
|
||||
let data = {
|
||||
clients: {},
|
||||
|
||||
// Key: Controller
|
||||
// Value: Controlee
|
||||
// Music is played on Value's device.
|
||||
control: {}
|
||||
}
|
||||
|
||||
function getSocketByDevice (devid) {
|
||||
for (let id in data.clients) {
|
||||
let sock = data.clients[i]
|
||||
if (sock.device && sock.device === devid) {
|
||||
return sock
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
function getControlling (sockid) {
|
||||
for (let id in data.control) {
|
||||
let cl = data.control[id]
|
||||
if (cl === sockid) return id
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
function start (io) {
|
||||
io.on('connection', function (socket) {
|
||||
let sockid = socket.id
|
||||
|
||||
socket.on('auth', function (device) {
|
||||
if (getSocketByDevice(device) != null) {
|
||||
return socket.emit('cerr', {error: 'devexists', code: 0})
|
||||
}
|
||||
|
||||
socket.device = device
|
||||
data.clients[sockid] = socket
|
||||
})
|
||||
|
||||
socket.on('req:devs', function () {
|
||||
let devices = []
|
||||
|
||||
for (let i in data.clients) {
|
||||
let cl = data.clients[i]
|
||||
if (cl.device === socket.device) continue
|
||||
devices.push(devices)
|
||||
}
|
||||
|
||||
socket.emit('res:devs', devices)
|
||||
})
|
||||
|
||||
// Controller wants to take control of Controlee
|
||||
socket.on('ctrl:connect', function (ctrlee) {
|
||||
let dev = getSocketByDevice(ctrlee)
|
||||
if (!dev) return socket.emit('cerr', {error: 'devnull', code: 1})
|
||||
|
||||
// Device is already controlling/being controlled
|
||||
if (data.control[sockid] || getControlling(dev.id)) return socket.emit('cerr', {error: 'devctrl', code: 2})
|
||||
|
||||
dev.emit('ctrl:connect', { device: socket.device })
|
||||
socket.emit('targ:success', { device: ctrlee })
|
||||
})
|
||||
|
||||
// Disconnect
|
||||
socket.on('ctrl:disconnect', function (ctrlee) {
|
||||
let id, ctrl
|
||||
if (getControlling(sockid)) {
|
||||
// If this is a controlee, disconnect from controller
|
||||
id = getControlling(sockid)
|
||||
ctrl = data.clients[id]
|
||||
|
||||
delete data.control[id]
|
||||
} else if (data.control[sockid]) {
|
||||
// If this is a controller, disconnect from controlee
|
||||
id = data.control[sockid]
|
||||
ctrl = data.clients[id]
|
||||
|
||||
delete data.control[sockid]
|
||||
}
|
||||
|
||||
if (!ctrl) return
|
||||
|
||||
ctrl.emit('targ:disconnect', {})
|
||||
})
|
||||
|
||||
let directpass = ['track', 'pause', 'seek', 'volume', 'mute', 'queue', 'req:queue', 'res:queue']
|
||||
for (let i in directpass) {
|
||||
let syn = 'sync:' + directpass[i]
|
||||
|
||||
// Set pause/play state
|
||||
// Sent from controlee to controller
|
||||
socket.on(syn, function (data) {
|
||||
let ctrl
|
||||
|
||||
if (getControlling(sockid)) {
|
||||
// If this is a controlee, send to controller
|
||||
ctrl = data.clients[getControlling(sockid)]
|
||||
} else if (data.control[sockid]) {
|
||||
// If this is a controller, send to controlee
|
||||
ctrl = data.clients[data.control[sockid]]
|
||||
}
|
||||
|
||||
ctrl && ctrl.emit('set:' + directpass[i], data)
|
||||
})
|
||||
}
|
||||
|
||||
// Synchronize timestamp
|
||||
socket.on('sync:time', function (data) {
|
||||
if (!getControlling(sockid)) return
|
||||
let ctrl = data.clients[getControlling(sockid)]
|
||||
ctrl && ctrl.emit('sync:time', data)
|
||||
})
|
||||
|
||||
socket.on('disconnect', function () {
|
||||
if (data.clients[sockid]) {
|
||||
let dev = data.clients[sockid]
|
||||
|
||||
// Controller loses connection
|
||||
// Emit to controlee
|
||||
if (data.control[sockid]) {
|
||||
let clt = data.clients[sockid]
|
||||
clt && clt.emit('ctrl:disconnect', {})
|
||||
|
||||
// Controlee loses connection
|
||||
// Emit to controller
|
||||
} else if (getControlling(sockid)) {
|
||||
let clt = data.clients[getControlling(sockid)]
|
||||
clt && clt.emit('targ:disconnect', {})
|
||||
}
|
||||
|
||||
delete data.clients[sockid]
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = { start }
|
Loading…
Reference in New Issue
Block a user