我正在尝试限制HTTP请求使用的带宽(下载/上传速度)。我正在使用NPM包stream-throttle。我创建了一个自定义HTTP代理来通过Throttle示例传输套接字,并对5 MB文件的下载速度进行计时。
const http = require("http");
const net = require("net");
const {Throttle, ThrottleGroup} = require("stream-throttle");
const maxBandwidth = 100;
// an example 5MB file of random data
const str = "http://212.183.159.230/5MB.zip";
// this pipes an instance of Throttle
class SlowAgent extends http.Agent {
createConnection(options, callback){
const socket = new net.Socket(options);
socket.pipe(new Throttle({rate: maxBandwidth}));
socket.connect(options);
return socket;
}
}
const options = {
// this should slow down the request
agent: new SlowAgent()
};
const time = Date.now();
const req = http.request(str, options, (res) => {
res.on("data", () => {
});
res.on('end', () => {
console.log("Done! Elapsed time: " + (Date.now() - time) + "ms");
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
req.on("end", () => {
console.log("done");
});
console.log("Request started");
req.end();
不管maxBandwidth
的值是什么,或者根本没有使用SlowAgent
(我已经尝试注解掉agent: new SlowAgent()
),我注意到在运行时间上没有任何差异(大约4000毫秒)。我如何修复我的SlowAgent类?我不理解socket.pipe
吗?或者我需要做其他事情吗?
freakish指出要将SlowAgent更改为:
// this pipes an instance of Throttle
class SlowAgent extends http.Agent {
createConnection(options, callback){
const socket = new net.Socket(options);
socket.connect(options);
return socket.pipe(new Throttle({rate: 10}));
}
}
但这就带来了一个问题
problem with request: Parse Error: Expected HTTP/ Error: Parse Error: Expected HTTP/
at Throttle.socketOnData (_http_client.js:456:22)
at Throttle.emit (events.js:209:13)
at addChunk (_stream_readable.js:305:12)
at readableAddChunk (_stream_readable.js:286:11)
at Throttle.Readable.push (_stream_readable.js:220:10)
at Throttle.Transform.push (_stream_transform.js:150:32)
at /home/max/Documents/Personal/node-projects/proxy/node_modules/stream-throttle/src/throttle.js:37:14
at processTicksAndRejections (internal/process/task_queues.js:75:11) {
bytesParsed: 0,
code: 'HPE_INVALID_CONSTANT',
reason: 'Expected HTTP/',
rawPacket: <Buffer 47>
}
3条答案
按热度按时间fumotvh31#
我设法让它工作,通过做了一个定制的
agent
和使用createConnection
内的http.request
选项:6fe3ivhb2#
流媒体带宽控制必须在服务器端和客户端实现。
从**客户的Angular **,
客户端应用或客户端网络层或服务器网络层
服务器应用程序或服务器网络层客户端网络层
请看看这个测试代码.你可以改变双方的速度变量.
环境
Windows 10中的节点v10.16.3。
server.js
client.js
一个二个一个一个
对于真实世界示例,更改client.js限制率和下一行
变成了
在现实世界中,随着参与者的增多,网络会变得更加复杂。
服务器端OSI模型〈==〉网络〈==〉客户端OSI模型
因为互联网提供商或运营商会节流他们的端口,这将影响您的上传和下载速率。
k97glaaz3#
可能是: