Skip to content

Commit

Permalink
fix(client): fix connection
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 23, 2024
1 parent 516673a commit 1849af7
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/client/app/home/home.vue
Expand Up @@ -8,14 +8,14 @@
</k-slot>
</el-scrollbar>
<div v-else>
<k-card class="connect">{{ global.messages?.connecting || '正在连接到 Koishi 服务器……' }}</k-card>
<k-card class="connect">正在连接到 Cordis 服务器……</k-card>
</div>
</k-layout>
</template>

<script lang="ts" setup>
import { global, socket } from '@cordisjs/client'
import { socket } from '@cordisjs/client'
import Welcome from './welcome.vue'
</script>
Expand Down
2 changes: 1 addition & 1 deletion packages/client/app/index.ts
@@ -1,4 +1,4 @@
import { connect, global, root } from '@koishijs/client'
import { connect, global, root } from '@cordisjs/client'
import home from './home'
import layout from './layout'
import settings from './settings'
Expand Down
2 changes: 1 addition & 1 deletion packages/client/app/layout/menu-item.vue
Expand Up @@ -8,7 +8,7 @@

<script lang="ts" setup>
import { MaybeGetter, useContext } from '@koishijs/client'
import { MaybeGetter, useContext } from '@cordisjs/client'
import { computed } from 'vue'
const props = defineProps<{
Expand Down
14 changes: 7 additions & 7 deletions packages/client/client/data.ts
@@ -1,5 +1,5 @@
import type { ClientConfig, Console, DataService, Events } from '@cordisjs/plugin-webui'
import type { Promisify, Universal } from 'koishi'
import type { ClientConfig, Console, DataService, Events, WebSocket } from '@cordisjs/plugin-webui'
import type { Promisify } from 'cosmokit'
import { markRaw, reactive, ref } from 'vue'
import { Context } from './context'

Expand All @@ -15,7 +15,7 @@ export function withProxy(url: string) {
return (global.proxyBase || '') + url
}

export const socket = ref<Universal.WebSocket>(null)
export const socket = ref<WebSocket>(null)
const listeners: Record<string, (data: any) => void> = {}
const responseHooks: Record<string, [Function, Function]> = {}

Expand Down Expand Up @@ -61,7 +61,7 @@ receive('response', ({ id, value, error }) => {
}
})

export function connect(ctx: Context, callback: () => Universal.WebSocket) {
export function connect(ctx: Context, callback: () => WebSocket) {
const value = callback()

let sendTimer: number
Expand All @@ -79,10 +79,10 @@ export function connect(ctx: Context, callback: () => Universal.WebSocket) {
for (const key in store) {
store[key] = undefined
}
console.log('[koishi] websocket disconnected, will retry in 1s...')
console.log('[cordis] websocket disconnected, will retry in 1s...')
setTimeout(() => {
connect(ctx, callback).then(location.reload, () => {
console.log('[koishi] websocket disconnected, will retry in 1s...')
console.log('[cordis] websocket disconnected, will retry in 1s...')
})
}, 1000)
}
Expand All @@ -99,7 +99,7 @@ export function connect(ctx: Context, callback: () => Universal.WebSocket) {

value.addEventListener('close', reconnect)

return new Promise<Universal.WebSocket.Event>((resolve, reject) => {
return new Promise<WebSocket.Event>((resolve, reject) => {
value.addEventListener('open', (event) => {
socket.value = markRaw(value)
resolve(event)
Expand Down
12 changes: 3 additions & 9 deletions packages/core/src/index.ts
Expand Up @@ -10,12 +10,8 @@ export * from './client'
export * from './entry'
export * from './service'

type NestedServices = {
[K in keyof Console.Services as `console.${K}`]: Console.Services[K]
}

declare module 'cordis' {
interface Context extends NestedServices {
interface Context {
console: Console
}

Expand Down Expand Up @@ -50,8 +46,6 @@ export class EntryProvider extends DataService<Dict<EntryData>> {
}

export abstract class Console extends Service {
static filter = false

private id = Math.random().toString(36).slice(2)

readonly entries: Dict<Entry> = Object.create(null)
Expand Down Expand Up @@ -106,11 +100,11 @@ export abstract class Console extends Service {
}

refresh<K extends keyof Console.Services>(type: K) {
return this.ctx.get(`console.${type}`)?.refresh()
return this.ctx.get(`console.services.${type}`)?.refresh()
}

patch<K extends keyof Console.Services>(type: K, value: Console.Services[K] extends DataService<infer T> ? T : never) {
return this.ctx.get(`console.${type}`)?.patch(value as any)
return this.ctx.get(`console.services.${type}`)?.patch(value as any)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/service.ts
Expand Up @@ -17,7 +17,7 @@ export abstract class DataService<T = never> extends Service {
}

constructor(protected ctx: Context, protected key: keyof Console.Services, public options: DataService.Options = {}) {
super(ctx, `console.${key}`, options.immediate)
super(ctx, `console.services.${key}`, options.immediate)
}

start() {
Expand Down

0 comments on commit 1849af7

Please sign in to comment.