Skip to content

Commit

Permalink
feat(webui): support ws reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 12, 2021
1 parent 2b6cc3b commit b0df6d8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-eval/src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function formatError(error: Error) {
.join('\n')
}

const main = wrap<MainHandle>(parentPort)
export const main = wrap<MainHandle>(parentPort)

export interface SessionData {
id: string
Expand Down
35 changes: 22 additions & 13 deletions packages/plugin-webui/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,32 @@ receive('expire', () => {
router.push('/login')
})

const endpoint = new URL(KOISHI_CONFIG.endpoint, location.origin).toString()
const socket = client.socket.value = new WebSocket(endpoint.replace(/^http/, 'ws'))

socket.onmessage = (ev) => {
const data = JSON.parse(ev.data)
console.debug(data)
if (data.type in client.listeners) {
client.listeners[data.type](data.body)
function connect() {
const endpoint = new URL(KOISHI_CONFIG.endpoint, location.origin).toString()
const socket = client.socket.value = new WebSocket(endpoint.replace(/^http/, 'ws'))

socket.onmessage = (ev) => {
const data = JSON.parse(ev.data)
console.debug(data)
if (data.type in client.listeners) {
client.listeners[data.type](data.body)
}
}

socket.onopen = () => {
if (!client.user.value) return
const { id, token } = client.user.value
client.send('validate', { id, token })
}
}

socket.onopen = () => {
if (!client.user.value) return
const { id, token } = client.user.value
client.send('validate', { id, token })
socket.onclose = () => {
console.log('[koishi] websocket disconnected, will retry in 1s...')
setTimeout(connect, 1000)
}
}

connect()

const loadingExtensions = Promise.all(KOISHI_CONFIG.extensions.map(path => {
return import(/* @vite-ignore */ path)
}))
Expand Down

0 comments on commit b0df6d8

Please sign in to comment.