From 9aeade98bdd0913128f57db1a98bbe1eafa7f08a Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期五, 19 七月 2024 17:41:36 +0800
Subject: [PATCH] 1. 新增webSocket相关功能

---
 src/views/inspection/WorkStream.vue |   60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/src/views/inspection/WorkStream.vue b/src/views/inspection/WorkStream.vue
index fb2ecdb..0800f34 100644
--- a/src/views/inspection/WorkStream.vue
+++ b/src/views/inspection/WorkStream.vue
@@ -1,14 +1,19 @@
 <template>
   <!-- <div class="border-r-small m-h-2 p-h-4"> -->
   <BaseCard>
+    <div>
+      <input type="text" v-model="inputVal" />
+      <button @click="handleSend">send</button>
+      <button @click="handleLink">link</button>
+    </div>
     <el-scrollbar ref="scrollbarRef" :height="height">
       <div ref="scrollContentRef">
         <div v-for="item in streams" :key="item.index">
           <el-text type="primary">[{{ item.time }}]: </el-text>
           <el-text>鐢ㄦ埛</el-text>
-          <el-text type="warning">{{ item.user }}</el-text>
-          <el-text>鍦�</el-text>
-          <el-text type="success">{{ item.obj }}</el-text>
+          <el-text type="warning">{{ item.userName }}</el-text>
+          <!-- <el-text>鍦�</el-text>
+          <el-text type="success">{{ item.obj }}</el-text> -->
           <el-text>{{ item.event }}</el-text>
         </div>
       </div>
@@ -17,10 +22,11 @@
   <!-- </div> -->
 </template>
 <script setup>
-import { reactive, ref, onMounted, inject } from 'vue'
+import { reactive, ref, onMounted, onUnmounted, inject } from 'vue'
 import dayjs from 'dayjs'
 
 import { unCalc } from '@/utils/css-util'
+import { ws } from '@/api/index'
 
 const excludeMapHeight = inject('excludeMapHeight')
 const height = `calc(${unCalc(excludeMapHeight)} - 36px)`
@@ -59,6 +65,49 @@
   }, 100)
 }
 
+const inputVal = ref('')
+
+const handleSend = () => {
+  if (socket) {
+    socket.send(inputVal.value)
+  }
+}
+
+let socket
+const handleLink = () => {
+  if (socket) {
+    socket.close()
+  }
+  socket = new WebSocket(`ws://192.168.0.138:8082/workstream`)
+  // 涓庢湇鍔″櫒寤虹珛杩炴帴锛氬彂閫佹秷鎭埌鏈嶅姟鍣�
+  socket.onopen = () => {
+    console.log('connect: ')
+  }
+  // 鏀跺埌鏈嶅姟鍣ㄥ彂閫佺殑娑堟伅锛歟vent澶勭悊鏈嶅姟鍣ㄨ繑鍥炵殑鏁版嵁
+  socket.onmessage = (event) => {
+    console.log('receive: ', event.data)
+    putWorkStream(event.data)
+  }
+  // 杩炴帴鎴栭�氫俊杩囩▼涓彂鐢熼敊璇�
+  socket.onerror = (event) => {
+    console.log('errror: ', event.error)
+  }
+  // 涓庢湇鍔″櫒鏂紑杩炴帴
+  socket.onclose = (event) => {
+    console.log('close: ', event.code)
+  }
+}
+
+/**
+ * 娣诲姞涓�鏉″伐浣滄祦淇℃伅
+ * @param {*} data
+ */
+function putWorkStream(data) {
+  const obj = JSON.parse(data)
+  streams.push(obj)
+  scrollToBottom()
+}
+
 onMounted(() => {
   // setInterval(() => {
   //   streams.push({
@@ -70,4 +119,7 @@
   //   scrollToBottom()
   // }, 10000)
 })
+onUnmounted(() => {
+  socket.close()
+})
 </script>

--
Gitblit v1.9.3