node.js restore

master
louisa 1 year ago
parent de78495ac1
commit e247bfd187

@ -3,18 +3,38 @@ var fs = require('fs');
var express = require('express'); var express = require('express');
var app = express(); var app = express();
var path = require('path'); var path = require('path');
var server = http.createServer(app); var server = http.createServer(app);
var port = 8000; var port = 8000;
const { SerialPort } = require('serialport')
const { ReadlineParser } = require('@serialport/parser-readline')
const sport = new SerialPort({ path: '/dev/ttyACM0', baudRate: 115200 })
const parser = sport.pipe(new ReadlineParser({ delimiter: '\r\n' }))
parser.on('data', console.log)
server.listen(port, () => {
console.log("Server is listening at port %d", port);
});
app.use(express.static(path.join(__dirname, "public")));
var io = require('socket.io')(server); var io = require('socket.io')(server);
io.on('connection', function(socket){ io.on('connection', function(socket){
console.log('Node.js is listening!!');
parser.on('data', function(data) {
const msg = data.split(' ');
console.log(msg[0], msg[1]);
io.emit(msg[0], msg[1]);
});
console.log('Node.js is listening!!');
socket.on("hello", (arg, callback) => { socket.on("hello", (arg, callback) => {
console.log("hellohelo"); // "world" console.log("hellohelo"); // "world"
io.emit('blink'); io.emit('world');
}); });
socket.on('chat message', (msg) => { socket.on('chat message', (msg) => {
@ -25,10 +45,7 @@ io.on('connection', function(socket) {
console.log('[user]['+ socket.id + '][position: ' + msg[0] + ',' + msg[1]+ ']'); console.log('[user]['+ socket.id + '][position: ' + msg[0] + ',' + msg[1]+ ']');
socket.to('expo').emit(socket.id, msg); socket.to('expo').emit(socket.id, msg);
}); });
});
server.listen(port, () => {
console.log("Server is listening at port %d", port);
});
app.use(express.static(path.join(__dirname, "public")));
});

@ -32,6 +32,18 @@
document.body.style.backgroundColor = 'white'; document.body.style.backgroundColor = 'white';
} }
}); });
function sendMessage() {
const inputField = document.getElementById('input');
const message = inputField.value;
if (message === 'blink') {
socket.emit('blink', message);
}
inputField.value = '';
}
</script> </script>
<script src='socket.io/socket.io.js'></script> <script src='socket.io/socket.io.js'></script>
<meta property="og:title" content="868 MHz" /> <meta property="og:title" content="868 MHz" />
@ -46,7 +58,7 @@
<!-- <progress id="file" max="100" value="70"> 70% </progress> --> <!-- <progress id="file" max="100" value="70"> 70% </progress> -->
<p>>>> CACHE CODE:</p> <p>>>> CACHE CODE:</p>
<span><form id="form" action=""> <span><form id="form" action="">
<input id="input" autocomplete="off" onkeyup="this.value = this.value.toUpperCase();"/><button>>>></button> <input id="input" autocomplete="off" onkeyup="this.value = this.value.toUpperCase();"/><button onclick="sendMessage()">>>></button>
</form></span> </form></span>

Loading…
Cancel
Save