You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
756 B
34 lines
756 B
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); |
|
}); |
|
});
|
|
|