Skip to content

Commit

Permalink
fix(server): disconnect ws clients on server close (#6215)
Browse files Browse the repository at this point in the history
  • Loading branch information
yury-s committed Apr 17, 2021
1 parent e4ae650 commit 17ead28
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/remote/playwrightServer.ts
Expand Up @@ -33,7 +33,7 @@ export interface PlaywrightServerDelegate {
}

export class PlaywrightServer {
private _server: http.Server | undefined;
private _wsServer: ws.Server | undefined;
private _clientsCount = 0;
private _delegate: PlaywrightServerDelegate;

Expand Down Expand Up @@ -74,11 +74,10 @@ export class PlaywrightServer {
});
});

this._server = server;
debugLog('Listening at ' + wsEndpoint);

const wsServer = new ws.Server({ server: this._server, path });
wsServer.on('connection', async socket => {
this._wsServer = new ws.Server({ server, path });
this._wsServer.on('connection', async socket => {
if (this._clientsCount && !this._delegate.allowMultipleClients) {
socket.close();
return;
Expand Down Expand Up @@ -117,10 +116,12 @@ export class PlaywrightServer {
}

async close() {
if (!this._server)
if (!this._wsServer)
return;
debugLog('Closing server');
await new Promise(f => this._server!.close(f));
// First disconnect all remaining clients.
await new Promise(f => this._wsServer!.close(f));
await new Promise(f => this._wsServer!.options.server!.close(f));
await this._delegate.onClose();
}
}

0 comments on commit 17ead28

Please sign in to comment.