简体   繁体   中英

Recording binary stream to WAV file over WebSocket with SSL

I have a NodeJS server which records binary data (as WAV files) streamed to it over WebSocket from a client browser (Chrome) with a microphone. This is working correctly over a non-SSL connection (ws://...), but not over SSL (wss://...) - the audio is completely static.

I'm wondering if the SSL encryption is messing with the binary data as it is received at the server.

Server

var WebSocketServer = require('ws').Server,
  wav = require('wav'),
  http = require('https');

var app = http.createServer({
  key: fs.readFileSync('test.key'),
  cert: fs.readFileSync('test.crt')
}).listen(9001);

var ws = new WebSocketServer({server: app});
ws.on('connection', function(client) {
  var fileWriter = new wav.FileWriter(myFilePath, {
    channels: 1,
    sampleRate: 48000,
    bitDepth: 16
  });

  var data = new Buffer(0);

  client.on('message', function(newData) {
    data = Buffer.concat([data, newData], data.length + newData.length);
  });

  client.on('close', function() {
    fileWriter.write(data);
    fileWriter.end();
  });
});

Client (Note, the client is using RecordRTC.js: http://recordrtc.org/RecordRTC.html , these are the main parts that send data to the server)

var audioProcess = function(e) {
  // get binary audio data and send to server using the open stream
  var left = e.inputBuffer.getChannelData(0);
  stream.write(convertoFloat32ToInt16(left));
};

var convertoFloat32ToInt16 = function(buffer) {
  var l = buffer.length;
  var buf = new Int16Array(l);

  while (l--) {
    buf[l] = buffer[l]*0xFFFF;    //convert to 16 bit
  }
  return buf.buffer
};

Thanks in advance for any insight/solutions!

I had the same problem with web sockets, in the end I used BinaryJS. Create a https server and then attach a binary server to it. Works fine for me.

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