wujingjing
2025-01-14 12a89593d13fa38810c7af54c7ea8cb72ae65a10
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
import { reverse } from 'lodash-es';
import { type Ref } from 'vue';
import type { ChatMessage } from '../model/types';
import { MAIN_URL } from '/@/constants';
import { SSEClient } from '/@/utils/sse/SSEClient';
import { Local } from '/@/utils/storage';
import { accessSessionKey } from '/@/utils/request';
 
type UseSyncMsgOptions = {
    updateLoadIndex: (addCount: number) => void;
    msgList: Ref<ChatMessage[]>;
};
 
export const useSyncMsg = (options: UseSyncMsgOptions) => {
    const { updateLoadIndex, msgList } = options;
    // 创建实例
    const sseClient = new SSEClient(
        `${MAIN_URL}chat/connect_broadcast_chat`,
 
        {},
        {
            onMessage: (data) => {
                return;
                const recentIds = reverse([
                    { id: 'a1b2c3d4', time: '2024-03-27 15:42:33' },
                    { id: 'e5f6g7h8', time: '2024-02-15 09:23:45' },
                    { id: 'i9j0k1l2', time: '2024-05-08 14:37:21' },
                    { id: 'm3n4o5p6', time: '2024-01-30 11:55:16' },
                    { id: 'q7r8s9t0', time: '2024-07-12 16:48:59' },
                    { id: 'u1v2w3x4', time: '2024-04-03 10:15:27' },
                    { id: 'y5z6a7b8', time: '2024-06-21 13:29:44' },
                    { id: 'c9d0e1f2', time: '2024-08-09 17:52:38' },
                    { id: 'g3h4i5j6', time: '2024-09-14 12:33:51' },
                    { id: 'k7l8m9n0', time: '2024-10-25 08:19:07' },
                ]);
                // const userHistoryIds = reverse(msgList.value.filter((item) => item.role === RoleEnum.user).map((item) => item.historyId));
                const userHistoryIds = reverse([
                    { id: 'a1b2c3d4', time: '2024-03-27 15:42:33' },
                    { id: 'e5f6g7h8', time: '2024-02-15 09:23:45' },
                    // {id: 'i9j0k1l2', time: '2024-05-08 14:37:21'},
                    // {id: 'm3n4o5p6', time: '2024-01-30 11:55:16'},
                    { id: 'q7r8s9t0', time: '2024-07-12 16:48:59' },
                    // {id: 'u1v2w3x4', time: '2024-04-03 10:15:27'},
                    { id: 'y5z6a7b8', time: '2024-06-21 13:29:44' },
                    { id: 'c9d0e1f2', time: '2024-08-09 17:52:38' },
                    { id: 'g3h4i5j6', time: '2024-09-14 12:33:51' },
                    // {id: 'k7l8m9n0', time: '2024-10-25 08:19:07'},
                ]);
 
                // 获取未同步的消息
                const unsyncedMessages = findUnsyncedMessages(recentIds, userHistoryIds);
                console.log('未同步的消息:', unsyncedMessages);
            },
            onError: (error) => {
                console.error('SSE error:', error);
            },
            onOpen: () => {
                console.log('SSE connection opened');
            },
            onClose: () => {
                console.log('SSE connection closed');
            },
            onRetry: (retryCount) => {
                console.log(`Retrying connection (${retryCount})`);
            },
        }
    );
    sseClient.connect({
        websessionid: Local.get(accessSessionKey),
    });
 
    // onMounted(() => {
    //     sseClient.connect({});
    // });
 
    // onUnmounted(() => {
    //     sseClient.disconnect();
    // });
 
    return {
        reconnect: () => sseClient.reconnect(),
        disconnect: () => sseClient.disconnect(),
        isConnected: () => sseClient.isConnected(),
    };
};