1
1
'use strict' ;
2
2
3
3
/* eslint no-console: off, func-names: off */
4
+ require ( './polyfills' ) ;
4
5
5
6
const fs = require ( 'fs' ) ;
6
7
const http = require ( 'http' ) ;
@@ -22,8 +23,6 @@ const webpackDevMiddleware = require('webpack-dev-middleware');
22
23
const OptionsValidationError = require ( './OptionsValidationError' ) ;
23
24
const optionsSchema = require ( './optionsSchema.json' ) ;
24
25
25
- require ( './polyfills' ) ;
26
-
27
26
const clientStats = { errorDetails : false } ;
28
27
29
28
function Server ( compiler , options ) {
@@ -483,49 +482,58 @@ Server.prototype.checkHost = function (headers) {
483
482
} ;
484
483
485
484
// delegate listen call and init sockjs
486
- Server . prototype . listen = function ( port , hostname ) {
485
+ Server . prototype . listen = function ( port , hostname , fn ) {
487
486
this . listenHostname = hostname ;
488
487
// eslint-disable-next-line
489
- const returnValue = this . listeningApp . listen . apply ( this . listeningApp , arguments ) ;
490
- const sockServer = sockjs . createServer ( {
491
- // Use provided up-to-date sockjs-client
492
- sockjs_url : '/__webpack_dev_server__/sockjs.bundle.js' ,
493
- // Limit useless logs
494
- log ( severity , line ) {
495
- if ( severity === 'error' ) {
496
- console . log ( line ) ;
497
- }
498
- }
499
- } ) ;
500
- sockServer . on ( 'connection' , ( conn ) => {
501
- if ( ! conn ) return ;
502
- if ( ! this . checkHost ( conn . headers ) ) {
503
- this . sockWrite ( [ conn ] , 'error' , 'Invalid Host header' ) ;
504
- conn . close ( ) ;
505
- return ;
506
- }
507
- this . sockets . push ( conn ) ;
508
488
509
- conn . on ( 'close' , ( ) => {
510
- const connIndex = this . sockets . indexOf ( conn ) ;
511
- if ( connIndex >= 0 ) {
512
- this . sockets . splice ( connIndex , 1 ) ;
489
+ const returnValue = this . listeningApp . listen ( port , hostname , ( err ) => {
490
+ const sockServer = sockjs . createServer ( {
491
+ // Use provided up-to-date sockjs-client
492
+ sockjs_url : '/__webpack_dev_server__/sockjs.bundle.js' ,
493
+ // Limit useless logs
494
+ log ( severity , line ) {
495
+ if ( severity === 'error' ) {
496
+ console . log ( line ) ;
497
+ }
513
498
}
514
499
} ) ;
515
500
516
- if ( this . clientLogLevel ) { this . sockWrite ( [ conn ] , 'log-level' , this . clientLogLevel ) ; }
501
+ sockServer . on ( 'connection' , ( conn ) => {
502
+ console . log ( 'connection.remotePort' , conn . remotePort ) ;
503
+ if ( ! conn ) return ;
504
+ if ( ! this . checkHost ( conn . headers ) ) {
505
+ this . sockWrite ( [ conn ] , 'error' , 'Invalid Host header' ) ;
506
+ conn . close ( ) ;
507
+ return ;
508
+ }
509
+ this . sockets . push ( conn ) ;
517
510
518
- if ( this . clientOverlay ) { this . sockWrite ( [ conn ] , 'overlay' , this . clientOverlay ) ; }
511
+ conn . on ( 'close' , ( ) => {
512
+ const connIndex = this . sockets . indexOf ( conn ) ;
513
+ if ( connIndex >= 0 ) {
514
+ this . sockets . splice ( connIndex , 1 ) ;
515
+ }
516
+ } ) ;
519
517
520
- if ( this . hot ) this . sockWrite ( [ conn ] , 'hot' ) ;
518
+ if ( this . clientLogLevel ) { this . sockWrite ( [ conn ] , 'log-level' , this . clientLogLevel ) ; }
521
519
522
- if ( ! this . _stats ) return ;
523
- this . _sendStats ( [ conn ] , this . _stats . toJson ( clientStats ) , true ) ;
524
- } ) ;
520
+ if ( this . clientOverlay ) { this . sockWrite ( [ conn ] , 'overlay' , this . clientOverlay ) ; }
521
+
522
+ if ( this . hot ) this . sockWrite ( [ conn ] , 'hot' ) ;
523
+
524
+ if ( ! this . _stats ) return ;
525
+ this . _sendStats ( [ conn ] , this . _stats . toJson ( clientStats ) , true ) ;
526
+ } ) ;
525
527
526
- sockServer . installHandlers ( this . listeningApp , {
527
- prefix : '/sockjs-node'
528
+ sockServer . installHandlers ( this . listeningApp , {
529
+ prefix : '/sockjs-node'
530
+ } ) ;
531
+
532
+ if ( fn ) {
533
+ fn . call ( this . listeningApp , err ) ;
534
+ }
528
535
} ) ;
536
+
529
537
return returnValue ;
530
538
} ;
531
539
0 commit comments