@@ -11,7 +11,7 @@ import Tracker from '@devicefarmer/adbkit/dist/src/adb/tracker';
1111import ADBDevice from '@devicefarmer/adbkit/dist/src/Device' ;
1212import internal from "stream" ;
1313import buffer from "buffer" ;
14-
14+ import { _context } from "./extension" ;
1515const DEBUG = false ;
1616
1717function logDebug ( message ?: any , ...optionalParams : any [ ] ) {
@@ -42,11 +42,26 @@ export class Device extends EventEmitter {
4242 this . attached = true ;
4343 this . name = data [ 'device_name' ] ;
4444 let message_id = `${ Date . now ( ) } _${ Math . random ( ) } ` ;
45- var returnData = JSON . stringify ( { message_id, data : "连接成功" , debug : true , type : 'hello' } )
46- logDebug ( "返回消息:" , returnData )
45+ let appVersionCode = data [ 'app_version_code' ]
46+ // @ts -ignore
47+ let extensionVersion = _context . extension . packageJSON . version
48+ let returnData
49+ if ( appVersionCode >= 629 ) {
50+ returnData = JSON . stringify ( { message_id, data : "ok" , version : extensionVersion , debug : DEBUG , type : 'hello' } )
51+ } else {
52+ returnData = JSON . stringify ( { message_id, data : "连接成功" , debug : DEBUG , type : 'hello' } )
53+ }
54+ logDebug ( "return data: " , returnData )
4755 this . connection . sendUTF ( returnData ) ;
4856 this . emit ( "attach" , this ) ;
4957 } ) ;
58+ this . on ( 'data:ping' , data => {
59+ logDebug ( "on client ping: " , data ) ;
60+ let message_id = `${ Date . now ( ) } _${ Math . random ( ) } ` ;
61+ var returnData = JSON . stringify ( { type : 'pong' , data : data } )
62+ logDebug ( "pong: " , returnData )
63+ this . connection . sendUTF ( returnData ) ;
64+ } )
5065 setTimeout ( ( ) => {
5166 if ( ! this . attached ) {
5267 console . log ( "handshake timeout" ) ;
@@ -161,7 +176,7 @@ export class AutoJsDebugServer extends EventEmitter {
161176 response . end ( ) ;
162177 }
163178 } ) ;
164- let wsServer = new ws . server ( { httpServer : this . httpServer } ) ;
179+ let wsServer = new ws . server ( { httpServer : this . httpServer , keepalive : true , keepaliveInterval : 10000 } ) ;
165180 wsServer . on ( 'request' , request => {
166181 let connection = request . accept ( ) ;
167182 if ( ! connection ) {
@@ -180,16 +195,18 @@ export class AutoJsDebugServer extends EventEmitter {
180195 private newDevice ( connection : ws . connection , type : string , id : string ) {
181196 let device = new Device ( connection , type , id ) ;
182197 logDebug ( connection . state , "--->status" )
183- device . on ( "attach" , ( device ) => {
184- this . attachDevice ( device ) ;
185- this . emit ( 'new_device' , device ) ;
186- let logChannel = this . newLogChannel ( device ) ;
187- logChannel . appendLine ( `Device connected: ${ device } ` ) ;
188- } )
198+ device
199+ . on ( "attach" , ( device ) => {
200+ this . attachDevice ( device ) ;
201+ this . emit ( 'new_device' , device ) ;
202+ let logChannel = this . newLogChannel ( device ) ;
203+ logChannel . appendLine ( `Device connected: ${ device } ` ) ;
204+ } )
189205 }
190206
191207 async adbShell ( device : DeviceClient , command : string ) : Promise < string > {
192208 let duplex : internal . Duplex = await device . shell ( command )
209+ // @ts -ignore
193210 let brandBuf : buffer . Buffer = await Adb . util . readAll ( duplex )
194211 return brandBuf . toString ( )
195212 }
@@ -415,25 +432,27 @@ export class AutoJsDebugServer extends EventEmitter {
415432 this . devices . splice ( this . devices . indexOf ( device ) , 1 ) ;
416433 console . log ( "detachDevice: " + device ) ;
417434 vscode . window . showInformationMessage ( `Device disconnected: ${ device } ` )
418- this . getLogChannel ( device ) . appendLine ( `Device disconnected: ${ device } ` ) ;
435+ var logChannel = this . getLogChannel ( device )
436+ logChannel . dispose ( ) ;
437+ this . logChannels . delete ( device . toString ( ) )
419438 }
420439
421440 /** 创建设备日志打印通道 */
422441 private newLogChannel ( device : Device ) : vscode . OutputChannel {
423- let channelName = ` ${ device } ` ;
424- let logChannel = this . logChannels . get ( channelName ) ;
425- if ( ! logChannel ) {
426- logChannel = vscode . window . createOutputChannel ( channelName ) ;
427- this . logChannels . set ( channelName , logChannel ) ;
428- }
442+ let channelName = device . toString ( ) ;
443+ // let logChannel = this.logChannels.get(channelName);
444+ // if (!logChannel) {
445+ let logChannel = vscode . window . createOutputChannel ( channelName ) ;
446+ this . logChannels . set ( channelName , logChannel ) ;
447+ // }
429448 logChannel . show ( true ) ;
430449 // console.log("创建日志通道" + channelName)
431450 return logChannel ;
432451 }
433452
434453 /** 获取设备日志打印通道 */
435454 private getLogChannel ( device : Device ) : vscode . OutputChannel {
436- let channelName = ` ${ device } ` ;
455+ let channelName = device . toString ( ) ;
437456 // console.log("获取日志通道:" + channelName);
438457 return this . logChannels . get ( channelName ) ;
439458 }
0 commit comments