@@ -22,17 +22,16 @@ function Server(compiler, options) {
22
22
23
23
this . hot = options . hot ;
24
24
this . headers = options . headers ;
25
+ this . sockets = [ ] ;
25
26
26
27
// Listening for events
27
28
var invalidPlugin = function ( ) {
28
- this . sockWrite ( "invalid" ) ;
29
+ this . sockWrite ( this . sockets , "invalid" ) ;
29
30
} . bind ( this ) ;
30
31
compiler . plugin ( "compile" , invalidPlugin ) ;
31
32
compiler . plugin ( "invalid" , invalidPlugin ) ;
32
33
compiler . plugin ( "done" , function ( stats ) {
33
- if ( ! this . sock ) return ;
34
-
35
- this . _sendStats ( stats . toJson ( ) ) ;
34
+ this . _sendStats ( this . sockets , stats . toJson ( ) ) ;
36
35
this . _stats = stats ;
37
36
} . bind ( this ) ) ;
38
37
@@ -147,7 +146,7 @@ function Server(compiler, options) {
147
146
}
148
147
proxy . web ( req , res , proxyOptions , function ( err ) {
149
148
var msg = "cannot proxy to " + proxyOptions . target + " (" + err . message + ")" ;
150
- this . sockWrite ( "proxy-error" , [ msg ] ) ;
149
+ this . sockWrite ( this . sockets , "proxy-error" , [ msg ] ) ;
151
150
res . statusCode = 502 ;
152
151
res . end ( ) ;
153
152
} . bind ( this ) ) ;
@@ -178,7 +177,7 @@ function Server(compiler, options) {
178
177
app . all ( "*" , function ( req , res ) {
179
178
proxy . web ( req , res , contentBase , function ( err ) {
180
179
var msg = "cannot proxy to " + contentBase . target + " (" + err . message + ")" ;
181
- this . sockWrite ( "proxy-error" , [ msg ] ) ;
180
+ this . sockWrite ( this . sockets , "proxy-error" , [ msg ] ) ;
182
181
res . statusCode = 502 ;
183
182
res . end ( ) ;
184
183
} . bind ( this ) ) ;
@@ -271,16 +270,25 @@ Server.prototype.listen = function() {
271
270
var sockServer = sockjs . createServer ( {
272
271
// Limit useless logs
273
272
log : function ( severity , line ) {
274
- if ( severity === ' error' ) {
273
+ if ( severity === " error" ) {
275
274
console . log ( line ) ;
276
275
}
277
276
}
278
277
} ) ;
279
278
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" ) ;
282
290
if ( ! this . _stats ) return ;
283
- this . _sendStats ( this . _stats . toJson ( ) , true ) ;
291
+ this . _sendStats ( [ conn ] , this . _stats . toJson ( ) , true ) ;
284
292
} . bind ( this ) ) ;
285
293
286
294
sockServer . installHandlers ( this . listeningApp , {
@@ -289,15 +297,17 @@ Server.prototype.listen = function() {
289
297
}
290
298
291
299
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 = [ ] ;
294
304
this . middleware . close ( ) ;
295
305
}
296
306
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
+ } ) ;
301
311
}
302
312
303
313
Server . prototype . serveMagicHtml = function ( req , res , next ) {
@@ -315,17 +325,17 @@ Server.prototype.serveMagicHtml = function(req, res, next) {
315
325
}
316
326
317
327
// send stats to a socket or multiple sockets
318
- Server . prototype . _sendStats = function ( stats , force ) {
328
+ Server . prototype . _sendStats = function ( sockets , stats , force ) {
319
329
if ( ! force && stats && ( ! stats . errors || stats . errors . length === 0 ) && stats . assets && stats . assets . every ( function ( asset ) {
320
330
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 ) ;
323
333
if ( stats . errors . length > 0 )
324
- this . sockWrite ( "errors" , stats . errors ) ;
334
+ this . sockWrite ( sockets , "errors" , stats . errors ) ;
325
335
else if ( stats . warnings . length > 0 )
326
- this . sockWrite ( "warnings" , stats . warnings ) ;
336
+ this . sockWrite ( sockets , "warnings" , stats . warnings ) ;
327
337
else
328
- this . sockWrite ( "ok" ) ;
338
+ this . sockWrite ( sockets , "ok" ) ;
329
339
}
330
340
331
341
Server . prototype . invalidate = function ( ) {
0 commit comments