@@ -11,6 +11,7 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
1111
1212 constructor ( private $logger : ILogger ,
1313 private $errors : IErrors ,
14+ private $lockService : ILockService ,
1415 private $options : IOptions ,
1516 private $net : INet ) {
1617 super ( ) ;
@@ -54,9 +55,9 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
5455 }
5556 } ) ;
5657
57- frontendSocket . on ( "close" , ( ) => {
58+ frontendSocket . on ( "close" , async ( ) => {
5859 this . $logger . info ( "Frontend socket closed" ) ;
59- device . destroyDebugSocket ( appId ) ;
60+ await device . destroyDebugSocket ( appId ) ;
6061 } ) ;
6162
6263 appDebugSocket . on ( "close" , ( ) => {
@@ -91,6 +92,7 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
9192 }
9293
9394 private async addWebSocketProxy ( device : Mobile . IiOSDevice , appId : string , projectName : string ) : Promise < ws . Server > {
95+ const clientConnectionLockFile = `debug-connection-${ device . deviceInfo . identifier } -${ appId } .lock` ;
9496 const cacheKey = `${ device . deviceInfo . identifier } -${ appId } ` ;
9597 const existingServer = this . deviceWebServers [ cacheKey ] ;
9698 if ( existingServer ) {
@@ -107,22 +109,37 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
107109 // We store the socket that connects us to the device in the upgrade request object itself and later on retrieve it
108110 // in the connection callback.
109111
112+ let currentAppSocket : net . Socket = null ;
113+ let currentWebSocket : ws = null ;
110114 const server = new ws . Server ( < any > {
111115 port : localPort ,
112116 host : "localhost" ,
113117 verifyClient : async ( info : any , callback : ( res : boolean , code ?: number , message ?: string ) => void ) => {
118+ await this . $lockService . lock ( clientConnectionLockFile ) ;
114119 let acceptHandshake = true ;
115120 this . $logger . info ( "Frontend client connected." ) ;
116121 let appDebugSocket ;
117122 try {
123+ if ( currentAppSocket ) {
124+ currentAppSocket . removeAllListeners ( ) ;
125+ currentAppSocket = null ;
126+ if ( currentWebSocket ) {
127+ currentWebSocket . removeAllListeners ( ) ;
128+ currentWebSocket . close ( ) ;
129+ currentWebSocket = null ;
130+ }
131+ await device . destroyDebugSocket ( appId ) ;
132+ }
118133 appDebugSocket = await device . getDebugSocket ( appId , projectName ) ;
134+ currentAppSocket = appDebugSocket ;
119135 this . $logger . info ( "Backend socket created." ) ;
120136 info . req [ "__deviceSocket" ] = appDebugSocket ;
121137 } catch ( err ) {
122138 err . deviceIdentifier = device . deviceInfo . identifier ;
123139 this . $logger . trace ( err ) ;
124140 this . emit ( CONNECTION_ERROR_EVENT_NAME , err ) ;
125141 acceptHandshake = false ;
142+ this . $lockService . unlock ( clientConnectionLockFile ) ;
126143 this . $logger . warn ( `Cannot connect to device socket. The error message is '${ err . message } '.` ) ;
127144 }
128145
@@ -131,6 +148,7 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
131148 } ) ;
132149 this . deviceWebServers [ cacheKey ] = server ;
133150 server . on ( "connection" , ( webSocket , req ) => {
151+ currentWebSocket = webSocket ;
134152 const encoding = "utf16le" ;
135153
136154 const appDebugSocket : net . Socket = ( < any > req ) [ "__deviceSocket" ] ;
@@ -163,20 +181,23 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
163181 } ) ;
164182
165183 appDebugSocket . on ( "close" , ( ) => {
184+ currentAppSocket = null ;
166185 this . $logger . info ( "Backend socket closed!" ) ;
167186 webSocket . close ( ) ;
168187 } ) ;
169188
170- webSocket . on ( "close" , ( ) => {
189+ webSocket . on ( "close" , async ( ) => {
190+ currentWebSocket = null ;
171191 this . $logger . info ( 'Frontend socket closed!' ) ;
172192 appDebugSocket . unpipe ( packets ) ;
173193 packets . destroy ( ) ;
174- device . destroyDebugSocket ( appId ) ;
194+ await device . destroyDebugSocket ( appId ) ;
175195 if ( ! this . $options . watch ) {
176196 process . exit ( 0 ) ;
177197 }
178198 } ) ;
179199
200+ this . $lockService . unlock ( clientConnectionLockFile ) ;
180201 } ) ;
181202
182203 return server ;
0 commit comments