From 8323da9036c68e5bbb924aa3878d633c3abb2fe5 Mon Sep 17 00:00:00 2001 From: nekosu Date: Sun, 20 Aug 2023 21:48:58 +0800 Subject: [PATCH] feat: reconnection. BRUTE FORCE --- .../src/components/framework/MonitorView.vue | 10 +++++++--- packages/server/src/index.ts | 19 +++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/packages/client/src/components/framework/MonitorView.vue b/packages/client/src/components/framework/MonitorView.vue index ba4206c..cef2848 100644 --- a/packages/client/src/components/framework/MonitorView.vue +++ b/packages/client/src/components/framework/MonitorView.vue @@ -29,10 +29,14 @@ async function tryConnect() { } socket = await api.controller() socket.onclose = () => { + console.log('close!') socket = null + setTimeout(() => { + tryConnect() + }, 100) } socket.onmessage = async ev => { - console.log('got image') + // console.log('got image') fr += 1 const cur = fr @@ -43,11 +47,11 @@ async function tryConnect() { const image = new Image(1280, 720) image.onload = () => { if (cur < prefr) { - console.log('skip image') + // console.log('skip image') return } prefr = cur - console.log('draw image') + // console.log('draw image') ctx.drawImage(image, 0, 0, 1280, 720, 0, 0, props.width, props.height) } image.src = url diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index 4bb75f1..19fb7ec 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -123,11 +123,18 @@ async function main() { let pending = false let quit = false + let skipCount = 0 + const timer = setInterval(async () => { if (controller) { - console.log(`/api/controller ${id} start push`) + // console.log(`/api/controller ${id} start push`) if (pending) { - console.log(`/api/controller ${id} skipped`) + // console.log(`/api/controller ${id} skipped`) + skipCount += 1 + if (skipCount > 10) { + console.log(`/api/controller ${id} skip too much`) + ws.close() + } return } pending = true @@ -138,19 +145,19 @@ async function main() { if (quit) { return } - console.log(`/api/controller ${id} get image`) + // console.log(`/api/controller ${id} get image`) const buffer = controller.image() pending = false if (buffer) { - console.log(`/api/controller ${id} push image`) + // console.log(`/api/controller ${id} push image`) ws.send(buffer) } } else { - console.log(`/api/controller ${id} screencap failed`) + // console.log(`/api/controller ${id} screencap failed`) pending = false } } - }, 500) + }, 100) ws.on('close', () => { quit = true console.log(`/api/controller ${id} closed`)