File size: 2,921 Bytes
d669ddb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { ref } from 'vue';
import { defineStore } from 'pinia';

export interface SydneyConfig {
  baseUrl: string;
  label: string;
  isUsable?: boolean;
  delay?: number;
  isCus?: boolean;
}

export interface CheckSydneyConfigResult {
  isUsable: boolean;
  errorMsg?: string;
  delay?: number;
}

export const useChatStore = defineStore(
  'chat-store',
  () => {
    const chatHubPath = '/sydney/ChatHub';

    const isShowChatServiceSelectModal = ref(false);
    const selectedSydneyBaseUrl = ref('');
    const sydneyConfigs = ref<SydneyConfig[]>([
      {
        baseUrl: 'https://sydney.bing.com',
        label: 'Bing 官方',
      },
      {
        baseUrl: 'https://sydney.b1ng.chat',
        label: 'Cloudflare Worker',
      },
      {
        baseUrl: location.origin,
        label: '本站',
      },
      {
        baseUrl: '',
        label: '自定义',
        isCus: true,
      },
    ]);
    const sydneyCheckTimeoutMS = 3000;

    const checkSydneyConfig = async (config: SydneyConfig): Promise<CheckSydneyConfigResult> => {
      if (!config.baseUrl) {
        return {
          isUsable: false,
          errorMsg: '链接不可为空',
        };
      }
      try {
        const startTime = Date.now();
        const ws = new WebSocket(config.baseUrl.replace('http', 'ws') + chatHubPath);
        const wsTimer = setTimeout(() => {
          ws.close();
        }, sydneyCheckTimeoutMS);
        await new Promise((resolve, reject) => {
          ws.onopen = () => {
            clearTimeout(wsTimer);
            resolve(ws.close());
          };
          ws.onerror = () => {
            clearTimeout(wsTimer);
            reject(new Error(`聊天服务器 ${config.baseUrl} 连接失败`));
          };
          ws.onclose = () => reject(new Error(`聊天服务器 ${config.baseUrl} 连接超时`));
        });
        return {
          isUsable: true,
          delay: Date.now() - startTime,
        };
      } catch (error) {
        return {
          isUsable: false,
          errorMsg: error instanceof Error ? error.message : '',
        };
      }
    };

    const checkAllSydneyConfig = async () => {
      const checkAllConfigPromises = sydneyConfigs.value
        .filter((x) => x.baseUrl)
        .map(async (config) => {
          const checkResult = await checkSydneyConfig(config);
          config.isUsable = checkResult.isUsable;
          config.delay = checkResult.delay;
        });
      await Promise.all(checkAllConfigPromises);
    };

    return {
      isShowChatServiceSelectModal,
      sydneyConfigs,
      selectedSydneyBaseUrl,
      checkSydneyConfig,
      checkAllSydneyConfig,
    };
  },
  {
    persist: {
      key: 'chat-store',
      storage: localStorage,
      paths: ['selectedSydneyBaseUrl', 'sydneyConfigs'],
    },
  }
);