Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, that's because <a href="http://nodejs.org/api/net.html#net_class_net_server" rel="nofollow"><code>net.server</code></a> uses <a href="http://nodejs.org/api/net.html#net_class_net_socket" rel="nofollow"><code>net.socket</code></a>. To get the totals, you will have to add <code>bytesRead</code> and <code>bytesWritten</code> to the totals once the socket is closed. Example:</p> <pre><code>const net = require("net"); var server = net.createServer(function (c) { c.on('close', function () { // add to the totals server.bytesSent += c.bytesWritten; server.bytesReceived += c.bytesRead; }); c.write('Hello world!\r\n'); c.pipe(c); c.end(); }); server.bytesReceived = 0; server.bytesSent = 0; server.listen(3000); 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: %d bytes", process.memoryUsage().rss); console.log("Uptime: %ds", Math.round(process.uptime())); console.log("Open connections: %d", server.connections); console.log("In: %d bytes (%d bytes/s)", server.bytesReceived, bpsReceived); console.log("Out: %d bytes (%d bytes/s)", server.bytesSent, bpsSent); }, 100); </code></pre> <p>If you need to update the totals in real-time (when data is received/sent), you can instead add the length of the buffers directly to the totals when they are written/read. This is especially good when you have sockets that are open for a long time and transferring large amounts of data.</p> <pre><code>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(); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload