support custom notification by carck · Pull Request #102 · binaryjs/binaryjs · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 55 additions & 12 deletions lib/client.js
6 changes: 3 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var ws = require('streamws');
var ws = require('ws');
var EventEmitter = require('events').EventEmitter;
var util = require('./util');

Expand All @@ -24,7 +24,7 @@ function BinaryServer(options) {
this._server = new ws.Server(options);
}

this._server.on('connection', function(socket){
this._server.on('connection', function(socket, req){
var clientId = self._clientCounter;
var binaryClient = new BinaryClient(socket, options);
binaryClient.id = clientId;
Expand All @@ -33,7 +33,7 @@ function BinaryServer(options) {
binaryClient.on('close', function(){
delete self.clients[clientId];
});
self.emit('connection', binaryClient);
self.emit('connection', binaryClient, req);
});
this._server.on('error', function(error){
self.emit('error', error);
Expand Down
33 changes: 30 additions & 3 deletions lib/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function BinaryStream(socket, id, create, meta) {

this._closed = false;
this._ended = false;
this._buf = [];

if(create) {
// This is a stream we are creating
Expand Down Expand Up @@ -61,19 +62,34 @@ BinaryStream.prototype._onPause = function() {
this.emit('pause');
};

BinaryStream.prototype.flow = function() {
var data = this._buf.shift();
if (data) {
this.emit('data', data);
}
if (!this.paused && this._buf.length > 0) {
process.nextTick(() => {
this.flow();
})
}
};

BinaryStream.prototype._onResume = function() {
// Emit resume event
this.paused = false;
this.emit('resume');
this.emit('drain');
process.nextTick(()=>{
this.flow();
})
};

BinaryStream.prototype._write = function(code, data, bonus) {
if (this._socket.readyState !== this._socket.constructor.OPEN) {
return false;
}
var message = util.pack([code, data, bonus]);
return this._socket.send(message) !== false;
return this._socket.send(message, {binary: true}) !== false;
};

BinaryStream.prototype.write = function(data) {
Expand Down Expand Up @@ -110,8 +126,19 @@ BinaryStream.prototype._onEnd = function() {
};

BinaryStream.prototype._onData = function(data) {
// Dispatch
this.emit('data', data);
if(!this.paused && this._buf.length === 0 && this.listenerCount('data') > 0) {
this.emit('data', data);
} else {
this._buf.push(data);
}
};

BinaryStream.prototype._onNotification = function(event) {
this.emit('notify', event);
};

BinaryStream.prototype.sendNotification = function(event) {
this._write(7, event, this.id);
};

BinaryStream.prototype.pause = function() {
Expand Down
12 changes: 4 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
"author": "Eric Zhang (http://ericzhang.com)",
"name": "binaryjs",
"description": "Binary realtime streaming made easy",
"version": "0.2.2",
"version": "0.2.3",
"homepage": "http://binaryjs.com",
"repository": {
"type": "git",
"url": "git://github.com/binaryjs/binaryjs.git"
},
"main": "lib/server.js",
"scripts": {
"test": "make test"
Expand All @@ -16,12 +12,12 @@
"node": ">=0.10.20"
},
"dependencies": {
"streamws": ">=0.1.1",
"binarypack": ">=0.0.4",
"ws": "https://github.com/carck/ws#9ada0bbf7e4a6e49744d8457c48ac253b3f7a5c6",
"binarypack": "https://github.com/carck/node-binarypack#0eed17aa94ef5d9fd9ec67e983a12631f57bfa0b",
"streamers": ">=0.1.0"
},
"devDependencies": {
"mocha": "~1.3.0",
"uglify-js": "~1.3.5"
}
}
}
15 changes: 15 additions & 0 deletions test/client.js