我使用节点js,socket.io和fs进行简单的带宽测试,用于流文件流。 我的目标是达到我已经完成的千兆速度但是我的问题是我如此快速地传输数据,我在客户端崩溃我的浏览器。 (FF和Chrome) 我不确定我是否遗漏了某些内容,或者我是否只是编写效率低下的代码。
我不关心客户端的数据所以我怀疑是否有一个javascript相当于将数据直接发送到/ dev / null来解决我的问题,但我不知道这样的事情。< / p>
有没有人有任何建议或遇到过类似的事情?
服务器端:
var io = require('socket.io').listen(81), fs = require('fs');
var connectedsocketlist = [];
io.sockets.on('connection', function (socket) {
connectedsocketlist.push(socket);
var readStream = fs.createReadStream("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);
});
});
客户端:
<html>
<script type="text/javascript" src="js/socket.io.js"></script>
<script type="text/javascript" src="js/jquery-2.0.1.min.js"></script>
<script>
var SPEED = {};
var startTime;
SPEED.bCount = 0;
var socket = io.connect('http://10.0.1.22:81',{resource:'socket.io', 'force new connection':false});
socket.on('message', function (data) {
SPEED.bCount = SPEED.bCount + data.length;
SPEED.bitsPerSecon = Math.floor( (SPEED.bCount * 8) / Math.floor(($.now() - SPEED.startTime) / 1000)*100/100);
$("#speed").text(Math.floor (SPEED.bitsPerSecon /1000 /1000 ) * 100 /100 );
});
</script>
<body>
<div style="solid #ccc" id="speed"> </div>
</body></html>
答案 0 :(得分:2)
在您的脚本中修复了许多逻辑错误并将其包装在默认快速服务器下之后,我发现在将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
/**
* 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'));
});
<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