V
V
Vladislav2020-12-14 20:25:06
Node.js
Vladislav, 2020-12-14 20:25:06

BinaryData Buffer how to assemble into one?

Please tell me how to correctly assemble the final file that comes from the socket in chunks (pieces).

connection.on('message', (message)=>{
            // console.log(message);
            if (message.type === 'binary'){
                // console.log(message.binaryData)
                // binaryAudio = Buffer.concat(binaryAudio, message.binaryData)
                // binaryAudio = binaryAudio .concat(binaryAudio, message.binaryData)
                // binaryAudio.push(message.binaryData)
                //binaryAudio += message.binaryData.toString()
                // binaryAudio.append(message.binaryData.toString())
            }
            if (message.type === 'utf8' && JSON.parse(message.utf8Data).streamcontrol){
                // console.log(binaryAudio.length)
                // binaryAudio = [];
                // res.end('');
                res.statusCode = 200;
                res.setHeader('content-type', 'audio/opus');
                res.setHeader('Content-Disposition', 'attachment; filename=file.ogg'); 
                res.setHeader("Content-Length", binaryAudio.toString().length);
                // res.end(binaryAudio);
                // res.end(binaryAudio.toString());
            }
        })


In general, the code above is all my attempts to put together.
But all this was not crowned with success.
The file does not open properly.

In what an essence there is a global variable binaryAudio
And when in sockets the binary type comes it is necessary to add the data to this variable.

At the end, when he sends all the binaries, he will send json in which there is a streamcontrol property and only in this case you need to save the resulting file.

The problem is in the correct assembly of the file. Because casting to string and concatenation doesn't work that easy.

If we compare the binary through the text editor, they differ in indentation and line breaks. They are not identical outwardly, they start the same way, and then there is a slightly different arrangement (visually)

How to assemble correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2020-12-15
@cr1gger

I found this solution:
When the binary arrives, we do:
binaryAudio.push(message.binaryData)
when we need to collect, we do:
let result = Buffer.concat(binaryAudio)
And we already give the result for loading.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question