@@ -20,10 +20,6 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
20
20
return this . deviceTcpServers [ `${ deviceIdentifier } -${ appId } ` ] ;
21
21
}
22
22
23
- public getWebSocketProxy ( deviceIdentifier : string , appId : string ) : ws . Server {
24
- return this . deviceWebServers [ `${ deviceIdentifier } -${ appId } ` ] ;
25
- }
26
-
27
23
public async addTCPSocketProxy ( device : Mobile . IiOSDevice , appId : string ) : Promise < net . Server > {
28
24
const cacheKey = `${ device . deviceInfo . identifier } -${ appId } ` ;
29
25
const existingServer = this . deviceTcpServers [ cacheKey ] ;
@@ -84,7 +80,17 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
84
80
return server ;
85
81
}
86
82
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 > {
88
94
const cacheKey = `${ device . deviceInfo . identifier } -${ appId } ` ;
89
95
const existingServer = this . deviceWebServers [ cacheKey ] ;
90
96
if ( existingServer ) {
@@ -104,21 +110,23 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
104
110
const server = new ws . Server ( < any > {
105
111
port : localPort ,
106
112
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 ;
108
115
this . $logger . info ( "Frontend client connected." ) ;
109
116
let appDebugSocket ;
110
117
try {
111
118
appDebugSocket = await device . getDebugSocket ( appId ) ;
119
+ this . $logger . info ( "Backend socket created." ) ;
120
+ info . req [ "__deviceSocket" ] = appDebugSocket ;
112
121
} catch ( err ) {
113
122
err . deviceIdentifier = device . deviceInfo . identifier ;
114
123
this . $logger . trace ( err ) ;
115
124
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.` ) ;
117
127
}
118
128
119
- this . $logger . info ( "Backend socket created." ) ;
120
- info . req [ "__deviceSocket" ] = appDebugSocket ;
121
- callback ( true ) ;
129
+ callback ( acceptHandshake ) ;
122
130
}
123
131
} ) ;
124
132
this . deviceWebServers [ cacheKey ] = server ;
@@ -152,12 +160,11 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
152
160
appDebugSocket . on ( "close" , ( ) => {
153
161
this . $logger . info ( "Backend socket closed!" ) ;
154
162
webSocket . close ( ) ;
155
- server . close ( ) ;
156
- delete this . deviceWebServers [ cacheKey ] ;
157
163
} ) ;
158
164
159
165
webSocket . on ( "close" , ( ) => {
160
166
this . $logger . info ( 'Frontend socket closed!' ) ;
167
+ appDebugSocket . unpipe ( packets ) ;
161
168
packets . destroy ( ) ;
162
169
device . destroyDebugSocket ( appId ) ;
163
170
if ( ! this . $options . watch ) {
@@ -167,7 +174,6 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
167
174
168
175
} ) ;
169
176
170
- this . $logger . info ( "Opened localhost " + localPort ) ;
171
177
return server ;
172
178
}
173
179
0 commit comments