Skip to content

Commit

Permalink
make require('engine.io')() return a new Server instance
Browse files Browse the repository at this point in the history
fixes #212
  • Loading branch information
defunctzombie committed Jun 4, 2014
1 parent cb2b80a commit 48f006f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,30 @@ These are exposed by `require('engine.io')`:

##### Methods

- `()`
- Returns a new `Server` instance. If the first argument is an `http.Server` then the
new `Server` instance will be attached to it. Otherwise, the arguments are passed
directly to the `Server` constructor.
- **Parameters**
- `http.Server`: optional, server to attach to.
- `Object`: optional, options object (see `Server#constructor` api docs below)

The following are identical ways to instantiate a server and then attach it.
```js
var httpServer; // previously created with `http.createServer();` from node.js api.

// create a server first, and then attach
var eioServer = require('engine.io').Server();
eioServer.attach(httpServer);

// or call the module as a function to get `Server`
var eioServer = require('engine.io')();
eioServer.attach(httpServer);

// immediately attach
var eioServer = require('engine.io')(http_server);
```

- `listen`
- Creates an `http.Server` which listens on the given port and attaches WS
to it. It returns `501 Not Implemented` for regular http requests.
Expand Down
17 changes: 14 additions & 3 deletions lib/engine.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,27 @@
var http = require('http');

/**
* Invoking the library as a function delegates to attach
* Invoking the library as a function delegates to attach if the first argument
* is an `http.Server`.
*
* @param {http.Server} server
* If there are no arguments or the first argument is an options object, then
* a new Server instance is returned.
*
* @param {http.Server} server (if specified, will be attached to by the new Server instance)
* @param {Object} options
* @return {Server} engine server
* @api public
*/

exports = module.exports = function() {
return attach.apply(this, arguments);
// backwards compatible use as `.attach`
// if first argument is an http server
if (arguments.length && arguments[0] instanceof http.Server) {
return attach.apply(this, arguments);
}

// if first argument is not an http server, then just make a regular eio server
return exports.Server.apply(null, arguments);
};

/**
Expand Down
4 changes: 4 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ module.exports = Server;
*/

function Server(opts){
if (!(this instanceof Server)) {
return new Server(opts);
}

this.clients = {};
this.clientsCount = 0;

Expand Down
7 changes: 7 additions & 0 deletions test/engine.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ describe('engine', function () {
expect(version).to.be(require('engine.io-client/package').version);
});

describe('engine()', function () {
it('should create a Server when require called with no arguments', function () {
var engine = eio();
expect(engine).to.be.an(eio.Server);
});
});

describe('listen', function () {
it('should open a http server that returns 501', function (done) {
var server = listen(function (port) {
Expand Down

0 comments on commit 48f006f

Please sign in to comment.