Send state updates through socket, finish app
This commit is contained in:
@@ -1,10 +1,21 @@
|
||||
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('/hello', function (req, res) {
|
||||
res.send('Hello World!')
|
||||
app.get('/api/dout/:pin', function (req, res) {
|
||||
res.send(ioState.dout[req.params.pin])
|
||||
})
|
||||
|
||||
const server = app.listen(3000, function () {
|
||||
@@ -13,6 +24,11 @@ const server = app.listen(3000, function () {
|
||||
|
||||
const io = require('socket.io').listen(server);
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
socket.emit('message', 'hello socket');
|
||||
io.on('connection', socket => {
|
||||
socket.emit('ioState', ioState);
|
||||
socket.on('update', data => {
|
||||
console.log(data);
|
||||
updateState(data);
|
||||
io.sockets.emit('ioState', ioState);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user