guest_worker/scanimation/LocalServer.js

71 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-12-18 10:48:23 +01:00
const express = require('express'),
app = express(),
server = require('http').Server(app),
io = require('socket.io')(server),
fs = require('fs'),
watch = require('node-watch')
let LocalServer = (config) => {
let ls = {
express: express,
app: app,
server: server,
io: io
}
app.use(express.static(config.frontend_folder))
io.on('connection', function(socket){
console.log('client connected')
socket.emit('connection')
// FRAME ANIMATION /////////////////////////////////////////////////////////////
socket.on('animationInit', function(){
fs.readdir(config.frames_folder, (err, files) => {
2020-01-23 13:24:53 +01:00
files = files.filter(file => file.endsWith('.jpg'))
2019-12-18 10:48:23 +01:00
socket.emit('frameData', {frames: files})
console.log('starting animation')
})
})
watch(config.frames_folder, function(e, name){
fs.readdir(config.frames_folder, (err, files) => {
2020-01-23 13:24:53 +01:00
files = files.filter(file => file.endsWith('.jpg'))
2019-12-18 10:48:23 +01:00
socket.emit('frameData', {frames: files})
console.log(`frames changed => ${name}`)
})
})
////////////////////////////////////////////////////////////////////////////////
})
ls.start = () => {
server.listen(config.port, function(){
console.log(`node server listening on port ${config.port}`)
})
}
return ls
}
module.exports = LocalServer