Comparing version 0.4.6 to 0.4.7
@@ -13,3 +13,4 @@ { | ||
"dependencies": { | ||
"express": "latest" | ||
"express": "latest", | ||
"ansi": "https://github.com/einaros/ansi.js/tarball/master" | ||
}, | ||
@@ -16,0 +17,0 @@ "devDependencies": {}, |
@@ -29,2 +29,3 @@ function onFilesSelected(e) { | ||
uploader.ondone = function() { | ||
uploader.close(); | ||
progress.innerHTML = '100% done, ' + totalFiles + ' files sent.'; | ||
@@ -31,0 +32,0 @@ } |
@@ -21,2 +21,4 @@ function Uploader(url, cb) { | ||
self.sendCallback = null; | ||
if (callback) callback(); | ||
if (self.sendQueue.length === 0 && self.ondone) self.ondone(null); | ||
if (self.sendQueue.length > 0) { | ||
@@ -26,4 +28,2 @@ var args = self.sendQueue.pop(); | ||
} | ||
if (callback) callback(); | ||
if (self.sendQueue.length === 0 && self.ondone) self.ondone(); | ||
} | ||
@@ -37,3 +37,3 @@ else if (data.event == 'error') { | ||
if (callback) callback(error); | ||
else throw error; | ||
if (self.ondone) self.ondone(error); | ||
} | ||
@@ -44,2 +44,3 @@ } | ||
Uploader.prototype.sendFile = function(file, cb) { | ||
if (this.ws.readyState != WebSocket.OPEN) throw new Error('Not connected'); | ||
if (this.sending) { | ||
@@ -55,1 +56,5 @@ this.sendQueue.push(arguments); | ||
} | ||
Uploader.prototype.close = function() { | ||
this.ws.close(); | ||
} |
var WebSocketServer = require('../../').Server | ||
, express = require('express') | ||
, fs = require('fs') | ||
, util = require('util') | ||
, path = require('path') | ||
, app = express.createServer(); | ||
, app = express.createServer() | ||
, events = require('events') | ||
, ansi = require('ansi') | ||
, cursor = ansi(process.stdout); | ||
app.use(express.static(__dirname + '/public')); | ||
function BandwidthSampler(ws, interval) { | ||
interval = interval || 2000; | ||
var previousByteCount = 0; | ||
var self = this; | ||
var intervalId = setInterval(function() { | ||
var byteCount = ws.bytesReceived; | ||
var bytesPerSec = (byteCount - previousByteCount) / (interval / 1000); | ||
previousByteCount = byteCount; | ||
self.emit('sample', bytesPerSec); | ||
}, interval); | ||
ws.on('close', function() { | ||
clearInterval(intervalId); | ||
}); | ||
} | ||
util.inherits(BandwidthSampler, events.EventEmitter); | ||
@@ -12,3 +30,3 @@ function makePathForFile(filePath, prefix, cb) { | ||
filePath = path.dirname(path.normalize(filePath)).replace(/^(\/|\\)+/, ''); | ||
var pieces = filePath.split('/'); | ||
var pieces = filePath.split(/(\\|\/)/); | ||
var incrementalPath = prefix; | ||
@@ -27,4 +45,19 @@ function step(error) { | ||
cursor.eraseData(2).goto(1, 1); | ||
app.use(express.static(__dirname + '/public')); | ||
var clientId = 0; | ||
var wss = new WebSocketServer({server: app}); | ||
wss.on('connection', function(ws) { | ||
var thisId = ++clientId; | ||
cursor.goto(1, 4 + thisId).eraseLine(); | ||
console.log('Client #%d connected', thisId); | ||
var sampler = new BandwidthSampler(ws); | ||
sampler.on('sample', function(bps) { | ||
cursor.goto(1, 4 + thisId).eraseLine(); | ||
console.log('WebSocket #%d incoming bandwidth: %d MB/s', thisId, Math.round(bps / (1024*1024))); | ||
}); | ||
var filesReceived = 0; | ||
var currentFile = null; | ||
@@ -45,3 +78,4 @@ ws.on('message', function(data, flags) { | ||
fs.writeFile(path + '/' + currentFile.name, data, function(error) { | ||
console.log('received %d bytes long file, %s', data.length, currentFile.path); | ||
++filesReceived; | ||
// console.log('received %d bytes long file, %s', data.length, currentFile.path); | ||
ws.send(JSON.stringify({event: 'complete', path: currentFile.path})); | ||
@@ -53,7 +87,11 @@ currentFile = null; | ||
}); | ||
ws.on('close', function() { | ||
console.log('closed', arguments); | ||
cursor.goto(1, 4 + thisId).eraseLine(); | ||
console.log('Client #%d disconnected. %d files received.', thisId, filesReceived); | ||
}); | ||
ws.on('error', function(e) { | ||
console.log('error', e); | ||
cursor.goto(1, 4 + thisId).eraseLine(); | ||
console.log('Client #%d error: %s', thisId, e.message); | ||
}); | ||
@@ -60,0 +98,0 @@ }); |
@@ -0,1 +1,8 @@ | ||
v0.4.7 - Feb 21st 2012 | ||
===================== | ||
* Exposed bytesReceived from websocket client object, which makes it possible to implement bandwidth sampling. [einaros] | ||
* Updated browser based file upload example to include and output per websocket channel bandwidth sampling. [einaros] | ||
* Changed build scripts to check which architecture is currently in use. Required after the node.js changes to have prebuilt packages target ia32 by default. [einaros] | ||
v0.4.6 - Feb 9th 2012 | ||
@@ -2,0 +9,0 @@ ===================== |
@@ -10,3 +10,3 @@ /*! | ||
*/ | ||
try { | ||
@@ -17,3 +17,5 @@ module.exports = require('../build/Release/bufferutil'); | ||
} catch (e) { | ||
console.error('bufferutil.node has either not been built (run make),'); | ||
console.error('or your node install has changed architecture (run make clean && make).') | ||
throw e; | ||
}} | ||
}} |
@@ -10,3 +10,3 @@ /*! | ||
*/ | ||
try { | ||
@@ -17,3 +17,5 @@ module.exports = require('../build/Release/validation'); | ||
} catch (e) { | ||
console.error('validation.node has either not been built (run make),'); | ||
console.error('or your node install has changed architecture (run make clean && make).') | ||
throw e; | ||
}} | ||
}} |
@@ -42,2 +42,8 @@ /*! | ||
Object.defineProperty(this, '_socket', { writable: true, value: null }); | ||
Object.defineProperty(this, '_bytesReceived', { writable: true, value: null }); | ||
Object.defineProperty(this, 'bytesReceived', { | ||
get: function() { | ||
return self._bytesReceived; | ||
} | ||
}); | ||
Object.defineProperty(this, 'readyState', { | ||
@@ -497,2 +503,3 @@ get: function() { | ||
if (upgradeHead && upgradeHead.length > 0) { | ||
self._bytesReceived += upgradeHead.length; | ||
var head = upgradeHead; | ||
@@ -503,6 +510,12 @@ upgradeHead = null; | ||
dataHandler = realHandler; | ||
if (data) receiver.add(data); | ||
if (data) { | ||
self._bytesReceived += data.length; | ||
receiver.add(data); | ||
} | ||
} | ||
// subsequent packets are pushed straight to the receiver | ||
function realHandler(data) { receiver.add(data); } | ||
function realHandler(data) { | ||
if (data) self._bytesReceived += data.length; | ||
receiver.add(data); | ||
} | ||
var dataHandler = firstHandler; | ||
@@ -509,0 +522,0 @@ socket.on('data', dataHandler); |
@@ -5,3 +5,3 @@ { | ||
"description": "simple to use, blazing fast and thoroughly tested websocket client, server and console for node.js, up-to-date against RFC-6455", | ||
"version": "0.4.6", | ||
"version": "0.4.7", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -43,2 +43,16 @@ var assert = require('assert') | ||
describe('properties', function() { | ||
it('#bytesReceived exposes number of bytes received', function(done) { | ||
var wss = new WebSocketServer({port: ++port}, function() { | ||
var ws = new WebSocket('ws://localhost:' + port); | ||
ws.on('message', function() { | ||
ws.bytesReceived.should.eql(8); | ||
wss.close(); | ||
done(); | ||
}); | ||
}); | ||
wss.on('connection', function(ws) { | ||
ws.send('foobar'); | ||
}); | ||
}); | ||
it('#url exposes the server url', function(done) { | ||
@@ -119,2 +133,6 @@ server.createServer(++port, function(srv) { | ||
/* | ||
* Ready state constants | ||
*/ | ||
var readyStates = { | ||
@@ -127,2 +145,6 @@ CONNECTING: 0, | ||
/* | ||
* Ready state constant tests | ||
*/ | ||
Object.keys(readyStates).forEach(function(state) { | ||
@@ -129,0 +151,0 @@ describe('.' + state, function() { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
203975
5283