Skip to content

Commit 2dd3058

Browse files
committed
Allow multiple socket connections.
1 parent 75bca73 commit 2dd3058

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

lib/Server.js

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@ function Server(compiler, options) {
2222

2323
this.hot = options.hot;
2424
this.headers = options.headers;
25+
this.sockets = [];
2526

2627
// Listening for events
2728
var invalidPlugin = function() {
28-
this.sockWrite("invalid");
29+
this.sockWrite(this.sockets, "invalid");
2930
}.bind(this);
3031
compiler.plugin("compile", invalidPlugin);
3132
compiler.plugin("invalid", invalidPlugin);
3233
compiler.plugin("done", function(stats) {
33-
if(!this.sock) return;
34-
35-
this._sendStats(stats.toJson());
34+
this._sendStats(this.sockets, stats.toJson());
3635
this._stats = stats;
3736
}.bind(this));
3837

@@ -147,7 +146,7 @@ function Server(compiler, options) {
147146
}
148147
proxy.web(req, res, proxyOptions, function(err){
149148
var msg = "cannot proxy to " + proxyOptions.target + " (" + err.message + ")";
150-
this.sockWrite("proxy-error", [msg]);
149+
this.sockWrite(this.sockets, "proxy-error", [msg]);
151150
res.statusCode = 502;
152151
res.end();
153152
}.bind(this));
@@ -178,7 +177,7 @@ function Server(compiler, options) {
178177
app.all("*", function(req, res) {
179178
proxy.web(req, res, contentBase, function(err) {
180179
var msg = "cannot proxy to " + contentBase.target + " (" + err.message + ")";
181-
this.sockWrite("proxy-error", [msg]);
180+
this.sockWrite(this.sockets, "proxy-error", [msg]);
182181
res.statusCode = 502;
183182
res.end();
184183
}.bind(this));
@@ -271,16 +270,25 @@ Server.prototype.listen = function() {
271270
var sockServer = sockjs.createServer({
272271
// Limit useless logs
273272
log: function(severity, line) {
274-
if (severity === 'error') {
273+
if (severity === "error") {
275274
console.log(line);
276275
}
277276
}
278277
});
279278
sockServer.on("connection", function(conn) {
280-
this.sock = conn;
281-
if(this.hot) this.sockWrite("hot");
279+
this.sockets.push(conn);
280+
281+
// Remove the connection when it's closed
282+
conn.on("close", function() {
283+
var connIndex = this.sockets.indexOf(conn);
284+
if (connIndex >= 0) {
285+
this.sockets.splice(connIndex, 1);
286+
}
287+
}.bind(this));
288+
289+
if(this.hot) this.sockWrite([conn], "hot");
282290
if(!this._stats) return;
283-
this._sendStats(this._stats.toJson(), true);
291+
this._sendStats([conn], this._stats.toJson(), true);
284292
}.bind(this));
285293

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

291299
Server.prototype.close = function() {
292-
this.sock.close(); // Will also close listeningApp
293-
this.sock = null;
300+
this.sockets.forEach(function(sock) {
301+
sock.close();
302+
});
303+
this.sockets = [];
294304
this.middleware.close();
295305
}
296306

297-
Server.prototype.sockWrite = function(type, data) {
298-
if (this.sock) {
299-
this.sock.write(JSON.stringify({type: type, data: data}));
300-
}
307+
Server.prototype.sockWrite = function(sockets, type, data) {
308+
sockets.forEach(function(sock) {
309+
sock.write(JSON.stringify({type: type, data: data}));
310+
});
301311
}
302312

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

317327
// send stats to a socket or multiple sockets
318-
Server.prototype._sendStats = function(stats, force) {
328+
Server.prototype._sendStats = function(sockets, stats, force) {
319329
if(!force && stats && (!stats.errors || stats.errors.length === 0) && stats.assets && stats.assets.every(function(asset) {
320330
return !asset.emitted;
321-
})) return this.sockWrite("still-ok");
322-
this.sockWrite("hash", stats.hash);
331+
})) return this.sockWrite(sockets, "still-ok");
332+
this.sockWrite(sockets, "hash", stats.hash);
323333
if(stats.errors.length > 0)
324-
this.sockWrite("errors", stats.errors);
334+
this.sockWrite(sockets, "errors", stats.errors);
325335
else if(stats.warnings.length > 0)
326-
this.sockWrite("warnings", stats.warnings);
336+
this.sockWrite(sockets, "warnings", stats.warnings);
327337
else
328-
this.sockWrite("ok");
338+
this.sockWrite(sockets, "ok");
329339
}
330340

331341
Server.prototype.invalidate = function() {

0 commit comments

Comments
 (0)