osc.js
osc.js copied to clipboard
bug: Data corruption when sending binary
Hi, I was sending icons over websocket OSC and some of them got corrupted. It seemed like larger ones were fine but smaller ones have trouble.
I've made a minimal example that shows the problem clearly:
const http = require('node:http');
const WebSocket = require("ws");
const osc = require('osc');
// The data we want to send
const bytes = new Uint8Array([ 4,4,4,4,4,4 ]);
// server
const hostname = '127.0.0.1';
const port = 7000;
const server = http.createServer(async (req, res) => {});
server.listen(port, hostname, () => {
// console.log(`Server running at http://${hostname}:${port}/`);
});
const wss = new WebSocket.Server({ server });
wss.on("connection", function(socket) {
const osc_port = new osc.WebSocketPort({
socket: socket,
metadata: true
});
osc_port.on("message", (osc_msg) => {
// console.log('rx osc_msg=', osc_msg)
console.log("rx osc_msg.args[0].value=", osc_msg.args[0].value)
console.log();
process.exit();
});
});
// client
const ui_url = `ws://${hostname}:${port}/`;
ui_port = new osc.WebSocketPort({
// url: ui_url,
metadata: true,
socket: new WebSocket(ui_url),
});
ui_port.on("ready", function () {
const osc_msg = {
address: '/foo',
args: [
{ type: 'b', value: bytes }
]
};
// console.log('tx osc_msg=', osc_msg)
console.log('tx osc_msg.args[0].value=', osc_msg.args[0].value)
ui_port.send(osc_msg);
});
want:
tx osc_msg.args[0].value= Uint8Array(6) [ 4, 4, 4, 4, 4, 4 ]
rx osc_msg.args[0].value= Uint8Array(6) [ 4, 4, 4, 4, 4, 4 ]
got:
tx osc_msg.args[0].value= Uint8Array(6) [ 4, 4, 4, 4, 4, 4 ]
rx osc_msg.args[0].value= Uint8Array(6) [ 0, 0, 0, 0, 0, 6 ]