Skip to content

Commit

Permalink
perf: ⚡️ Add webSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Nov 22, 2023
1 parent 5ac5ee6 commit ae96df0
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 14 deletions.
15 changes: 5 additions & 10 deletions copilot/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { createApp, toRaw } from '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 plugins from '@/plugins/index.js'
import icons from '@/icons/index.js'
Expand All @@ -23,15 +25,8 @@ app.config.globalProperties.$restoreIP = restoreIP

app.config.globalProperties.$toRaw = toRaw

app.config.globalProperties.$mockAPI = ({ imitate = {}, delay = 500 } = {}) =>
new Promise((resolve) => {
setTimeout(() => {
resolve({
code: '0000',
data: imitate,
success: true,
})
}, delay)
})
app.config.globalProperties.$mockAPI = mockAPI

app.config.globalProperties.$ws = ws

app.mount('#app')
11 changes: 11 additions & 0 deletions copilot/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function mockAPI({ imitate = {}, delay = 500 } = {}) {
return new Promise((resolve) => {
setTimeout(() => {
resolve({
code: '0000',
data: imitate,
success: true,
})
}, delay)
})
}
19 changes: 19 additions & 0 deletions copilot/utils/ws.js
Original file line number Diff line number Diff line change
@@ -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
10 changes: 7 additions & 3 deletions electron/copilot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { relative } from 'node:path'
import { Hono } from 'hono'
import { serve } from '@hono/node-server'
import { serveStatic } from '@hono/node-server/serve-static'
import createWebSocketServer from './wss/index'

export default async (mainWindow) => {
const app = new Hono()
Expand All @@ -13,9 +14,10 @@ export default async (mainWindow) => {
const VITE_DEV_SERVER_URL = process.env.VITE_DEV_SERVER_URL

if (VITE_DEV_SERVER_URL) {
app.get('/', ctx =>
ctx.redirect(`${VITE_DEV_SERVER_URL}copilot/index.html`),
)
app.get('/', (ctx) => {
console.log('ctx', ctx.get('wss'))
return ctx.redirect(`${VITE_DEV_SERVER_URL}copilot/index.html`)
})
}
else {
app.use(
Expand All @@ -39,5 +41,7 @@ export default async (mainWindow) => {
)
}

createWebSocketServer()

serve({ fetch: app.fetch, port: 1996 })
}
34 changes: 34 additions & 0 deletions electron/copilot/wss/index.js
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"fs-extra": "^11.1.1",
"hono": "^3.10.1",
"husky": "^8.0.0",
"isomorphic-ws": "^5.0.0",
"lodash-es": "^4.17.21",
"pinia": "^2.1.7",
"postcss": "^8.4.31",
Expand All @@ -57,6 +58,7 @@
"vite-svg-loader": "^4.0.0",
"vue-command": "^35.2.1",
"vue-i18n": "^9.5.0",
"which": "^4.0.0"
"which": "^4.0.0",
"ws": "^8.14.2"
}
}

0 comments on commit ae96df0

Please sign in to comment.