Permalink
Browse files

Merge pull request #23 from tmpvar/nodelike

add binary.createServer(fn) to be more like http.createServer
  • Loading branch information...
2 parents 5a9eaf2 + 99ba1c2 commit 57789efb62698f43f56ce40eceee9019e15085ac @ericz ericz committed Jan 6, 2013
Showing with 30 additions and 4 deletions.
  1. +30 −4 lib/server.js
View
@@ -8,15 +8,15 @@ function BinaryServer(options) {
if (!(this instanceof BinaryServer)) return new BinaryServer(options);
var self = this;
-
+
options = util.extend({
host: '0.0.0.0',
chunkSize: 40960
}, options);
-
+
this.clients = {};
this._clientCounter = 0;
-
+
this._server = new ws.Server(options);
this._server.on('connection', function(socket){
var clientId = self._clientCounter;
@@ -41,4 +41,30 @@ BinaryServer.prototype.close = function(code, message){
}
exports.BinaryClient = BinaryClient;
-exports.BinaryServer = BinaryServer;
+exports.BinaryServer = BinaryServer;
+
+// Expose a method similar to http.createServer
+//
+// usage:
+// var fs = require('fs');
+// require('binaryjs').createServer(function(c) {
+// fs.createReadStream('some.bin').pipe(c);
+// });
+//
+exports.createServer = function(fn) {
+ var server;
+
+ return {
+ listen : function(port, opts) {
+ opts = opts || {};
+ opts.port = port;
+
+ var server = new BinaryServer({ port : port });
+ server.on('connection', function(conn) {
+ fn && fn(conn.createStream());
+ });
+
+ return server;
+ }
+ }
+};

2 comments on commit 57789ef

@sequoiar

line 62 should be var server = new BinaryServer(opts);

@ericz
Member
ericz commented on 57789ef Jan 16, 2013

@sequoiar fixed in 714d054

Please sign in to comment.