diff --git a/lib/declarations.d.ts b/lib/declarations.d.ts index c069049d4b..5f948e84b6 100644 --- a/lib/declarations.d.ts +++ b/lib/declarations.d.ts @@ -739,8 +739,7 @@ interface IAppDebugSocketProxyFactory extends NodeJS.EventEmitter { getTCPSocketProxy(deviceIdentifier: string, appId: string): any; addTCPSocketProxy(device: Mobile.IiOSDevice, appId: string): Promise; - getWebSocketProxy(deviceIdentifier: string, appId: string): any; - addWebSocketProxy(device: Mobile.IiOSDevice, appId: string): Promise; + ensureWebSocketProxy(device: Mobile.IiOSDevice, appId: string): Promise; removeAllProxies(): void; } diff --git a/lib/device-sockets/ios/app-debug-socket-proxy-factory.ts b/lib/device-sockets/ios/app-debug-socket-proxy-factory.ts index 7d932301f0..7e8f412728 100644 --- a/lib/device-sockets/ios/app-debug-socket-proxy-factory.ts +++ b/lib/device-sockets/ios/app-debug-socket-proxy-factory.ts @@ -20,10 +20,6 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu return this.deviceTcpServers[`${deviceIdentifier}-${appId}`]; } - public getWebSocketProxy(deviceIdentifier: string, appId: string): ws.Server { - return this.deviceWebServers[`${deviceIdentifier}-${appId}`]; - } - public async addTCPSocketProxy(device: Mobile.IiOSDevice, appId: string): Promise { const cacheKey = `${device.deviceInfo.identifier}-${appId}`; const existingServer = this.deviceTcpServers[cacheKey]; @@ -84,7 +80,17 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu return server; } - public async addWebSocketProxy(device: Mobile.IiOSDevice, appId: string): Promise { + public async ensureWebSocketProxy(device: Mobile.IiOSDevice, appId: string): Promise { + const existingWebProxy = this.deviceWebServers[`${device.deviceInfo.identifier}-${appId}`]; + const result = existingWebProxy || await this.addWebSocketProxy(device, appId); + + // TODO: do not remove till VSCode waits for this message in order to reattach + this.$logger.info("Opened localhost " + result.options.port); + + return result; + } + + private async addWebSocketProxy(device: Mobile.IiOSDevice, appId: string): Promise { const cacheKey = `${device.deviceInfo.identifier}-${appId}`; const existingServer = this.deviceWebServers[cacheKey]; if (existingServer) { @@ -152,12 +158,11 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu appDebugSocket.on("close", () => { this.$logger.info("Backend socket closed!"); webSocket.close(); - server.close(); - delete this.deviceWebServers[cacheKey]; }); webSocket.on("close", () => { this.$logger.info('Frontend socket closed!'); + appDebugSocket.unpipe(packets); packets.destroy(); device.destroyDebugSocket(appId); if (!this.$options.watch) { @@ -167,7 +172,6 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu }); - this.$logger.info("Opened localhost " + localPort); return server; } diff --git a/lib/services/ios-device-debug-service.ts b/lib/services/ios-device-debug-service.ts index 88d6b481c9..ed711ff184 100644 --- a/lib/services/ios-device-debug-service.ts +++ b/lib/services/ios-device-debug-service.ts @@ -164,8 +164,7 @@ export class IOSDeviceDebugService extends DebugServiceBase implements IDeviceDe if (debugOptions.chrome) { this.$logger.info("'--chrome' is the default behavior. Use --inspector to debug iOS applications using the Safari Web Inspector."); } - const existingWebProxy = this.$appDebugSocketProxyFactory.getWebSocketProxy(this.deviceIdentifier, debugData.applicationIdentifier); - const webSocketProxy = existingWebProxy || await this.$appDebugSocketProxyFactory.addWebSocketProxy(this.device, debugData.applicationIdentifier); + const webSocketProxy = await this.$appDebugSocketProxyFactory.ensureWebSocketProxy(this.device, debugData.applicationIdentifier); return this.getChromeDebugUrl(debugOptions, webSocketProxy.options.port); }