You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.5 KiB
JavaScript

2 years ago
var http = require('http');
var fs = require('fs');
var express = require('express');
var app = express();
var path = require('path');
var server = http.createServer(app);
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")));
1 year ago
io.emit("blink");
1 year ago
var io = require('socket.io')(server);
1 year ago
//here we are listening to the data coming in from the modules, it's called node-data in the html
io.on('connection', function(socket){
1 year ago
console.log("A client connected!");
1 year ago
1 year ago
parser.on('data', function(data) {
const msg = data.split(' ');
console.log(msg[0], msg[1]);
io.emit('node-data', data);
});
1 year ago
socket.on("message", (message) => {
console.log("Received message:", message);
if (message === "blink") {
io.emit("blink"); // Trigger the blink event to all connected clients
}
});
2 years ago
socket.on('chat message', (msg) => {
console.log('[user]['+ socket.id + '][' + msg + ']');
});
2 years ago
socket.on('userposition', (msg) => {
console.log('[user]['+ socket.id + '][position: ' + msg[0] + ',' + msg[1]+ ']');
socket.to('expo').emit(socket.id, msg);
});
2 years ago
});