node.js update

master
louisa 1 year ago
parent e0383376f6
commit 28dd8fc7d1

100
app.js

@ -1,69 +1,53 @@
var http = require('http'); const osc = require('osc-min');
var fs = require('fs'); const dgram = require('dgram');
var express = require('express');
var app = express(); const oscHost = '127.0.0.1'; // IP address of the OSC destination
var path = require('path'); const oscPort = 8080; // Port number of the OSC destination
var server = http.createServer(app);
var port = 8000; // Function to send the OSC message
const osc = require('osc'); function sendOSCMessage() {
// Create the OSC message
const { SerialPort } = require('serialport') const oscMessage = osc.toBuffer({
const { ReadlineParser } = require('@serialport/parser-readline') address: '/blink',
const sport = new SerialPort({ path: '/dev/ttyACM0', baudRate: 115200 }) args: [1]
});
// 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);
});
app.use(express.static(path.join(__dirname, "public"))); // Create a UDP socket
const udpSocket = dgram.createSocket('udp4');
// Start the HTTP server // Send the OSC message
const port = 3000; // Specify your desired HTTP port number udpSocket.send(oscMessage, 0, oscMessage.length, oscPort, oscHost, (err) => {
http.listen(port, () => { if (err) {
console.log(`Server is running on port ${port}`); 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) { const express = require('express');
console.log("A client connected!"); const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
parser.on('data', function(data) { // Handle socket.io events
const msg = data.split(' '); io.on('connection', (socket) => {
console.log(msg[0], msg[1]); console.log('A client connected!');
io.emit('node-data', data);
});
socket.on("blink", () => { socket.on('blink', () => {
console.log("Blink event received!"); console.log('Blink event received!');
io.emit("blink"); // Emit the "blink" event to all connected clients 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) => { // Add other event handlers here...
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);
});
const port = 3000; // Specify your desired port number
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
}); });

@ -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');
});

@ -6,7 +6,6 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="icon" href="img/favicon.ico" type="image/x-icon" /> <link rel="icon" href="img/favicon.ico" type="image/x-icon" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="../osc.js"></script>
<link href="style.css" rel="stylesheet"> <link href="style.css" rel="stylesheet">
<!-- <script src="js/p5.js"></script> <!-- <script src="js/p5.js"></script>
<script src="js/p5.geolocation.js"></script> --> <script src="js/p5.geolocation.js"></script> -->

Loading…
Cancel
Save