Skip to content

Commit 294e155

Browse files
refactor: listen method (#1884)
1 parent c535bb2 commit 294e155

File tree

1 file changed

+104
-101
lines changed

1 file changed

+104
-101
lines changed

lib/Server.js

Lines changed: 104 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,110 @@ class Server {
675675
}
676676
}
677677

678+
createSocketServer() {
679+
const socket = sockjs.createServer({
680+
// Use provided up-to-date sockjs-client
681+
sockjs_url: '/__webpack_dev_server__/sockjs.bundle.js',
682+
// Limit useless logs
683+
log: (severity, line) => {
684+
if (severity === 'error') {
685+
this.log.error(line);
686+
} else {
687+
this.log.debug(line);
688+
}
689+
},
690+
});
691+
692+
socket.on('connection', (connection) => {
693+
if (!connection) {
694+
return;
695+
}
696+
697+
if (
698+
!this.checkHost(connection.headers) ||
699+
!this.checkOrigin(connection.headers)
700+
) {
701+
this.sockWrite([connection], 'error', 'Invalid Host/Origin header');
702+
703+
connection.close();
704+
705+
return;
706+
}
707+
708+
this.sockets.push(connection);
709+
710+
connection.on('close', () => {
711+
const idx = this.sockets.indexOf(connection);
712+
713+
if (idx >= 0) {
714+
this.sockets.splice(idx, 1);
715+
}
716+
});
717+
718+
if (this.hot) {
719+
this.sockWrite([connection], 'hot');
720+
}
721+
722+
if (this.progress) {
723+
this.sockWrite([connection], 'progress', this.progress);
724+
}
725+
726+
if (this.clientOverlay) {
727+
this.sockWrite([connection], 'overlay', this.clientOverlay);
728+
}
729+
730+
if (this.clientLogLevel) {
731+
this.sockWrite([connection], 'log-level', this.clientLogLevel);
732+
}
733+
734+
if (!this._stats) {
735+
return;
736+
}
737+
738+
this._sendStats([connection], this.getStats(this._stats), true);
739+
});
740+
741+
socket.installHandlers(this.listeningApp, {
742+
prefix: this.sockPath,
743+
});
744+
}
745+
746+
listen(port, hostname, fn) {
747+
this.hostname = hostname;
748+
749+
return this.listeningApp.listen(port, hostname, (err) => {
750+
this.createSocketServer();
751+
752+
if (this.options.bonjour) {
753+
runBonjour(this.options);
754+
}
755+
756+
this.showStatus();
757+
758+
if (fn) {
759+
fn.call(this.listeningApp, err);
760+
}
761+
});
762+
}
763+
764+
close(cb) {
765+
this.sockets.forEach((socket) => {
766+
socket.close();
767+
});
768+
769+
this.sockets = [];
770+
771+
this.contentBaseWatchers.forEach((watcher) => {
772+
watcher.close();
773+
});
774+
775+
this.contentBaseWatchers = [];
776+
777+
this.listeningApp.kill(() => {
778+
this.middleware.close(cb);
779+
});
780+
}
781+
678782
static get DEFAULT_STATS() {
679783
return {
680784
all: false,
@@ -817,107 +921,6 @@ class Server {
817921
);
818922
}
819923

820-
// delegate listen call and init sockjs
821-
listen(port, hostname, fn) {
822-
this.hostname = hostname;
823-
824-
return this.listeningApp.listen(port, hostname, (err) => {
825-
const socket = sockjs.createServer({
826-
// Use provided up-to-date sockjs-client
827-
sockjs_url: '/__webpack_dev_server__/sockjs.bundle.js',
828-
// Limit useless logs
829-
log: (severity, line) => {
830-
if (severity === 'error') {
831-
this.log.error(line);
832-
} else {
833-
this.log.debug(line);
834-
}
835-
},
836-
});
837-
838-
socket.on('connection', (connection) => {
839-
if (!connection) {
840-
return;
841-
}
842-
843-
if (
844-
!this.checkHost(connection.headers) ||
845-
!this.checkOrigin(connection.headers)
846-
) {
847-
this.sockWrite([connection], 'error', 'Invalid Host/Origin header');
848-
849-
connection.close();
850-
851-
return;
852-
}
853-
854-
this.sockets.push(connection);
855-
856-
connection.on('close', () => {
857-
const idx = this.sockets.indexOf(connection);
858-
859-
if (idx >= 0) {
860-
this.sockets.splice(idx, 1);
861-
}
862-
});
863-
864-
if (this.hot) {
865-
this.sockWrite([connection], 'hot');
866-
}
867-
868-
if (this.progress) {
869-
this.sockWrite([connection], 'progress', this.progress);
870-
}
871-
872-
if (this.clientOverlay) {
873-
this.sockWrite([connection], 'overlay', this.clientOverlay);
874-
}
875-
876-
if (this.clientLogLevel) {
877-
this.sockWrite([connection], 'log-level', this.clientLogLevel);
878-
}
879-
880-
if (!this._stats) {
881-
return;
882-
}
883-
884-
this._sendStats([connection], this.getStats(this._stats), true);
885-
});
886-
887-
socket.installHandlers(this.listeningApp, {
888-
prefix: this.sockPath,
889-
});
890-
891-
if (this.options.bonjour) {
892-
runBonjour(this.options);
893-
}
894-
895-
this.showStatus();
896-
897-
if (fn) {
898-
fn.call(this.listeningApp, err);
899-
}
900-
});
901-
}
902-
903-
close(cb) {
904-
this.sockets.forEach((socket) => {
905-
socket.close();
906-
});
907-
908-
this.sockets = [];
909-
910-
this.contentBaseWatchers.forEach((watcher) => {
911-
watcher.close();
912-
});
913-
914-
this.contentBaseWatchers = [];
915-
916-
this.listeningApp.kill(() => {
917-
this.middleware.close(cb);
918-
});
919-
}
920-
921924
// eslint-disable-next-line
922925
sockWrite(sockets, type, data) {
923926
sockets.forEach((socket) => {

0 commit comments

Comments
 (0)