|
1 | 1 | <template>
|
2 | 2 | <div class="flex">
|
| 3 | + <!-- 左侧:建立连接、发送消息 --> |
3 | 4 | <el-card :gutter="12" class="w-1/2" shadow="always">
|
4 | 5 | <template #header>
|
5 | 6 | <div class="card-header">
|
|
11 | 12 | <el-tag :color="getTagColor">{{ status }}</el-tag>
|
12 | 13 | </div>
|
13 | 14 | <hr class="my-4" />
|
14 |
| - |
15 | 15 | <div class="flex">
|
16 | 16 | <el-input v-model="server" disabled>
|
17 |
| - <template #prepend> 服务地址</template> |
| 17 | + <template #prepend>服务地址</template> |
18 | 18 | </el-input>
|
19 |
| - <el-button :type="getIsOpen ? 'danger' : 'primary'" @click="toggle"> |
| 19 | + <el-button :type="getIsOpen ? 'danger' : 'primary'" @click="toggleConnectStatus"> |
20 | 20 | {{ getIsOpen ? '关闭连接' : '开启连接' }}
|
21 | 21 | </el-button>
|
22 | 22 | </div>
|
23 |
| - <p class="mt-4 text-lg font-medium">设置</p> |
| 23 | + <p class="mt-4 text-lg font-medium">消息输入框</p> |
24 | 24 | <hr class="my-4" />
|
25 | 25 | <el-input
|
26 |
| - v-model="sendValue" |
| 26 | + v-model="sendText" |
27 | 27 | :autosize="{ minRows: 2, maxRows: 4 }"
|
28 | 28 | :disabled="!getIsOpen"
|
29 | 29 | clearable
|
30 | 30 | type="textarea"
|
| 31 | + placeholder="请输入你要发送的消息" |
31 | 32 | />
|
32 |
| - <el-button :disabled="!getIsOpen" block class="mt-4" type="primary" @click="handlerSend"> |
| 33 | + <el-select v-model="sendUserId" class="mt-4" placeholder="请选择发送人"> |
| 34 | + <el-option key="" label="所有人" value="" /> |
| 35 | + <el-option |
| 36 | + v-for="user in userList" |
| 37 | + :key="user.id" |
| 38 | + :label="user.nickname" |
| 39 | + :value="user.id" |
| 40 | + /> |
| 41 | + </el-select> |
| 42 | + <el-button :disabled="!getIsOpen" block class="ml-2 mt-4" type="primary" @click="handlerSend"> |
33 | 43 | 发送
|
34 | 44 | </el-button>
|
35 | 45 | </el-card>
|
| 46 | + <!-- 右侧:消息记录 --> |
36 | 47 | <el-card :gutter="12" class="w-1/2" shadow="always">
|
37 | 48 | <template #header>
|
38 | 49 | <div class="card-header">
|
|
41 | 52 | </template>
|
42 | 53 | <div class="max-h-80 overflow-auto">
|
43 | 54 | <ul>
|
44 |
| - <li v-for="item in getList" :key="item.time" class="mt-2"> |
| 55 | + <li v-for="msg in messageList.reverse()" :key="msg.time" class="mt-2"> |
45 | 56 | <div class="flex items-center">
|
46 | 57 | <span class="text-primary mr-2 font-medium">收到消息:</span>
|
47 |
| - <span>{{ formatDate(item.time) }}</span> |
| 58 | + <span>{{ formatDate(msg.time) }}</span> |
48 | 59 | </div>
|
49 | 60 | <div>
|
50 |
| - {{ item.res }} |
| 61 | + {{ msg.text }} |
51 | 62 | </div>
|
52 | 63 | </li>
|
53 | 64 | </ul>
|
|
57 | 68 | </template>
|
58 | 69 | <script lang="ts" setup>
|
59 | 70 | import { formatDate } from '@/utils/formatTime'
|
60 |
| -import { useUserStore } from '@/store/modules/user' |
61 | 71 | import { useWebSocket } from '@vueuse/core'
|
| 72 | +import { getAccessToken } from '@/utils/auth' |
| 73 | +import * as UserApi from '@/api/system/user' |
62 | 74 |
|
63 | 75 | defineOptions({ name: 'InfraWebSocket' })
|
64 | 76 |
|
65 |
| -const userStore = useUserStore() |
66 |
| -
|
67 |
| -const sendValue = ref('') |
| 77 | +const message = useMessage() // 消息弹窗 |
68 | 78 |
|
69 | 79 | const server = ref(
|
70 |
| - (import.meta.env.VITE_BASE_URL + '/websocket/message').replace('http', 'ws') + |
71 |
| - '?userId=' + |
72 |
| - userStore.getUser.id |
73 |
| -) |
74 |
| -
|
75 |
| -const state = reactive({ |
76 |
| - recordList: [] as { id: number; time: number; res: string }[] |
77 |
| -}) |
| 80 | + (import.meta.env.VITE_BASE_URL + '/infra/ws').replace('http', 'ws') + '?token=' + getAccessToken() |
| 81 | +) // WebSocket 服务地址 |
| 82 | +const getIsOpen = computed(() => status.value === 'OPEN') // WebSocket 连接是否打开 |
| 83 | +const getTagColor = computed(() => (getIsOpen.value ? 'success' : 'red')) // WebSocket 连接的展示颜色 |
78 | 84 |
|
| 85 | +/** 发起 WebSocket 连接 */ |
79 | 86 | const { status, data, send, close, open } = useWebSocket(server.value, {
|
80 | 87 | autoReconnect: false,
|
81 | 88 | heartbeat: true
|
82 | 89 | })
|
83 | 90 |
|
| 91 | +/** 监听接收到的数据 */ |
| 92 | +const messageList = ref([] as { time: number; text: string }[]) // 消息列表 |
84 | 93 | watchEffect(() => {
|
85 |
| - if (data.value) { |
86 |
| - try { |
87 |
| - const res = JSON.parse(data.value) |
88 |
| - state.recordList.push(res) |
89 |
| - } catch (error) { |
90 |
| - state.recordList.push({ |
91 |
| - res: data.value, |
92 |
| - id: Math.ceil(Math.random() * 1000), |
| 94 | + if (!data.value) { |
| 95 | + return |
| 96 | + } |
| 97 | + try { |
| 98 | + // 1. 收到心跳 |
| 99 | + if (data.value === 'pong') { |
| 100 | + // state.recordList.push({ |
| 101 | + // text: '【心跳】', |
| 102 | + // time: new Date().getTime() |
| 103 | + // }) |
| 104 | + return |
| 105 | + } |
| 106 | +
|
| 107 | + // 2.1 解析 type 消息类型 |
| 108 | + const jsonMessage = JSON.parse(data.value) |
| 109 | + const type = jsonMessage.type |
| 110 | + const content = JSON.parse(jsonMessage.content) |
| 111 | + if (!type) { |
| 112 | + message.error('未知的消息类型:' + data.value) |
| 113 | + return |
| 114 | + } |
| 115 | + // 2.2 消息类型:demo-message-receive |
| 116 | + if (type === 'demo-message-receive') { |
| 117 | + const single = content.single |
| 118 | + if (single) { |
| 119 | + messageList.value.push({ |
| 120 | + text: `【单发】用户编号(${content.fromUserId}):${content.text}`, |
| 121 | + time: new Date().getTime() |
| 122 | + }) |
| 123 | + } else { |
| 124 | + messageList.value.push({ |
| 125 | + text: `【群发】用户编号(${content.fromUserId}):${content.text}`, |
| 126 | + time: new Date().getTime() |
| 127 | + }) |
| 128 | + } |
| 129 | + return |
| 130 | + } |
| 131 | + // 2.3 消息类型:notice-push |
| 132 | + if (type === 'notice-push') { |
| 133 | + messageList.value.push({ |
| 134 | + text: `【系统通知】:${content.title}`, |
93 | 135 | time: new Date().getTime()
|
94 | 136 | })
|
| 137 | + return |
95 | 138 | }
|
| 139 | + message.error('未处理消息:' + data.value) |
| 140 | + } catch (error) { |
| 141 | + message.error('处理消息发生异常:' + data.value) |
| 142 | + console.error(error) |
96 | 143 | }
|
97 | 144 | })
|
98 | 145 |
|
99 |
| -const getIsOpen = computed(() => status.value === 'OPEN') |
100 |
| -const getTagColor = computed(() => (getIsOpen.value ? 'success' : 'red')) |
101 |
| -
|
102 |
| -const getList = computed(() => { |
103 |
| - return [...state.recordList].reverse() |
104 |
| -}) |
105 |
| -
|
106 |
| -function handlerSend() { |
107 |
| - send(sendValue.value) |
108 |
| - sendValue.value = '' |
| 146 | +/** 发送消息 */ |
| 147 | +const sendText = ref('') // 发送内容 |
| 148 | +const sendUserId = ref('') // 发送人 |
| 149 | +const handlerSend = () => { |
| 150 | + // 1.1 先 JSON 化 message 消息内容 |
| 151 | + const messageContent = JSON.stringify({ |
| 152 | + text: sendText.value, |
| 153 | + toUserId: sendUserId.value |
| 154 | + }) |
| 155 | + // 1.2 再 JSON 化整个消息 |
| 156 | + const jsonMessage = JSON.stringify({ |
| 157 | + type: 'demo-message-send', |
| 158 | + content: messageContent |
| 159 | + }) |
| 160 | + // 2. 最后发送消息 |
| 161 | + send(jsonMessage) |
| 162 | + sendText.value = '' |
109 | 163 | }
|
110 | 164 |
|
111 |
| -function toggle() { |
| 165 | +/** 切换 websocket 连接状态 */ |
| 166 | +const toggleConnectStatus = () => { |
112 | 167 | if (getIsOpen.value) {
|
113 | 168 | close()
|
114 | 169 | } else {
|
115 | 170 | open()
|
116 | 171 | }
|
117 | 172 | }
|
| 173 | +
|
| 174 | +/** 初始化 **/ |
| 175 | +const userList = ref<any[]>([]) // 用户列表 |
| 176 | +onMounted(async () => { |
| 177 | + // 获取用户列表 |
| 178 | + userList.value = await UserApi.getSimpleUserList() |
| 179 | +}) |
118 | 180 | </script>
|
0 commit comments