Permalink
Browse files

index: added client serving with `send`

  • Loading branch information...
1 parent bec38c2 commit 373c66f1ee78645f59c8d1a0335024e140746688 @rauchg rauchg committed Dec 13, 2012
Showing with 97 additions and 1 deletion.
  1. +97 −1 lib/index.js
View
@@ -4,12 +4,29 @@
*/
var http = require('http')
+ , send = require('send')
+ , parse = require('url').parse
, engine = require('engine.io')
, Client = require('./client')
, Namespace = require('./namespace')
, debug = require('debug')('socket.io:server');
/**
+ * Module exports.
+ */
+
+module.exports = Server;
+
+/**
+ * Read client
+ */
+
+var client = {
+ source: require('socket.io-client').source,
+ version: require('socket.io-client/package').version
+};
+
+/**
* Server constructor.
*
* @param {http.Server|Number|Object} http server, port or options
@@ -24,11 +41,40 @@ function Server(srv, opts){
srv = null;
}
opts = opts || {};
- if (srv) this.attach(srv, opts);
+ this.nsps = {};
this.sockets = this.of('/');
+ this.path(opts.path || '/socket.io');
+ this.client(false !== opts.client);
+ if (srv) this.attach(srv, opts);
}
/**
+ * Serve client code.
+ *
+ * @param {Boolean} whether to serve client code
+ * @api public
+ */
+
+Server.prototype.client = function(v){
+ if (!arguments.length) return this._client;
+ this._client = v;
+ return this;
+};
+
+/**
+ * Sets the client serving path.
+ *
+ * @param {String} pathname
+ * @api public
+ */
+
+Server.prototype.path = function(v){
+ if (!arguments.length) return this._path;
+ this._path = v.replace(/\/$/, '');
+ return this;
+};
+
+/**
* Attaches socket.io to a server or port.
*
* @param {http.Server|Number} server or port
@@ -42,12 +88,56 @@ Server.prototype.attach = function(srv, opts){
srv = engine.listen(srv, opts);
} else {
debug('creating engine.io instance', opts);
+ if (this._client) this.serve(srv);
srv = engine.attach(srv, opts);
}
this.bind(srv);
};
/**
+ * Attaches the static file serving.
+ *
+ * @param {Function|http.Server} http server
+ * @api private
+ */
+
+Server.prototype.serve = function(srv){
+ var url = this._path + '/';
+ var self = this;
+
+ function handle(req, res){
+ send(req, parse(req.url).pathname.substr(url.length))
+ .root(__dirname + '/client')
+ .index(false)
+ .pipe(res);
+ }
+
+ if (srv.use) {
+ debug('attaching client serving middleware');
+ srv.use(function(req, res, next){
+ if (0 == req.url.indexOf(url)) {
+ handle(req, res);
+ } else {
+ next();
+ }
+ });
+ } else {
+ debug('attaching client serving req handler');
+ var evs = srv.listeners('request').slice(0);
+ srv.removeAllListeners('request');
+ srv.on('request', function(req, res) {
+ if (0 == req.url.indexOf(url)) {
+ handle(req, res);
+ } else {
+ for (var i = 0; i < evs.length; i++) {
+ evs[i].call(srv, req.res);
+ }
+ }
+ });
+ }
+};
+
+/**
* Binds socket.io to an engine.io instance.
*
* @param {engine.Server} engine.io (or compatible) server
@@ -105,3 +195,9 @@ Namespace.flags.forEach(function(flag){
return this;
});
});
+
+/**
+ * BC with `io.listen`
+ */
+
+Server.listen = Server;

0 comments on commit 373c66f

Please sign in to comment.