@@ -27,6 +27,8 @@ const remotes = ref<RemoteEntry[]>(loadRemotes())
2727const phase = ref <' server-pick' | ' chat' >(' server-pick' )
2828const transport = ref <WebSocketTransport | null >(null )
2929const currentBaseUrl = ref (' ' )
30+ const connectError = ref (' ' )
31+ const connecting = ref (false )
3032
3133const themeMode = ref <ThemeMode >(((): ThemeMode => {
3234 const v = localStorage .getItem (THEME_KEY )
@@ -56,15 +58,28 @@ function onSystemChange() {
5658onMounted (() => mq .addEventListener (' change' , onSystemChange ))
5759onUnmounted (() => mq .removeEventListener (' change' , onSystemChange ))
5860
59- function selectServer(baseUrl : string ) {
60- transport .value = new WebSocketTransport (baseUrl )
61- currentBaseUrl .value = baseUrl
62- phase .value = ' chat'
61+ async function selectServer(baseUrl : string ) {
62+ if (connecting .value ) return
63+ connectError .value = ' '
64+ connecting .value = true
65+ const nextTransport = new WebSocketTransport (baseUrl )
66+ try {
67+ await nextTransport .getSettings ()
68+ transport .value = nextTransport
69+ currentBaseUrl .value = baseUrl
70+ phase .value = ' chat'
71+ } catch {
72+ nextTransport .disconnect ()
73+ connectError .value = ` 无法连接服务器 ${baseUrl } `
74+ } finally {
75+ connecting .value = false
76+ }
6377}
6478
6579function switchServer() {
6680 transport .value ?.disconnect ()
6781 transport .value = null
82+ connectError .value = ' '
6883 phase .value = ' server-pick'
6984}
7085
@@ -108,6 +123,8 @@ function removeRemote(index: number) {
108123 </div >
109124
110125 <template v-if =" phase === ' server-pick' " >
126+ <div v-if =" connectError" class =" desktop-connect-error" >{{ connectError }}</div >
127+ <div v-if =" connecting" class =" desktop-connect-status" >正在连接服务器...</div >
111128 <ServerPicker
112129 :remotes =" remotes "
113130 @select-local =" selectLocal "
@@ -183,4 +200,22 @@ body {
183200 right : 12px ;
184201 z-index : 10 ;
185202}
203+ .desktop-connect-error ,
204+ .desktop-connect-status {
205+ padding : 8px 12px ;
206+ margin : 8px 12px 0 ;
207+ border-radius : 4px ;
208+ font-size : 12px ;
209+ flex-shrink : 0 ;
210+ }
211+ .desktop-connect-error {
212+ color : var (--chatui-btn-danger , #d14343 );
213+ background : var (--chatui-bg-surface );
214+ border : 1px solid var (--chatui-btn-danger , #d14343 );
215+ }
216+ .desktop-connect-status {
217+ color : var (--chatui-fg-secondary );
218+ background : var (--chatui-bg-surface );
219+ border : 1px solid var (--chatui-border );
220+ }
186221 </style >
0 commit comments