@@ -20,10 +20,6 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
2020 return this . deviceTcpServers [ `${ deviceIdentifier } -${ appId } ` ] ;
2121 }
2222
23- public getWebSocketProxy ( deviceIdentifier : string , appId : string ) : ws . Server {
24- return this . deviceWebServers [ `${ deviceIdentifier } -${ appId } ` ] ;
25- }
26-
2723 public async addTCPSocketProxy ( device : Mobile . IiOSDevice , appId : string ) : Promise < net . Server > {
2824 const cacheKey = `${ device . deviceInfo . identifier } -${ appId } ` ;
2925 const existingServer = this . deviceTcpServers [ cacheKey ] ;
@@ -84,7 +80,17 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
8480 return server ;
8581 }
8682
87- public async addWebSocketProxy ( device : Mobile . IiOSDevice , appId : string ) : Promise < ws . Server > {
83+ public async ensureWebSocketProxy ( device : Mobile . IiOSDevice , appId : string ) : Promise < ws . Server > {
84+ const existingWebProxy = this . deviceWebServers [ `${ device . deviceInfo . identifier } -${ appId } ` ] ;
85+ const result = existingWebProxy || await this . addWebSocketProxy ( device , appId ) ;
86+
87+ // TODO: do not remove till VSCode waits for this message in order to reattach
88+ this . $logger . info ( "Opened localhost " + result . options . port ) ;
89+
90+ return result ;
91+ }
92+
93+ private async addWebSocketProxy ( device : Mobile . IiOSDevice , appId : string ) : Promise < ws . Server > {
8894 const cacheKey = `${ device . deviceInfo . identifier } -${ appId } ` ;
8995 const existingServer = this . deviceWebServers [ cacheKey ] ;
9096 if ( existingServer ) {
@@ -104,21 +110,23 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
104110 const server = new ws . Server ( < any > {
105111 port : localPort ,
106112 host : "localhost" ,
107- verifyClient : async ( info : any , callback : Function ) => {
113+ verifyClient : async ( info : any , callback : ( res : boolean , code ?: number , message ?: string ) => void ) => {
114+ let acceptHandshake = true ;
108115 this . $logger . info ( "Frontend client connected." ) ;
109116 let appDebugSocket ;
110117 try {
111118 appDebugSocket = await device . getDebugSocket ( appId ) ;
119+ this . $logger . info ( "Backend socket created." ) ;
120+ info . req [ "__deviceSocket" ] = appDebugSocket ;
112121 } catch ( err ) {
113122 err . deviceIdentifier = device . deviceInfo . identifier ;
114123 this . $logger . trace ( err ) ;
115124 this . emit ( CONNECTION_ERROR_EVENT_NAME , err ) ;
116- this . $errors . failWithoutHelp ( `Cannot connect to device socket.The error message is ${ err . message } ` ) ;
125+ acceptHandshake = false ;
126+ this . $logger . warn ( `Cannot connect to device socket. The error message is '${ err . message } '. Try starting the application manually.` ) ;
117127 }
118128
119- this . $logger . info ( "Backend socket created." ) ;
120- info . req [ "__deviceSocket" ] = appDebugSocket ;
121- callback ( true ) ;
129+ callback ( acceptHandshake ) ;
122130 }
123131 } ) ;
124132 this . deviceWebServers [ cacheKey ] = server ;
@@ -152,12 +160,11 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
152160 appDebugSocket . on ( "close" , ( ) => {
153161 this . $logger . info ( "Backend socket closed!" ) ;
154162 webSocket . close ( ) ;
155- server . close ( ) ;
156- delete this . deviceWebServers [ cacheKey ] ;
157163 } ) ;
158164
159165 webSocket . on ( "close" , ( ) => {
160166 this . $logger . info ( 'Frontend socket closed!' ) ;
167+ appDebugSocket . unpipe ( packets ) ;
161168 packets . destroy ( ) ;
162169 device . destroyDebugSocket ( appId ) ;
163170 if ( ! this . $options . watch ) {
@@ -167,7 +174,6 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
167174
168175 } ) ;
169176
170- this . $logger . info ( "Opened localhost " + localPort ) ;
171177 return server ;
172178 }
173179
0 commit comments