简体   繁体   中英

How to send ArrayBuffer in json from js client to nodejs server?

I am trying to send data of type ArrayBuffer in json to my server using socket.io like this:

socket.emit('record', {
       name: myUsername + '.wav',
       data: data //arraybuffer
 });

On server side, when i receive 'record' event in socket, I get the data from JSON and save it in name file like this:

socket.on('record', function(message){
      var fileWriter = new wav.FileWriter(message.name, {
        channels: 1,
        sampleRate: 48000,
        bitDepth: 16
        });
      message.data.pipe(fileWriter);
    });

I am using require('wav') & require('stream') package from npm. The problem is that my server crashes on message.data.pipe(fileWriter); with this error:

TypeError: message.data.pipe is not a function

What am I doing wrong? Can't i send ArrayBuffer like this in socket.io?

data is an ArrayBuffer , not a ReadableStream . Enqueue data to be read by a ReadableStream , passed to a WritableStream or TransformStream , for example using .pipeThrough() , see Receiving data via stdin and storing as a variable/array .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM