Skip to content

Commit

Permalink
Allow multiple socket connections.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceK33z committed Nov 20, 2015
1 parent 75bca73 commit 2dd3058
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ function Server(compiler, options) {

this.hot = options.hot;
this.headers = options.headers;
this.sockets = [];

// Listening for events
var invalidPlugin = function() {
this.sockWrite("invalid");
this.sockWrite(this.sockets, "invalid");
}.bind(this);
compiler.plugin("compile", invalidPlugin);
compiler.plugin("invalid", invalidPlugin);
compiler.plugin("done", function(stats) {
if(!this.sock) return;

this._sendStats(stats.toJson());
this._sendStats(this.sockets, stats.toJson());
this._stats = stats;
}.bind(this));

Expand Down Expand Up @@ -147,7 +146,7 @@ function Server(compiler, options) {
}
proxy.web(req, res, proxyOptions, function(err){
var msg = "cannot proxy to " + proxyOptions.target + " (" + err.message + ")";
this.sockWrite("proxy-error", [msg]);
this.sockWrite(this.sockets, "proxy-error", [msg]);
res.statusCode = 502;
res.end();
}.bind(this));
Expand Down Expand Up @@ -178,7 +177,7 @@ function Server(compiler, options) {
app.all("*", function(req, res) {
proxy.web(req, res, contentBase, function(err) {
var msg = "cannot proxy to " + contentBase.target + " (" + err.message + ")";
this.sockWrite("proxy-error", [msg]);
this.sockWrite(this.sockets, "proxy-error", [msg]);
res.statusCode = 502;
res.end();
}.bind(this));
Expand Down Expand Up @@ -271,16 +270,25 @@ Server.prototype.listen = function() {
var sockServer = sockjs.createServer({
// Limit useless logs
log: function(severity, line) {
if (severity === 'error') {
if (severity === "error") {
console.log(line);
}
}
});
sockServer.on("connection", function(conn) {
this.sock = conn;
if(this.hot) this.sockWrite("hot");
this.sockets.push(conn);

// Remove the connection when it's closed
conn.on("close", function() {
var connIndex = this.sockets.indexOf(conn);
if (connIndex >= 0) {
this.sockets.splice(connIndex, 1);
}
}.bind(this));

if(this.hot) this.sockWrite([conn], "hot");
if(!this._stats) return;
this._sendStats(this._stats.toJson(), true);
this._sendStats([conn], this._stats.toJson(), true);
}.bind(this));

sockServer.installHandlers(this.listeningApp, {
Expand All @@ -289,15 +297,17 @@ Server.prototype.listen = function() {
}

Server.prototype.close = function() {
this.sock.close(); // Will also close listeningApp
this.sock = null;
this.sockets.forEach(function(sock) {
sock.close();
});
this.sockets = [];
this.middleware.close();
}

Server.prototype.sockWrite = function(type, data) {
if (this.sock) {
this.sock.write(JSON.stringify({type: type, data: data}));
}
Server.prototype.sockWrite = function(sockets, type, data) {
sockets.forEach(function(sock) {
sock.write(JSON.stringify({type: type, data: data}));
});
}

Server.prototype.serveMagicHtml = function(req, res, next) {
Expand All @@ -315,17 +325,17 @@ Server.prototype.serveMagicHtml = function(req, res, next) {
}

// send stats to a socket or multiple sockets
Server.prototype._sendStats = function(stats, force) {
Server.prototype._sendStats = function(sockets, stats, force) {
if(!force && stats && (!stats.errors || stats.errors.length === 0) && stats.assets && stats.assets.every(function(asset) {
return !asset.emitted;
})) return this.sockWrite("still-ok");
this.sockWrite("hash", stats.hash);
})) return this.sockWrite(sockets, "still-ok");
this.sockWrite(sockets, "hash", stats.hash);
if(stats.errors.length > 0)
this.sockWrite("errors", stats.errors);
this.sockWrite(sockets, "errors", stats.errors);
else if(stats.warnings.length > 0)
this.sockWrite("warnings", stats.warnings);
this.sockWrite(sockets, "warnings", stats.warnings);
else
this.sockWrite("ok");
this.sockWrite(sockets, "ok");
}

Server.prototype.invalidate = function() {
Expand Down

0 comments on commit 2dd3058

Please sign in to comment.