index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <div class="flex">
  3. <el-card :gutter="12" class="w-1/2" shadow="always">
  4. <template #header>
  5. <div class="card-header">
  6. <span>连接</span>
  7. </div>
  8. </template>
  9. <div class="flex items-center">
  10. <span class="text-lg font-medium mr-4"> 连接状态: </span>
  11. <el-tag :color="getTagColor">{{ status }}</el-tag>
  12. </div>
  13. <hr class="my-4" />
  14. <div class="flex">
  15. <el-input v-model="server" disabled>
  16. <template #prepend> 服务地址</template>
  17. </el-input>
  18. <el-button :type="getIsOpen ? 'danger' : 'primary'" @click="toggle">
  19. {{ getIsOpen ? '关闭连接' : '开启连接' }}
  20. </el-button>
  21. </div>
  22. <p class="text-lg font-medium mt-4">设置</p>
  23. <hr class="my-4" />
  24. <el-input
  25. v-model="sendValue"
  26. :autosize="{ minRows: 2, maxRows: 4 }"
  27. :disabled="!getIsOpen"
  28. clearable
  29. type="textarea"
  30. />
  31. <el-button :disabled="!getIsOpen" block class="mt-4" type="primary" @click="handlerSend">
  32. 发送
  33. </el-button>
  34. </el-card>
  35. <el-card :gutter="12" class="w-1/2" shadow="always">
  36. <template #header>
  37. <div class="card-header">
  38. <span>消息记录</span>
  39. </div>
  40. </template>
  41. <div class="max-h-80 overflow-auto">
  42. <ul>
  43. <li v-for="item in getList" :key="item.time" class="mt-2">
  44. <div class="flex items-center">
  45. <span class="mr-2 text-primary font-medium">收到消息:</span>
  46. <span>{{ formatDate(item.time) }}</span>
  47. </div>
  48. <div>
  49. {{ item.res }}
  50. </div>
  51. </li>
  52. </ul>
  53. </div>
  54. </el-card>
  55. </div>
  56. </template>
  57. <script lang="ts" name="WebSocket" setup>
  58. import { formatDate } from '@/utils/formatTime'
  59. import { useUserStore } from '@/store/modules/user'
  60. import { useWebSocket } from '@vueuse/core'
  61. const userStore = useUserStore()
  62. const sendValue = ref('')
  63. const server = ref(
  64. (import.meta.env.VITE_BASE_URL + '/websocket/message').replace('http', 'ws') +
  65. '?userId=' +
  66. userStore.getUser.id
  67. )
  68. const state = reactive({
  69. recordList: [] as { id: number; time: number; res: string }[]
  70. })
  71. const { status, data, send, close, open } = useWebSocket(server.value, {
  72. autoReconnect: false,
  73. heartbeat: true
  74. })
  75. watchEffect(() => {
  76. if (data.value) {
  77. try {
  78. const res = JSON.parse(data.value)
  79. state.recordList.push(res)
  80. } catch (error) {
  81. state.recordList.push({
  82. res: data.value,
  83. id: Math.ceil(Math.random() * 1000),
  84. time: new Date().getTime()
  85. })
  86. }
  87. }
  88. })
  89. const getIsOpen = computed(() => status.value === 'OPEN')
  90. const getTagColor = computed(() => (getIsOpen.value ? 'success' : 'red'))
  91. const getList = computed(() => {
  92. return [...state.recordList].reverse()
  93. })
  94. function handlerSend() {
  95. send(sendValue.value)
  96. sendValue.value = ''
  97. }
  98. function toggle() {
  99. if (getIsOpen.value) {
  100. close()
  101. } else {
  102. open()
  103. }
  104. }
  105. </script>