Skip to content

Commit eb4b754

Browse files
author
wilinz
committed
1. 添加心跳检测
2. 设备断开连接后关闭日志通道
1 parent 52bc7f2 commit eb4b754

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ All notable changes to the "auto-js-vscodeext-fixed" extension will be documente
44

55
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
66

7-
7+
### 1.109.0
8+
- 添加心跳检测(需要Autox.js 6.2.9 版本以上才会启用)
9+
- 设备断开连接后关闭日志通道
810

911
### 1.108.0
1012

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "auto-js-vsce-fixed",
33
"displayName": "Auto.js-Autox.js-VSCodeExt",
44
"description": "用于在pc上vscode中开发autoxjs的自动化脚本的插件。兼容autojs 和autoxjs,增加功能:usb方式adb进行连接; 右键【文件夹】保存项目到设备;右键【js文件】运行;js文件中点击右上角运行; 结合webpack自动编译,js自动运行到手机",
5-
"version": "1.108.0",
5+
"version": "1.109.0",
66
"publisher": "aaroncheng",
77
"icon": "logo.png",
88
"repository": {

src/autojs-debug.ts

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Tracker from '@devicefarmer/adbkit/dist/src/adb/tracker';
1111
import ADBDevice from '@devicefarmer/adbkit/dist/src/Device';
1212
import internal from "stream";
1313
import buffer from "buffer";
14-
14+
import { _context } from "./extension";
1515
const DEBUG = false;
1616

1717
function 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
}

src/extension.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ class Extension {
443443
}
444444

445445

446-
let _context: any;
446+
export let _context: vscode.ExtensionContext;
447447
let extension = new Extension();
448448
const commands = ['startAllServer', 'stopAllServer', 'startServer', 'stopServer', 'startTrackADBDevices',
449449
'stopTrackADBDevices', 'manuallyConnectADB', 'manuallyDisconnect', 'showServerAddress', 'showQrCode', 'openDocument', 'run', 'runOnDevice',
@@ -455,6 +455,8 @@ export function activate(context: vscode.ExtensionContext) {
455455
let action: Function = extension[command];
456456
context.subscriptions.push(vscode.commands.registerCommand('extension.' + command, action.bind(extension)));
457457
_context = context;
458+
// @ts-ignore
459+
console.log(context.extension.packageJSON.version)
458460
})
459461
}
460462

0 commit comments

Comments
 (0)