Permalink
Browse files

Revert "engine.io: added middleware attachment possibility (eg: for e…

…xpress)"

This reverts commit 7b3243d.
  • Loading branch information...
1 parent be99036 commit 73c772d0b3e38dc7ba54d3e8c1338c838b61f636 @rauchg rauchg committed Dec 16, 2012
Showing with 36 additions and 67 deletions.
  1. +36 −67 lib/engine.io.js
View
@@ -93,6 +93,7 @@ exports.listen = function (port, options, fn) {
*/
exports.attach = function (server, options) {
+ var engine = new exports.Server(options);
var options = options || {};
var path = (options.path || '/engine.io').replace(/\/$/, '');
var resource = options.resource || 'default';
@@ -104,82 +105,50 @@ exports.attach = function (server, options) {
return path == req.url.substr(0, path.length);
}
- // Server instance to return
- var engine;
-
- // if express-like
- if ('function' == typeof server && server.use) {
- if (null == options.allowUpgrades || options.allowUpgrades) {
- options.allowUpgrades = false;
- console.log(
- '\033[91m' +
- 'You are trying to attach engine.io to an express app.\n' +
- 'HTTP long-polling will work, but WebSocket won\'t.\n' +
- 'In order for engine.io to listen to the `upgrade` event\n' +
- 'you need to pass a Node `http.Server` instance.' +
- 'You can explicitly pass the option `allowUpgrades: false\n' +
- 'for this warning to disappear.' +
- '\033[39m'
- );
- }
-
- engine = new exports.Server(options);
- server.use(function(req, res, next){
- if (check(req)) {
- debug('intercepting request for path "%s"', path);
- engine.handleRequest(req, res);
- } else {
- next();
- }
- });
- } else {
- engine = new exports.Server(options);
+ // cache and clean up listeners
+ var listeners = server.listeners('request');
+ var oldListeners = [];
- // cache and clean up listeners
- var listeners = server.listeners('request');
- var oldListeners = [];
+ // copy the references onto a new array for node >=0.7
+ for (var i = 0, l = listeners.length; i < l; i++) {
+ oldListeners[i] = listeners[i];
+ }
- // copy the references onto a new array for node >=0.7
- for (var i = 0, l = listeners.length; i < l; i++) {
- oldListeners[i] = listeners[i];
- }
+ server.removeAllListeners('request');
- server.removeAllListeners('request');
+ server.on('close', function () {
+ engine.close();
+ });
- server.on('close', function () {
- engine.close();
- });
+ // add request handler
+ server.on('request', function (req, res) {
+ if (check(req)) {
+ debug('intercepting request for path "%s"', path);
+ engine.handleRequest(req, res);
+ } else {
+ for (var i = 0, l = oldListeners.length; i < l; i++) {
+ oldListeners[i].call(server, req, res);
+ }
+ }
+ });
- // add request handler
- server.on('request', function (req, res) {
+ if(~engine.transports.indexOf('websocket')) {
+ server.on('upgrade', function (req, socket, head) {
if (check(req)) {
- debug('intercepting request for path "%s"', path);
- engine.handleRequest(req, res);
- } else {
- for (var i = 0, l = oldListeners.length; i < l; i++) {
- oldListeners[i].call(server, req, res);
- }
+ engine.handleUpgrade(req, socket, head);
+ } else if (false !== options.destroyUpgrade) {
+ socket.end();
}
});
+ }
- if(~engine.transports.indexOf('websocket')) {
- server.on('upgrade', function (req, socket, head) {
- if (check(req)) {
- engine.handleUpgrade(req, socket, head);
- } else if (false !== options.destroyUpgrade) {
- socket.end();
- }
- });
- }
-
- // flash policy file
- var trns = engine.transports;
- var policy = options.policyFile;
- if (~trns.indexOf('flashsocket') && false !== policy) {
- server.on('connection', function (socket) {
- engine.handleSocket(socket);
- });
- }
+ // flash policy file
+ var trns = engine.transports;
+ var policy = options.policyFile;
+ if (~trns.indexOf('flashsocket') && false !== policy) {
+ server.on('connection', function (socket) {
+ engine.handleSocket(socket);
+ });
}
return engine;

0 comments on commit 73c772d

Please sign in to comment.