From d7765fb5dc1dc62ba386fbd38c9c61ad84564e1a Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Sat, 30 Jul 2022 19:28:23 +0200 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ts/smartproxy.classes.networkproxy.ts | 62 ++++++++++++++------------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index b133f9c..ff32d2d 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@pushrocks/smartproxy', - version: '3.0.20', + version: '3.0.21', description: 'a proxy for handling high workloads of proxying' } diff --git a/ts/smartproxy.classes.networkproxy.ts b/ts/smartproxy.classes.networkproxy.ts index ad266ff..6be88f5 100644 --- a/ts/smartproxy.classes.networkproxy.ts +++ b/ts/smartproxy.classes.networkproxy.ts @@ -234,40 +234,44 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g= const wss = new plugins.ws.WebSocketServer({ server: this.httpsServer }); wss.on('connection', (ws: plugins.wsDefault) => { console.log('got connection for wsc'); - const wscConnected = plugins.smartpromise.defer(); - - const wsc = new plugins.wsDefault(this.router.routeWs(ws), { - headers: { - Host: ws.url, - }, - }); - - wsc.on('open', () => { - wscConnected.resolve(); - }); + let wscConnectedDeferred: plugins.smartpromise.Deferred; ws.on('message', async (message) => { - await wscConnected.promise; + if (!wscConnectedDeferred) { + wscConnectedDeferred = plugins.smartpromise.defer(); + const wsc = new plugins.wsDefault(this.router.routeWs(ws), { + headers: { + Host: ws.url, + }, + }); + + wsc.on('open', () => { + wscConnectedDeferred.resolve(wsc); + }); + } + const wsc = await wscConnectedDeferred.promise; wsc.emit('message', message); - }); - wsc.on('message', (message) => { - ws.emit('message', message); - }); + wsc.on('message', (message) => { + ws.emit('message', message); + }); + + // handle closing + const cleanUp = () => { + ws.close(); + ws.terminate(); + wsc.close(); + wsc.terminate(); + }; + + ws.on('close', (message) => { + cleanUp(); + }); - // handle closing - const cleanUp = () => { - ws.close(); - ws.terminate(); - wsc.close(); - wsc.terminate(); - }; - - ws.on('close', (message) => { - cleanUp(); - }); - wsc.on('close', (message) => { - cleanUp(); + wsc.on('close', (message) => { + cleanUp(); + }); }); + }); this.httpsServer.keepAliveTimeout = 61000; this.httpsServer.headersTimeout = 65000;