mirror of
https://github.com/viarotel-org/escrcpy.git
synced 2025-02-22 04:49:42 +01:00
perf: ⚡️ Add webSocket
This commit is contained in:
parent
5ac5ee6e97
commit
ae96df03d4
@ -1,5 +1,7 @@
|
|||||||
import { createApp, toRaw } from 'vue'
|
import { createApp, toRaw } from 'vue'
|
||||||
import App from './App.vue'
|
import App from './App.vue'
|
||||||
|
import { mockAPI } from './utils/index.js'
|
||||||
|
import ws from './utils/ws.js'
|
||||||
import { i18n, t } from '@/locales/index.js'
|
import { i18n, t } from '@/locales/index.js'
|
||||||
import plugins from '@/plugins/index.js'
|
import plugins from '@/plugins/index.js'
|
||||||
import icons from '@/icons/index.js'
|
import icons from '@/icons/index.js'
|
||||||
@ -23,15 +25,8 @@ app.config.globalProperties.$restoreIP = restoreIP
|
|||||||
|
|
||||||
app.config.globalProperties.$toRaw = toRaw
|
app.config.globalProperties.$toRaw = toRaw
|
||||||
|
|
||||||
app.config.globalProperties.$mockAPI = ({ imitate = {}, delay = 500 } = {}) =>
|
app.config.globalProperties.$mockAPI = mockAPI
|
||||||
new Promise((resolve) => {
|
|
||||||
setTimeout(() => {
|
app.config.globalProperties.$ws = ws
|
||||||
resolve({
|
|
||||||
code: '0000',
|
|
||||||
data: imitate,
|
|
||||||
success: true,
|
|
||||||
})
|
|
||||||
}, delay)
|
|
||||||
})
|
|
||||||
|
|
||||||
app.mount('#app')
|
app.mount('#app')
|
||||||
|
11
copilot/utils/index.js
Normal file
11
copilot/utils/index.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
export function mockAPI({ imitate = {}, delay = 500 } = {}) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
setTimeout(() => {
|
||||||
|
resolve({
|
||||||
|
code: '0000',
|
||||||
|
data: imitate,
|
||||||
|
success: true,
|
||||||
|
})
|
||||||
|
}, delay)
|
||||||
|
})
|
||||||
|
}
|
19
copilot/utils/ws.js
Normal file
19
copilot/utils/ws.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
const ws = new WebSocket('wss://websocket-echo.com/')
|
||||||
|
|
||||||
|
let timeout = null
|
||||||
|
function heartbeat() {
|
||||||
|
clearTimeout(timeout)
|
||||||
|
|
||||||
|
timeout = setTimeout(() => {
|
||||||
|
ws.close()
|
||||||
|
}, 30000 + 1000)
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.addEventListener('error', console.error)
|
||||||
|
ws.addEventListener('open', heartbeat)
|
||||||
|
ws.addEventListener('ping', heartbeat)
|
||||||
|
ws.addEventListener('close', () => {
|
||||||
|
clearTimeout(timeout)
|
||||||
|
})
|
||||||
|
|
||||||
|
export default ws
|
@ -2,6 +2,7 @@ import { relative } from 'node:path'
|
|||||||
import { Hono } from 'hono'
|
import { Hono } from 'hono'
|
||||||
import { serve } from '@hono/node-server'
|
import { serve } from '@hono/node-server'
|
||||||
import { serveStatic } from '@hono/node-server/serve-static'
|
import { serveStatic } from '@hono/node-server/serve-static'
|
||||||
|
import createWebSocketServer from './wss/index'
|
||||||
|
|
||||||
export default async (mainWindow) => {
|
export default async (mainWindow) => {
|
||||||
const app = new Hono()
|
const app = new Hono()
|
||||||
@ -13,9 +14,10 @@ export default async (mainWindow) => {
|
|||||||
const VITE_DEV_SERVER_URL = process.env.VITE_DEV_SERVER_URL
|
const VITE_DEV_SERVER_URL = process.env.VITE_DEV_SERVER_URL
|
||||||
|
|
||||||
if (VITE_DEV_SERVER_URL) {
|
if (VITE_DEV_SERVER_URL) {
|
||||||
app.get('/', ctx =>
|
app.get('/', (ctx) => {
|
||||||
ctx.redirect(`${VITE_DEV_SERVER_URL}copilot/index.html`),
|
console.log('ctx', ctx.get('wss'))
|
||||||
)
|
return ctx.redirect(`${VITE_DEV_SERVER_URL}copilot/index.html`)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
app.use(
|
app.use(
|
||||||
@ -39,5 +41,7 @@ export default async (mainWindow) => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createWebSocketServer()
|
||||||
|
|
||||||
serve({ fetch: app.fetch, port: 1996 })
|
serve({ fetch: app.fetch, port: 1996 })
|
||||||
}
|
}
|
||||||
|
34
electron/copilot/wss/index.js
Normal file
34
electron/copilot/wss/index.js
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import { WebSocketServer } from 'ws'
|
||||||
|
|
||||||
|
function createWebSocketServer() {
|
||||||
|
const wss = new WebSocketServer({ port: 8080 })
|
||||||
|
|
||||||
|
function heartbeat(value = true) {
|
||||||
|
this.isAlive = value
|
||||||
|
}
|
||||||
|
|
||||||
|
wss.on('connection', (ws) => {
|
||||||
|
heartbeat.call(ws)
|
||||||
|
ws.on('error', console.error)
|
||||||
|
ws.on('pong', heartbeat)
|
||||||
|
})
|
||||||
|
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
wss.clients.forEach((ws) => {
|
||||||
|
if (ws.isAlive === false) {
|
||||||
|
return ws.terminate()
|
||||||
|
}
|
||||||
|
|
||||||
|
heartbeat.call(ws, false)
|
||||||
|
ws.ping()
|
||||||
|
})
|
||||||
|
}, 30000)
|
||||||
|
|
||||||
|
wss.on('close', () => {
|
||||||
|
clearInterval(interval)
|
||||||
|
})
|
||||||
|
|
||||||
|
return wss
|
||||||
|
}
|
||||||
|
|
||||||
|
export default createWebSocketServer
|
@ -44,6 +44,7 @@
|
|||||||
"fs-extra": "^11.1.1",
|
"fs-extra": "^11.1.1",
|
||||||
"hono": "^3.10.1",
|
"hono": "^3.10.1",
|
||||||
"husky": "^8.0.0",
|
"husky": "^8.0.0",
|
||||||
|
"isomorphic-ws": "^5.0.0",
|
||||||
"lodash-es": "^4.17.21",
|
"lodash-es": "^4.17.21",
|
||||||
"pinia": "^2.1.7",
|
"pinia": "^2.1.7",
|
||||||
"postcss": "^8.4.31",
|
"postcss": "^8.4.31",
|
||||||
@ -57,6 +58,7 @@
|
|||||||
"vite-svg-loader": "^4.0.0",
|
"vite-svg-loader": "^4.0.0",
|
||||||
"vue-command": "^35.2.1",
|
"vue-command": "^35.2.1",
|
||||||
"vue-i18n": "^9.5.0",
|
"vue-i18n": "^9.5.0",
|
||||||
"which": "^4.0.0"
|
"which": "^4.0.0",
|
||||||
|
"ws": "^8.14.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user