@@ -29,7 +29,6 @@ class Server {
29
29
this . compiler = compiler ;
30
30
this . options = options ;
31
31
this . logger = this . compiler . getInfrastructureLogger ( 'webpack-dev-server' ) ;
32
- this . webSocketConnections = [ ] ;
33
32
this . staticWatchers = [ ] ;
34
33
// Keep track of websocket proxies for external websocket upgrade.
35
34
this . webSocketProxies = [ ] ;
@@ -193,11 +192,13 @@ class Server {
193
192
msg = `${ msg } (${ addInfo } )` ;
194
193
}
195
194
196
- this . sendMessage ( this . webSocketConnections , 'progress-update' , {
197
- percent,
198
- msg,
199
- pluginName,
200
- } ) ;
195
+ if ( this . webSocketServer ) {
196
+ this . sendMessage ( this . webSocketServer . clients , 'progress-update' , {
197
+ percent,
198
+ msg,
199
+ pluginName,
200
+ } ) ;
201
+ }
201
202
202
203
if ( this . server ) {
203
204
this . server . emit ( 'progress-update' , { percent, msg, pluginName } ) ;
@@ -212,15 +213,17 @@ class Server {
212
213
}
213
214
214
215
setupHooks ( ) {
215
- // Listening for events
216
- const invalidPlugin = ( ) => {
217
- this . sendMessage ( this . webSocketConnections , 'invalid' ) ;
218
- } ;
219
-
220
216
const addHooks = ( compiler ) => {
221
- compiler . hooks . invalid . tap ( 'webpack-dev-server' , invalidPlugin ) ;
217
+ compiler . hooks . invalid . tap ( 'webpack-dev-server' , ( ) => {
218
+ if ( this . webSocketServer ) {
219
+ this . sendMessage ( this . webSocketServer . clients , 'invalid' ) ;
220
+ }
221
+ } ) ;
222
222
compiler . hooks . done . tap ( 'webpack-dev-server' , ( stats ) => {
223
- this . sendStats ( this . webSocketConnections , this . getStats ( stats ) ) ;
223
+ if ( this . webSocketServer ) {
224
+ this . sendStats ( this . webSocketServer . clients , this . getStats ( stats ) ) ;
225
+ }
226
+
224
227
this . stats = stats ;
225
228
} ) ;
226
229
} ;
@@ -673,15 +676,19 @@ class Server {
673
676
// eslint-disable-next-line new-cap
674
677
this . webSocketServer = new this . webSocketServerImplementation ( this ) ;
675
678
676
- this . webSocketServer . onConnection ( ( connection , headers ) => {
677
- if ( ! connection ) {
678
- return ;
679
- }
679
+ this . webSocketServer . implementation . on ( 'connection' , ( client , request ) => {
680
+ const headers =
681
+ // eslint-disable-next-line no-nested-ternary
682
+ typeof request !== 'undefined'
683
+ ? request . headers
684
+ : typeof client . headers !== 'undefined'
685
+ ? client . headers
686
+ : // eslint-disable-next-line no-undefined
687
+ undefined ;
680
688
681
689
if ( ! headers ) {
682
690
this . logger . warn (
683
- 'webSocketServer implementation must pass headers to the callback of onConnection(f) ' +
684
- 'via f(connection, headers) in order for clients to pass a headers security check'
691
+ 'webSocketServer implementation must pass headers for the "connection" event'
685
692
) ;
686
693
}
687
694
@@ -690,48 +697,34 @@ class Server {
690
697
! this . checkHostHeader ( headers ) ||
691
698
! this . checkOriginHeader ( headers )
692
699
) {
693
- this . sendMessage ( [ connection ] , 'error' , 'Invalid Host/Origin header' ) ;
700
+ this . sendMessage ( [ client ] , 'error' , 'Invalid Host/Origin header' ) ;
694
701
695
- this . webSocketServer . closeConnection ( connection ) ;
702
+ client . terminate ( ) ;
696
703
697
704
return ;
698
705
}
699
706
700
- this . webSocketConnections . push ( connection ) ;
701
-
702
- this . webSocketServer . onConnectionClose ( connection , ( ) => {
703
- const idx = this . webSocketConnections . indexOf ( connection ) ;
704
-
705
- if ( idx >= 0 ) {
706
- this . webSocketConnections . splice ( idx , 1 ) ;
707
- }
708
- } ) ;
709
-
710
707
if ( this . options . hot === true || this . options . hot === 'only' ) {
711
- this . sendMessage ( [ connection ] , 'hot' ) ;
708
+ this . sendMessage ( [ client ] , 'hot' ) ;
712
709
}
713
710
714
711
if ( this . options . liveReload ) {
715
- this . sendMessage ( [ connection ] , 'liveReload' ) ;
712
+ this . sendMessage ( [ client ] , 'liveReload' ) ;
716
713
}
717
714
718
715
if ( this . options . client && this . options . client . progress ) {
719
- this . sendMessage (
720
- [ connection ] ,
721
- 'progress' ,
722
- this . options . client . progress
723
- ) ;
716
+ this . sendMessage ( [ client ] , 'progress' , this . options . client . progress ) ;
724
717
}
725
718
726
719
if ( this . options . client && this . options . client . overlay ) {
727
- this . sendMessage ( [ connection ] , 'overlay' , this . options . client . overlay ) ;
720
+ this . sendMessage ( [ client ] , 'overlay' , this . options . client . overlay ) ;
728
721
}
729
722
730
723
if ( ! this . stats ) {
731
724
return ;
732
725
}
733
726
734
- this . sendStats ( [ connection ] , this . getStats ( this . stats ) , true ) ;
727
+ this . sendStats ( [ client ] , this . getStats ( this . stats ) , true ) ;
735
728
} ) ;
736
729
}
737
730
@@ -1072,8 +1065,7 @@ class Server {
1072
1065
1073
1066
close ( callback ) {
1074
1067
if ( this . webSocketServer ) {
1075
- this . webSocketServer . close ( ) ;
1076
- this . webSocketConnections = [ ] ;
1068
+ this . webSocketServer . implementation . close ( ) ;
1077
1069
}
1078
1070
1079
1071
const prom = Promise . all (
@@ -1235,12 +1227,14 @@ class Server {
1235
1227
return false ;
1236
1228
}
1237
1229
1238
- sendMessage ( webSocketConnections , type , data ) {
1239
- webSocketConnections . forEach ( ( webSocketConnection ) => {
1240
- this . webSocketServer . send (
1241
- webSocketConnection ,
1242
- JSON . stringify ( { type, data } )
1243
- ) ;
1230
+ // eslint-disable-next-line class-methods-use-this
1231
+ sendMessage ( clients , type , data ) {
1232
+ clients . forEach ( ( client ) => {
1233
+ // `sockjs` uses `1` to indicate client is ready to accept data
1234
+ // `ws` uses `WebSocket.OPEN`, but it is mean `1` too
1235
+ if ( client . readyState === 1 ) {
1236
+ client . send ( JSON . stringify ( { type, data } ) ) ;
1237
+ }
1244
1238
} ) ;
1245
1239
}
1246
1240
@@ -1270,7 +1264,7 @@ class Server {
1270
1264
}
1271
1265
1272
1266
// Send stats to a socket or multiple sockets
1273
- sendStats ( webSocketConnections , stats , force ) {
1267
+ sendStats ( clients , stats , force ) {
1274
1268
const shouldEmit =
1275
1269
! force &&
1276
1270
stats &&
@@ -1280,23 +1274,23 @@ class Server {
1280
1274
stats . assets . every ( ( asset ) => ! asset . emitted ) ;
1281
1275
1282
1276
if ( shouldEmit ) {
1283
- this . sendMessage ( webSocketConnections , 'still-ok' ) ;
1277
+ this . sendMessage ( clients , 'still-ok' ) ;
1284
1278
1285
1279
return ;
1286
1280
}
1287
1281
1288
- this . sendMessage ( webSocketConnections , 'hash' , stats . hash ) ;
1282
+ this . sendMessage ( clients , 'hash' , stats . hash ) ;
1289
1283
1290
1284
if ( stats . errors . length > 0 || stats . warnings . length > 0 ) {
1291
1285
if ( stats . warnings . length > 0 ) {
1292
- this . sendMessage ( webSocketConnections , 'warnings' , stats . warnings ) ;
1286
+ this . sendMessage ( clients , 'warnings' , stats . warnings ) ;
1293
1287
}
1294
1288
1295
1289
if ( stats . errors . length > 0 ) {
1296
- this . sendMessage ( webSocketConnections , 'errors' , stats . errors ) ;
1290
+ this . sendMessage ( clients , 'errors' , stats . errors ) ;
1297
1291
}
1298
1292
} else {
1299
- this . sendMessage ( webSocketConnections , 'ok' ) ;
1293
+ this . sendMessage ( clients , 'ok' ) ;
1300
1294
}
1301
1295
}
1302
1296
@@ -1337,7 +1331,13 @@ class Server {
1337
1331
// disabling refreshing on changing the content
1338
1332
if ( this . options . liveReload ) {
1339
1333
watcher . on ( 'change' , ( item ) => {
1340
- this . sendMessage ( this . webSocketConnections , 'static-changed' , item ) ;
1334
+ if ( this . webSocketServer ) {
1335
+ this . sendMessage (
1336
+ this . webSocketServer . clients ,
1337
+ 'static-changed' ,
1338
+ item
1339
+ ) ;
1340
+ }
1341
1341
} ) ;
1342
1342
}
1343
1343
0 commit comments