lint topos
This commit is contained in:
@ -2,13 +2,13 @@ const WebSocket = require("ws");
|
||||
const osc = require("osc");
|
||||
|
||||
const cleanIncomingOSC = (oscMsg) => {
|
||||
let data = oscMsg.args;
|
||||
// Remove information about type of data
|
||||
data = data.map((item) => {
|
||||
return item.value;
|
||||
})
|
||||
return {data: data, address: oscMsg.address};
|
||||
}
|
||||
let data = oscMsg.args;
|
||||
// Remove information about type of data
|
||||
data = data.map((item) => {
|
||||
return item.value;
|
||||
});
|
||||
return { data: data, address: oscMsg.address };
|
||||
};
|
||||
|
||||
// ==============================================
|
||||
// Receiving and forwarding OSC UDP messages
|
||||
@ -16,18 +16,21 @@ const cleanIncomingOSC = (oscMsg) => {
|
||||
console.log("> OSC Input: 127.0.0.1:30000");
|
||||
const wss = new WebSocket.Server({ port: 3001 });
|
||||
var udpPort = new osc.UDPPort({
|
||||
localAddress: "0.0.0.0",
|
||||
localPort: 30000,
|
||||
metadata: true
|
||||
localAddress: "0.0.0.0",
|
||||
localPort: 30000,
|
||||
metadata: true,
|
||||
});
|
||||
udpPort.on("message", function (oscMsg, timeTag, info) {
|
||||
console.log(`> Incoming OSC to ${oscMsg.address}:`, oscMsg.args.map(
|
||||
(item) => {return item.value})
|
||||
);
|
||||
wss.clients.forEach(client => {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(JSON.stringify(cleanIncomingOSC(oscMsg)));
|
||||
}
|
||||
});
|
||||
console.log(
|
||||
`> Incoming OSC to ${oscMsg.address}:`,
|
||||
oscMsg.args.map((item) => {
|
||||
return item.value;
|
||||
}),
|
||||
);
|
||||
wss.clients.forEach((client) => {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(JSON.stringify(cleanIncomingOSC(oscMsg)));
|
||||
}
|
||||
});
|
||||
});
|
||||
udpPort.open();
|
||||
udpPort.open();
|
||||
|
||||
@ -12,10 +12,14 @@ wss.on("connection", function (ws) {
|
||||
const message = JSON.parse(data);
|
||||
sendOscMessage(
|
||||
formatAndTypeMessage(message),
|
||||
message.address,
|
||||
message.port
|
||||
message.address,
|
||||
message.port,
|
||||
);
|
||||
console.log(
|
||||
`> Message sent to ${message.address}:${message.port}: ${JSON.stringify(
|
||||
message.args,
|
||||
)}`,
|
||||
);
|
||||
console.log(`> Message sent to ${message.address}:${message.port}: ${JSON.stringify(message.args)}`)
|
||||
} catch (error) {
|
||||
console.error("> Error processing message:", error);
|
||||
}
|
||||
@ -24,12 +28,12 @@ wss.on("connection", function (ws) {
|
||||
|
||||
wss.on("error", function (error) {
|
||||
console.error("> Server error:", error);
|
||||
})
|
||||
});
|
||||
|
||||
wss.on("close", function () {
|
||||
// Close the websocket server
|
||||
wss.close();
|
||||
console.log("> Closing websocket server")
|
||||
console.log("> Closing websocket server");
|
||||
});
|
||||
|
||||
let udpPort = new osc.UDPPort({
|
||||
@ -37,7 +41,7 @@ let udpPort = new osc.UDPPort({
|
||||
localPort: 3000,
|
||||
metadata: true,
|
||||
remoteAddress: "0.0.0.0",
|
||||
remotePort: 57120,
|
||||
remotePort: 57120,
|
||||
});
|
||||
udpPort.on("error", function (error) {
|
||||
console.error("> UDP Port error:", error);
|
||||
@ -51,7 +55,7 @@ udpPort.open();
|
||||
|
||||
function sendOscMessage(message, address, port) {
|
||||
try {
|
||||
udpPort.options.remotePort = port
|
||||
udpPort.options.remotePort = port;
|
||||
message.address = address;
|
||||
udpPort.send(message);
|
||||
} catch (error) {
|
||||
@ -61,21 +65,19 @@ function sendOscMessage(message, address, port) {
|
||||
|
||||
const formatAndTypeMessage = (message) => {
|
||||
let newMessage = {};
|
||||
delete message.args['address'];
|
||||
delete message.args['port'];
|
||||
delete message.args["address"];
|
||||
delete message.args["port"];
|
||||
newMessage.address = message.address;
|
||||
newMessage.timestamp = osc.timeTag(message.timetag);
|
||||
|
||||
args = [...Object.entries(message.args)].flat().map((arg) => {
|
||||
if (typeof arg === 'string')
|
||||
return {type: 's', value: arg};
|
||||
if (typeof arg === 'number')
|
||||
return {type: 'f', value: arg};
|
||||
if (typeof arg === 'boolean')
|
||||
return value ? {type: 's', value: 1} : {type: 's', value: 0};
|
||||
})
|
||||
if (typeof arg === "string") return { type: "s", value: arg };
|
||||
if (typeof arg === "number") return { type: "f", value: arg };
|
||||
if (typeof arg === "boolean")
|
||||
return value ? { type: "s", value: 1 } : { type: "s", value: 0 };
|
||||
});
|
||||
|
||||
newMessage.args = args
|
||||
newMessage.args = args;
|
||||
|
||||
return newMessage;
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
var pjson = require('./package.json');
|
||||
var pjson = require("./package.json");
|
||||
let banner = `
|
||||
┏┳┓ ┏┓┏┓┏┓
|
||||
┃ ┏┓┏┓┏┓┏ ┃┃┗┓┃
|
||||
┻ ┗┛┣┛┗┛┛ ┗┛┗┛┗┛
|
||||
┛
|
||||
${pjson.version}\n`
|
||||
${pjson.version}\n`;
|
||||
function greet() {
|
||||
console.log(banner)
|
||||
console.log(banner);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
greet: greet
|
||||
}
|
||||
greet: greet,
|
||||
};
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
const WebSocket = require("ws");
|
||||
const osc = require("osc");
|
||||
|
||||
require('./banner').greet();
|
||||
require("./banner").greet();
|
||||
// Topos to OSC
|
||||
require('./ToposToOSC')
|
||||
require("./ToposToOSC");
|
||||
// OSC to Topos
|
||||
require("./OSCtoTopos")
|
||||
|
||||
require("./OSCtoTopos");
|
||||
|
||||
Reference in New Issue
Block a user