From 28dd8fc7d18f07a28241dbec939bcd6ec1e829ef Mon Sep 17 00:00:00 2001 From: louisa Date: Thu, 15 Jun 2023 15:45:00 +0200 Subject: [PATCH] node.js update --- app.js | 100 +++++++++++++++++++--------------------------- osc.js | 37 ----------------- public/index.html | 1 - 3 files changed, 42 insertions(+), 96 deletions(-) delete mode 100644 osc.js diff --git a/app.js b/app.js index d295f55..0311899 100644 --- a/app.js +++ b/app.js @@ -1,69 +1,53 @@ -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 osc = require('osc'); - -const { SerialPort } = require('serialport') -const { ReadlineParser } = require('@serialport/parser-readline') -const sport = new SerialPort({ path: '/dev/ttyACM0', baudRate: 115200 }) - - -// Create an OSC server and specify the port -const oscPort = new osc.UDPPort({ - localAddress: '0.0.0.0', // Listen on all network interfaces - localPort: 8080 // Specify your desired OSC port number -}); - -// Start the OSC server -oscPort.open(); - -// Handle incoming OSC messages -oscPort.on('message', (oscMessage) => { - console.log('Received OSC message:', oscMessage); - // Handle the OSC message here and trigger the desired actions -}); - -const parser = sport.pipe(new ReadlineParser({ delimiter: '\r\n' })) - -server.listen(port, () => { - console.log("Server is listening at port %d", port); -}); +const osc = require('osc-min'); +const dgram = require('dgram'); + +const oscHost = '127.0.0.1'; // IP address of the OSC destination +const oscPort = 8080; // Port number of the OSC destination + +// Function to send the OSC message +function sendOSCMessage() { + // Create the OSC message + const oscMessage = osc.toBuffer({ + address: '/blink', + args: [1] + }); -app.use(express.static(path.join(__dirname, "public"))); + // Create a UDP socket + const udpSocket = dgram.createSocket('udp4'); -// Start the HTTP server -const port = 3000; // Specify your desired HTTP port number -http.listen(port, () => { - console.log(`Server is running on port ${port}`); -}); + // Send the OSC message + udpSocket.send(oscMessage, 0, oscMessage.length, oscPort, oscHost, (err) => { + if (err) { + console.error('Error sending OSC message:', err); + } else { + console.log('OSC message sent successfully'); + } -var io = require('socket.io')(server); + // Close the UDP socket + udpSocket.close(); + }); +} -io.on('connection', function(socket) { - console.log("A client connected!"); +const express = require('express'); +const app = express(); +const server = require('http').createServer(app); +const io = require('socket.io')(server); - parser.on('data', function(data) { - const msg = data.split(' '); - console.log(msg[0], msg[1]); - io.emit('node-data', data); - }); +// Handle socket.io events +io.on('connection', (socket) => { + console.log('A client connected!'); - socket.on("blink", () => { - console.log("Blink event received!"); - io.emit("blink"); // Emit the "blink" event to all connected clients + socket.on('blink', () => { + console.log('Blink event received!'); + sendOSCMessage(); // Send the OSC message when the 'blink' event is received + io.emit('blink'); // Emit the 'blink' event to all connected clients }); - socket.on('chat message', (msg) => { - console.log('[user][' + socket.id + '][' + msg + ']'); - }); + // Add other event handlers here... - socket.on('userposition', (msg) => { - console.log('[user][' + socket.id + '][position: ' + msg[0] + ',' + msg[1] + ']'); - socket.to('expo').emit(socket.id, msg); - }); +}); +const port = 3000; // Specify your desired port number +server.listen(port, () => { + console.log(`Server is running on port ${port}`); }); diff --git a/osc.js b/osc.js deleted file mode 100644 index f405397..0000000 --- a/osc.js +++ /dev/null @@ -1,37 +0,0 @@ -const express = require('express'); -const osc = require('osc-min'); -const dgram = require('dgram'); - -const app = express(); -const oscHost = '192.168.2.1'; // IP address of the OSC destination -const oscPort = 8000; // Port number of the OSC destination - -app.get('/trigger-blink', (req, res) => { - // Create the OSC message - const oscMessage = osc.toBuffer({ - address: '/blink', - args: [1] - }); - - // Create a UDP socket - const udpSocket = dgram.createSocket('udp4'); - - // Send the OSC message - udpSocket.send(oscMessage, 0, oscMessage.length, oscPort, oscHost, (err) => { - if (err) { - console.error('Error sending OSC message:', err); - res.status(500).send('Error sending OSC message'); - } else { - console.log('OSC message sent successfully'); - res.send('OSC message sent successfully'); - } - - // Close the UDP socket - udpSocket.close(); - }); -}); - -// Start the server -app.listen(8000, () => { - console.log('Server is running on port 8000'); -}); diff --git a/public/index.html b/public/index.html index c6b5a2e..7c4eb7f 100644 --- a/public/index.html +++ b/public/index.html @@ -6,7 +6,6 @@ -