在修复脚本中的一些逻辑错误并将其包装在默认快速服务器下之后,我发现在将 2.2GB 文件从服务器传输到客户端时内存没有明显增加。
我使用 socket.volatile.send() 和 socket.volatile.emit() 运行,速度没有明显差异。
Server: node.js v0.8.6 on Fedora release 11 (Leonidas)
Client: Google Chrome Version 28.0.1500.95 m
服务器源:app.js
/**
* Module dependencies.
*/
var express=require('express'),
app=express(),
http=require('http'),
server=http.createServer(app),
io=require('socket.io').listen(server),
routes=require('./routes'),
user=require('./routes/user'),
path=require('path'),
fs=require('fs');
// all environments -- express defaults
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.errorHandler());
app.get('/', routes.index);
app.get('/users', user.list);
var connectedsocketlist=[];
io.sockets.on('connection', function (socket) {
console.log('socket connected');
connectedsocketlist.push(socket);
var readStream=fs.createReadStream("./public/2Gig.txt", {
'encoding': 'base64',
mode: 0666,
autoClose: true,
bufferSize: 1024
});
console.log(io.sockets.manager.rooms);
io.set('log level', 2, 'transports', ['websocket']); // reduce logging
readStream.on('data', function(data) {
//socket.volatile.send(data);
socket.volatile.emit('message',data);
});
readStream.on('end',function(){
console.log('DATA COMPLETE');
socket.volatile.emit('data_complete');
});
});
// start it up...
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
客户端来源:test.html
<html>
<head>
</head>
<body>
<div>Elapsed Seconds:<span style="color:#ccc;background-color:#000;" id="seconds"> </span></div>
<div>Bits per second:<span style="color:#ccc;background-color:#000;" id="bitSpeed"> </span></div>
<div>Bytes per second:<span style="color:#ccc;background-color:#000;" id="byteSpeed"> </span></div>
<div id="msg"> </div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script>
var socket=io.connect()
SPEED={
startTime:Date.now(),
elapsedSeconds:0,
totalBits:0,
totalBytes:0,
bitsPerSecond:0,
bytesPerSecond:0
};
socket.on('message', function (data) {
var numBytes=data.length,
now=Date.now();
SPEED.totalBytes=SPEED.totalBytes+numBytes;
//console.log('received %s bytes of data (%s bytes total)',numBytes,SPEED.totalBytes);
SPEED.elapsedSeconds=Math.floor((now-SPEED.startTime)/1000);
$('#seconds').text(SPEED.elapsedSeconds);
SPEED.totalBits=SPEED.totalBits+(numBytes*8);
SPEED.bitsPerSecond=Math.floor(SPEED.totalBits/SPEED.elapsedSeconds);
$('#bitSpeed').text(SPEED.bitsPerSecond);
SPEED.bytesPerSecond=Math.floor(SPEED.totalBytes/SPEED.elapsedSeconds);
$('#byteSpeed').text(SPEED.bytesPerSecond);
});
socket.on('data_complete',function(){
$('#msg').text('DATA IS COMPLETE');
});
</script>
</body>
</html>
服务器输出
> node app.js
info - socket.io started
Express server listening on port 3000
GET /test.html 200 4ms - 1.45kb
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized SgL8H5FaXzGGZdmuBebk
debug - setting request GET /socket.io/1/websocket/SgL8H5FaXzGGZdmuBebk
debug - set heartbeat interval for client SgL8H5FaXzGGZdmuBebk
debug - client authorized for
debug - websocket writing 1::
socket connected
{ '': [ 'SgL8H5FaXzGGZdmuBebk' ] }
DATA COMPLETE
^C
>
客户结果
Request: http://hostname:3000/test.html
Response:
Elapsed Seconds:353
Bits per second:84686466
Bytes per second:10585808
DATA IS COMPLETE