FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

New option to control the stream id generation by erlichmen · Pull Request #40 · binaryjs/binaryjs · GitHub

Repository navigation

Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .js  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
34 changes: 27 additions & 7 deletions lib/client.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ var BinaryStream = require('./stream').BinaryStream;

// end node


function sequentialStreamId(socket) {
if (!(this instanceof sequentialStreamId)) return new sequentialStreamId(socket);

if(typeof socket === 'string') {
this._nextId = 0;
} else {
// Use odd numbered ids for server originated streams
this._nextId = 1;
}
}

sequentialStreamId.prototype.next = function(meta) {
var result = this._nextId;
this._nextId += 2;

return result;
}

function BinaryClient(socket, options) {
if (!(this instanceof BinaryClient)) return new BinaryClient(socket, options);

Expand All @@ -17,17 +36,18 @@ function BinaryClient(socket, options) {
var self = this;

this._options = util.extend({
chunkSize: 40960
chunkSize: 40960,
streamIdGenerator: sequentialStreamId
}, options);

this.streams = {};


this.streamIdGenerator = this._options.streamIdGenerator(socket);

if(typeof socket === 'string') {
this._nextId = 0;
this._socket = new WebSocket(socket);
} else {
// Use odd numbered ids for server originated streams
this._nextId = 1;
this._socket = socket;
}

Expand Down Expand Up @@ -234,10 +254,10 @@ BinaryClient.prototype.createStream = function(meta){
return;
}
var self = this;
var streamId = this._nextId;
this._nextId += 2;
var streamId = this.streamIdGenerator.next(meta);

var binaryStream = new BinaryStream(this._socket, streamId, true, meta);
binaryStream.on('close', function(){
binaryStream.on('close', function(){
delete self.streams[streamId];
});
this.streams[streamId] = binaryStream;
Expand Down

Back | FazBrowse Home | New Git URL