gpt4 book ai didi

node.js - 我可以检测 Node Socket.IO 客户端使用了多少带宽吗?

转载 作者:太空宇宙 更新时间:2023-11-04 01:07:10 26 4
gpt4 key购买 nike

有人声称我的网站正在使用相当大量的带宽,而他们的互联网提供商正在限制带宽或类似的东西。

知道如何获取客户端每秒接收和发送的数据量吗?我想在可以监控的地方(在客户端的屏幕上)显示少量的使用情况统计信息。

谢谢

最佳答案

看起来这是可能的,我发现了这个gist 。我测试了一下,似乎有效!

它并不是特定于socket.io的, 我找不到任何东西 (哦我发现了this)。但看起来您可以将其实现到服务器上的发射函数中以计算发送的字节数。祝你好运。

const net = require("net");

var server = net.createServer(function (c) {
var oldWrite = c.write;
c.write = function(d) {
if (!Buffer.isBuffer(d)) {
d = new Buffer(d);
}
oldWrite.call(this, d);
server.bytesSent += d.length;
};

c.on('data', function(d){
server.bytesReceived += d.length;
});

c.write('Hello world!\r\n');
c.pipe(c);
c.end();
});

server.bytesReceived = 0;
server.bytesSent = 0;

server.listen(3000);

var units = ["B", "kB", "MB", "TB"];
function simplifiedUnits(input) {
var unit = units[0];
var i = 0;
while (input > 1024 && ++i) {
unit = units[i];
input /= 1024;
}
return Math.round(input) + " " + unit;
}

var time = process.hrtime();
setInterval(function (){
process.stdout.write('\u001B[2J\u001B[0;0f');
var diff = process.hrtime(time)[0] + process.hrtime(time)[1]/1000000000;
var bpsSent = Math.round(server.bytesSent/diff) || 0;
var bpsReceived = Math.round(server.bytesReceived/diff) || 0;
console.log("Running node.js %s on %s-%s", process.version, process.platform, process.arch);
console.log("Memory usage: %s", simplifiedUnits(process.memoryUsage().rss));
console.log("Uptime: %ds", Math.round(process.uptime()));
console.log("Open connections: %d", server.connections);
console.log("In: %s (%s/s)", simplifiedUnits(server.bytesReceived), simplifiedUnits(bpsReceived));
console.log("Out: %s (%s/s)", simplifiedUnits(server.bytesSent), simplifiedUnits(bpsSent));
}, 100);

关于node.js - 我可以检测 Node Socket.IO 客户端使用了多少带宽吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21921420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com