einaros/ws — Issue #36: Make it possible to listen on multiple http/https servers
Author: ypocat
Created: 2012-02-29 09:58:31+00:00
State: open

The reason for this is to be able to run a single `ws` on a single Expressjs/Connect http server abstraction, which works on top of one `http` sever, and one `https` server, or possibly even more servers connected to various ports.

So that this setup is possible:

```js
var express = require('express');
var app = express.createServer();
var ws = require('ws');

var httpsOptions = { key: ..., crt: ... };

var httpServer = http.createServer(app.handle.bind(app)).listen(port);
var httpsServer = https.createServer(httpsOptions, app.handle.bind(app))
.listen(httpsPort);

// the server could optionally take an array of servers to listen on:
var wss = new ws.Server({ server: [ httpServer, httpsServer ] });

// configure your "app", not having to deal with both
// http and https version separately:
app.get('/', function(req, res, next) { ... });
```

Currently, I'm working around this like this:

```js
var ServerHub = function(servers) {
var self = this;
var events = [ 'upgrade', 'error' ];
for(var s in servers) {
for(var e in events) {
(function(server, event) {
server.on(event, function() {
var args = Array.prototype.slice.call(arguments);
args.unshift(event);
self.emit.apply(self, args);
});
})(servers[s], events[e]);
}
}
};
util.inherits(ServerHub, events.EventEmitter);

wss = new ws.Server({ server: new ServerHub([ httpServer, httpsServer ]) });
```