critical-leddemo/server.js

35 lines
756 B
JavaScript

const express = require('express')
const app = express()
var ioState = {
dout: [false],
}
const updateState = data => {
ioState[data.type] = ioState[data.type].map((x, i) =>
i == data.pin ? data.value : x
);
console.log(ioState);
}
app.use('/', express.static('dist'))
app.get('/api/dout/:pin', function (req, res) {
res.send(ioState.dout[req.params.pin])
})
const server = app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
const io = require('socket.io').listen(server);
io.on('connection', socket => {
socket.emit('ioState', ioState);
socket.on('update', data => {
console.log(data);
updateState(data);
io.sockets.emit('ioState', ioState);
});
});