|
| 1 | +<template> |
| 2 | + <PageWrapper title="WebSocket 示例"> |
| 3 | + <div class="flex"> |
| 4 | + <div class="w-1/3 bg-white p-4"> |
| 5 | + <div class="flex items-center"> |
| 6 | + <span class="text-lg font-medium mr-4"> 连接状态: </span> |
| 7 | + <Tag :color="getTagColor">{{ status }}</Tag> |
| 8 | + </div> |
| 9 | + <hr class="my-4" /> |
| 10 | + |
| 11 | + <div class="flex"> |
| 12 | + <a-input v-model:value="server" disabled> |
| 13 | + <template #addonBefore> 服务地址 </template> |
| 14 | + </a-input> |
| 15 | + <a-button :type="getIsOpen ? 'danger' : 'primary'" @click="toggle"> |
| 16 | + {{ getIsOpen ? '关闭连接' : '开启连接' }} |
| 17 | + </a-button> |
| 18 | + </div> |
| 19 | + <p class="text-lg font-medium mt-4">设置</p> |
| 20 | + <hr class="my-4" /> |
| 21 | + |
| 22 | + <InputTextArea |
| 23 | + placeholder="需要发送到服务器的内容" |
| 24 | + :disabled="!getIsOpen" |
| 25 | + v-model:value="sendValue" |
| 26 | + allowClear |
| 27 | + /> |
| 28 | + |
| 29 | + <a-button type="primary" block class="mt-4" :disabled="!getIsOpen" @click="handlerSend"> |
| 30 | + 发送 |
| 31 | + </a-button> |
| 32 | + </div> |
| 33 | + |
| 34 | + <div class="w-2/3 bg-white ml-4 p-4"> |
| 35 | + <span class="text-lg font-medium mr-4"> 消息记录: </span> |
| 36 | + <hr class="my-4" /> |
| 37 | + |
| 38 | + <div class="max-h-80 overflow-auto"> |
| 39 | + <ul> |
| 40 | + <li v-for="item in getList" class="border-b-1 mt-2" :key="item.time"> |
| 41 | + <div class="flex items-center"> |
| 42 | + <span class="mr-2 text-primary font-medium">收到消息:</span> |
| 43 | + <span>{{ formatToDateTime(item.time) }}</span> |
| 44 | + </div> |
| 45 | + <div> |
| 46 | + {{ item.res }} |
| 47 | + </div> |
| 48 | + </li> |
| 49 | + </ul> |
| 50 | + </div> |
| 51 | + </div> |
| 52 | + </div> |
| 53 | + </PageWrapper> |
| 54 | +</template> |
| 55 | +<script lang="ts"> |
| 56 | + import { defineComponent, reactive, watchEffect, computed, toRefs } from 'vue'; |
| 57 | + import { Alert, Tag, Input } from 'ant-design-vue'; |
| 58 | +
|
| 59 | + import { PageWrapper } from '/@/components/Page'; |
| 60 | +
|
| 61 | + import { useWebSocket } from '@vueuse/core'; |
| 62 | +
|
| 63 | + import { formatToDateTime } from '/@/utils/dateUtil'; |
| 64 | + export default defineComponent({ |
| 65 | + components: { |
| 66 | + PageWrapper, |
| 67 | + [Input.name]: Input, |
| 68 | + InputTextArea: Input.TextArea, |
| 69 | + Alert, |
| 70 | + Tag, |
| 71 | + }, |
| 72 | + setup() { |
| 73 | + const state = reactive({ |
| 74 | + server: 'ws://localhost:3380/test', |
| 75 | + sendValue: '', |
| 76 | + recordList: [] as { id: number; time: number; res: string }[], |
| 77 | + }); |
| 78 | +
|
| 79 | + const { status, data, send, close, open } = useWebSocket(state.server, { |
| 80 | + autoReconnect: true, |
| 81 | + heartbeat: true, |
| 82 | + }); |
| 83 | +
|
| 84 | + 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), |
| 93 | + time: new Date().getTime(), |
| 94 | + }); |
| 95 | + } |
| 96 | + } |
| 97 | + }); |
| 98 | +
|
| 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(state.sendValue); |
| 108 | + state.sendValue = ''; |
| 109 | + } |
| 110 | +
|
| 111 | + function toggle() { |
| 112 | + if (getIsOpen.value) { |
| 113 | + close(); |
| 114 | + } else { |
| 115 | + open(); |
| 116 | + } |
| 117 | + } |
| 118 | + return { |
| 119 | + status, |
| 120 | + formatToDateTime, |
| 121 | + ...toRefs(state), |
| 122 | + handlerSend, |
| 123 | + getList, |
| 124 | + toggle, |
| 125 | + getIsOpen, |
| 126 | + getTagColor, |
| 127 | + }; |
| 128 | + }, |
| 129 | + }); |
| 130 | +</script> |
0 commit comments