Fixed: fixed bug scrcpy and shell is disconnect when switch page

This commit is contained in:
Kenta420 2024-02-19 14:24:05 +07:00
parent 9543d4541c
commit 0fe469b5c6
43 changed files with 1378 additions and 1366 deletions

View file

@ -0,0 +1,62 @@
import { type BrowserWindow } from 'electron/main'
import { AdbServerNodeTcpConnector } from '@yume-chan/adb-server-node-tcp'
import type { AdbServerDevice, AdbServerTransport } from '@yume-chan/adb'
import { Adb, AdbServerClient } from '@yume-chan/adb'
import { DecodeUtf8Stream, WritableStream } from '@yume-chan/stream-extra'
let adb: Adb | undefined
export function AdbDaemon(_win: BrowserWindow | null, ipcMain: Electron.IpcMain) {
ipcMain.handle('adb', async () => {
await createConnection()
})
ipcMain.handle('adb:shell', async (event, command: string) => {
if (!adb) {
return
}
const process = await adb.subprocess.shell(command)
let result: string | null = null
await process.stdout.pipeThrough(new DecodeUtf8Stream()).pipeTo(
new WritableStream({
write(chunk) {
result += chunk
}
})
)
return result
})
}
async function createConnection() {
const connector: AdbServerNodeTcpConnector = new AdbServerNodeTcpConnector({
host: 'localhost',
port: 5037
})
console.log('Connecting to ADB server...')
connector.connect()
const client: AdbServerClient = new AdbServerClient(connector)
const devices: AdbServerDevice[] = await client.getDevices()
if (devices.length === 0) {
console.log('No device found')
return
}
console.log('Devices found:', devices.map(device => device.serial).join(', '))
const device: AdbServerDevice | undefined = devices.find(device => device.serial === 'd')
if (!device) {
console.log('No device found')
return
}
console.log('Device found:', device.serial)
const transport: AdbServerTransport = await client.createTransport(device)
adb = new Adb(transport)
}