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.

102 lines
2.5 KiB
JavaScript

const http = require('http');
const fs = require('fs');
const express = require('express');
const app = express();
const path = require('path');
const server = http.createServer(app);
const port = 8000;
const osc = require('osc');
const io = require('socket.io')(server);
const udpPort = new osc.UDPPort({
localAddress: "0.0.0.0",
localPort: 57121,
remoteAddress: "192.168.2.5",
remotePort: 8080,
metadata: true
});
// Open the socket.
udpPort.open();
server.listen(port, () => {
console.log("Server is listening at port %d", port);
});
app.use(express.static(path.join(__dirname, "public")));
io.on('connection', function(socket) {
console.log("new user online!");
socket.on('disconnect', () => {
console.log("User disconnected");
});
socket.on('userposition', (msg) => {
console.log('[user][' + socket.id + '][position: ' + msg[0] + ',' + msg[1] + ']');
// Assuming you want to emit to all connected clients except the sender
socket.broadcast.emit('userposition', msg);
});
socket.on('name', (msg) => {
console.log("[name][" + socket.id + "][" + msg + "]");
// You can handle name storage or broadcasting here
});
});
// Your POST endpoints
app.post('/hamp-off', (req, res) => {
console.log('off received');
var hampctrl = {
address: "/silentserver",
args: [{
type: "s",
value: "off"
}]
};
udpPort.send(hampctrl);
// Send a response back to the client
res.status(200).send("OSC message sent");
});
app.post('/hamp-on', (req, res) => {
console.log('on received');
var hampctrl = {
address: "/silentserver",
args: [{
type: "s",
value: "comp"
}]
};
udpPort.send(hampctrl);
// Send a response back to the client
res.status(200).send("OSC message sent");
});
// Assuming you want to handle parser here, uncomment if needed
// 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', function(data) {
// const msg = data.split(' ');
// console.log(msg[0], msg[1]);
// io.emit('node-data', data);
// });
// Assuming you want to handle OSC message here, uncomment if needed
// udpPort.on("message", function (oscMsg, timeTag, info) {
// io.emit('osc-data', oscMsg);
// });
// Handle disconnection event if needed
// io.on('disconnect', () => {
// console.log("User disconnected");
// });