From e247bfd187f2c50b873642afe0c748437be3f593 Mon Sep 17 00:00:00 2001 From: louisa Date: Wed, 14 Jun 2023 19:27:36 +0200 Subject: [PATCH] node.js restore --- app.js | 53 +++++++++++++++++++++++++++++++---------------- public/index.html | 14 ++++++++++++- 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/app.js b/app.js index 589f34d..58617b6 100644 --- a/app.js +++ b/app.js @@ -3,32 +3,49 @@ 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"))); + + var io = require('socket.io')(server); -io.on('connection', function(socket) { - console.log('Node.js is listening!!'); +io.on('connection', function(socket){ - socket.on("hello", (arg, callback) => { + 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) => { console.log("hellohelo"); // "world" - io.emit('blink'); - }); + io.emit('world'); - socket.on('chat message', (msg) => { - console.log('[user][' + socket.id + '][' + msg + ']'); - }); + }); - socket.on('userposition', (msg) => { - console.log('[user][' + socket.id + '][position: ' + msg[0] + ',' + msg[1] + ']'); - socket.to('expo').emit(socket.id, msg); - }); -}); + socket.on('chat message', (msg) => { + console.log('[user]['+ socket.id + '][' + msg + ']'); + }); -server.listen(port, () => { - console.log("Server is listening at port %d", port); -}); + socket.on('userposition', (msg) => { + console.log('[user]['+ socket.id + '][position: ' + msg[0] + ',' + msg[1]+ ']'); + socket.to('expo').emit(socket.id, msg); + }); -app.use(express.static(path.join(__dirname, "public"))); + + +}); diff --git a/public/index.html b/public/index.html index d5fc643..a887f10 100644 --- a/public/index.html +++ b/public/index.html @@ -32,6 +32,18 @@ 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 = ''; + } + @@ -46,7 +58,7 @@

>>> CACHE CODE:

- +