Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

engine.io: added middleware attachment possibility (eg: for express)

  • Loading branch information...
commit 7b3243dbeaa611751780617158a577b006d477ce 1 parent 7e0cdca
@rauchg rauchg authored
Showing with 67 additions and 36 deletions.
  1. +67 −36 lib/engine.io.js
View
103 lib/engine.io.js
@@ -93,7 +93,6 @@ 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';
@@ -105,50 +104,82 @@ exports.attach = function (server, options) {
return path == req.url.substr(0, path.length);
}
- // 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];
- }
+ // 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'
+ );
+ }
- server.removeAllListeners('request');
+ 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);
- server.on('close', function () {
- engine.close();
- });
+ // cache and clean up listeners
+ var listeners = server.listeners('request');
+ var oldListeners = [];
- // 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);
- }
+ // 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];
}
- });
- if(~engine.transports.indexOf('websocket')) {
- server.on('upgrade', function (req, socket, head) {
+ server.removeAllListeners('request');
+
+ server.on('close', function () {
+ engine.close();
+ });
+
+ // add request handler
+ server.on('request', function (req, res) {
if (check(req)) {
- engine.handleUpgrade(req, socket, head);
- } else if (false !== options.destroyUpgrade) {
- socket.end();
+ 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);
+ }
}
});
- }
- // 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);
- });
+ 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);
+ });
+ }
}
return engine;
Please sign in to comment.
Something went wrong with that request. Please try again.