Search code examples
javascriptnode.jssocketsudposc

Retrieving FLOAT, DOUBLE, INT or BOOL values from Buffer


Good afternoon dear community,

I am working on a networking project where I receive data though UDP socket from a software called VDMX, and trying to parse the above-mentioned data types from the buffer.

In my node.js app, I receive values with the following socket client with no issue:

const dgram = require('dgram');
const server = dgram.createSocket('udp4');

server.on('error', (err) => {
  console.log(`server error:\n${err.stack}`);
  server.close();
});

server.on('message', (msg, rinfo) => {
  console.log(msg.toString());
});

server.on('listening', () => {
  const address = server.address();
  console.log(`server listening ${address.address}:${address.port}`);
});

server.bind(1235);

However the data retrieved from the software, I am not sure how to parse the necessary part from the data sent as string/buffer.

In every data sent, there is a prefix information of OSC info in the data sent like '/lux' by default. When I chose different data types, here is what I receive in my Node.js console:

If I choose float:

/lux,fB�F�
/lux,fB��
/lux,fB�ɘ
/lux,fB��

If I choose double:

/lux,d@L ��ϑL
/lux,d@L��S�
�|�x,d@K�f
/lux,d@K�Y�)�

If I choose integer 64-bit:

/lux,h1
/lux,h0
/lux,h/
/lux,h.

I would highly appreciate if you could guide me how to get the values from these buffers. Thank you very much in advance!


Solution

  • use msg.readDoubleLE(offset), msg.readFloat32LE(offset) and similar functions

    https://nodejs.org/dist/latest-v10.x/docs/api/buffer.html#buffer_buf_readdoublele_offset