3

我必须维护旧代码。基础:它应该处理传入的流(音频和/或视频)。第 3 方设备在请求时发送流。然后客户端应该使用它。

代码未完成,但对我来说,似乎不错。NodeJS 版本是最新的(atm):8.9.4

// init.js(主 js 文件)

global.readStream = new readStream();

读取流对象看起来像这个旧对象:// readStream 对象

var Readable = require('stream').Readable;
var util = require('util');

function myReadStream() {
  Readable.call(this);
  this.data = null;
}
util.inherits(myReadStream, Readable);

myReadStream.prototype.addData = function (newData) {
    this.data = newData;
    console.log('new data: ', this.data);
};

myReadStream.prototype._read = function () {
  this.push(this.data);
};

所以,两个端点:

app.get('incomingdata', function (req, res) {
     myReadStream.addData = res.newIncomingData; 
     // just writing the stream data directly
     console.log('incoming: ', res.newIncomingData);
});

app.get('outgoingData', function (req, res) {
     myReadStream
  .on('readable', function () {
    var obj;
    while (null !== (obj = myReadStream.read())) {
      myReadStream.pipe(res); // direct pipe to res
      // alternative idea: res.send(obj);
      // alternative #2: res.write(obj);
      console.log('outgoing: ', obj);
    }
  });
});

结果是,“outgoingData”的调用总是重复 readStream 数据的一个阶段(在传出数据调用之前最近提供的数据)但不刷新......

例如:

Incoming: <Buffer 00 e3 ff ab a2 ....>
new data: <Buffer 00 e3 ff ab a2 ....>
Incoming: <Buffer 01 3b ca c1 b0 ....>
new data: <Buffer 01 3b ca c1 b0 ....>
Outgoing: <Buffer 01 3b ca c1 b0 ....>
Incoming: <Buffer 99 fa e8 77 00 ....>
new data: <Buffer 99 fa e8 77 00 ....>
Outgoing: <Buffer 01 3b ca c1 b0 ....>
Incoming: <Buffer ef b0 00 22 33 ....>
new data: <Buffer ef b0 00 22 33 ....>
Outgoing: <Buffer 01 3b ca c1 b0 ....>
Incoming: <Buffer 25 7b 91 aa 00 ....>
new data: <Buffer 25 7b 91 aa 00 ....>
Outgoing: <Buffer 01 3b ca c1 b0 ....>

我只有 Socket.Io 的经验,在可读性和可写性方面并不多。据我所知,这段代码是在大约 1.5 年前为 4.x 版开发的。代码看起来不错,但遗漏了一些东西。知道出了什么问题,我该如何纠正?

(ps:我不喜欢维护遗留代码)

4

0 回答 0