@@ -1142,9 +1142,6 @@ class Server {
11421142 }
11431143
11441144 createServer ( ) {
1145- const https = require ( "https" ) ;
1146- const http = require ( "http" ) ;
1147-
11481145 if ( this . options . https ) {
11491146 if ( this . options . http2 ) {
11501147 // TODO: we need to replace spdy with http2 which is an internal module
@@ -1158,18 +1155,22 @@ class Server {
11581155 this . app
11591156 ) ;
11601157 } else {
1158+ const https = require ( "https" ) ;
1159+
11611160 this . server = https . createServer ( this . options . https , this . app ) ;
11621161 }
11631162 } else {
1163+ const http = require ( "http" ) ;
1164+
11641165 this . server = http . createServer ( this . app ) ;
11651166 }
11661167
11671168 this . server . on ( "connection" , ( socket ) => {
1168- // add socket to list
1169+ // Add socket to list
11691170 this . sockets . push ( socket ) ;
11701171
11711172 socket . once ( "close" , ( ) => {
1172- // remove socket from list
1173+ // Remove socket from list
11731174 this . sockets . splice ( this . sockets . indexOf ( socket ) , 1 ) ;
11741175 } ) ;
11751176 } ) ;
@@ -1783,40 +1784,42 @@ class Server {
17831784 }
17841785
17851786 async stop ( ) {
1786- if ( this . webSocketProxies . length > 0 ) {
1787- this . webSocketProxies = [ ] ;
1788- }
1787+ this . webSocketProxies = [ ] ;
17891788
1790- if ( this . staticWatchers . length > 0 ) {
1791- await Promise . all ( this . staticWatchers . map ( ( watcher ) => watcher . close ( ) ) ) ;
1789+ await Promise . all ( this . staticWatchers . map ( ( watcher ) => watcher . close ( ) ) ) ;
17921790
1793- this . staticWatchers = [ ] ;
1794- }
1791+ this . staticWatchers = [ ] ;
17951792
17961793 if ( this . webSocketServer ) {
17971794 await new Promise ( ( resolve ) => {
17981795 this . webSocketServer . implementation . close ( ( ) => {
1796+ this . webSocketServer = null ;
1797+
17991798 resolve ( ) ;
18001799 } ) ;
1801- } ) ;
18021800
1803- this . webSocketServer = null ;
1801+ for ( const client of this . webSocketServer . clients ) {
1802+ client . terminate ( ) ;
1803+ }
1804+
1805+ this . webSocketServer . clients = [ ] ;
1806+ } ) ;
18041807 }
18051808
18061809 if ( this . server ) {
18071810 await new Promise ( ( resolve ) => {
1808- this . server . kill ( ( cb ) => {
1809- this . server . stopCallback ( cb ) ;
1810- this . sockets . forEach ( ( socket ) => {
1811- socket . destroy ( ) ;
1812- } ) ;
1813- // reset so the server can be restarted
1814- this . sockets = [ ] ;
1811+ this . server . close ( ( ) => {
1812+ this . server = null ;
1813+
18151814 resolve ( ) ;
18161815 } ) ;
1817- } ) ;
18181816
1819- this . server = null ;
1817+ for ( const socket of this . sockets ) {
1818+ socket . destroy ( ) ;
1819+ }
1820+
1821+ this . sockets = [ ] ;
1822+ } ) ;
18201823
18211824 if ( this . middleware ) {
18221825 await new Promise ( ( resolve , reject ) => {
0 commit comments