NodeJS Websocket Binary

penta

Участник
Автор темы
41
8
Отправляю аудиопоток на сервер, когда уже доходит выбивает ошибка ниже. Если нужен код по С++ клиенту -- напишите



JavaScript:
const WebSocket = require('ws');
const fs = require('fs');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
  console.log('Client connected');
  ws.on('message', function incoming(data) {
    try {
      fs.writeFile('audio_received.wav', data, function(err) {
        if (err) {
          console.error('Error saving audio file:', err);
        } else {
          console.log('Audio file saved successfully');
        }
      }); bina
    } catch(error) {
      console.log(error);
    }
  });
  ws.on('close', function close() {
    console.log('Client disconnected');
  });
});
Error: Invalid WebSocket frame: invalid UTF-8 sequence
at Receiver.dataMessage (C:\Users\Vladislav\Desktop\nodeJs\node_modules\ws\lib\receiver.js:590:28)
at Receiver.getData (C:\Users\Vladislav\Desktop\nodeJs\node_modules\ws\lib\receiver.js:500:10)
at Receiver.startLoop (C:\Users\Vladislav\Desktop\nodeJs\node_modules\ws\lib\receiver.js:171:16)
at Receiver._write (C:\Users\Vladislav\Desktop\nodeJs\node_modules\ws\lib\receiver.js:98:10)
at writeOrBuffer (node:internal/streams/writable:447:12)
at _write (node:internal/streams/writable:389:10)
at Writable.write (node:internal/streams/writable:393:10)
at Socket.socketOnData (C:\Users\Vladislav\Desktop\nodeJs\node_modules\ws\lib\websocket.js:1303:35)
at Socket.emit (node:events:514:28)
at addChunk (node:internal/streams/readable:376:12)
Emitted 'error' event on WebSocket instance at:
at Receiver.receiverOnError (C:\Users\Vladislav\Desktop\nodeJs\node_modules\ws\lib\websocket.js:1189:13)
at Receiver.emit (node:events:514:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
code: 'WS_ERR_INVALID_UTF8',
[Symbol(status-code)]: 1007
}
 

.caterpillarteal$

Участник
22
6
JavaScript:
const WebSocket = require('ws');
const fs = require('fs');
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
  console.log('Client connected');

  ws.on('message', function incoming(data) {
    try {
      fs.writeFile('audio_received.wav', data, { encoding: 'binary' }, function(err) {
        if (err) {
          console.error('Error saving audio file:', err);
        } else {
          console.log('Audio file saved successfully');
        }
      });
    } catch(error) {
      console.log(error);
    }
  });

  ws.on('close', function close() {
    console.log('Client disconnected');
  });
});
 

penta

Участник
Автор темы
41
8
JavaScript:
const WebSocket = require('ws');
const fs = require('fs');
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
  console.log('Client connected');

  ws.on('message', function incoming(data) {
    try {
      fs.writeFile('audio_received.wav', data, { encoding: 'binary' }, function(err) {
        if (err) {
          console.error('Error saving audio file:', err);
        } else {
          console.log('Audio file saved successfully');
        }
      });
    } catch(error) {
      console.log(error);
    }
  });

  ws.on('close', function close() {
    console.log('Client disconnected');
  });
});
Не поможет, у WebSocket есть опция: если не UTF8 то кинуть ошибку. Решил это путем внедрения в библиотеки.