From 0ec9d9c2f790b432ec86311e757c469ca413b59b Mon Sep 17 00:00:00 2001
From: loulijun2021 <1694218219@qq.com>
Date: 星期四, 20 七月 2023 10:24:00 +0800
Subject: [PATCH] 1.signalR 优化

---
 src/main.js                      |    3 -
 src/layout/components/Navbar.vue |   53 +++++++++++++----
 src/utils/signalR.js             |   70 ++++++++++++-----------
 3 files changed, 77 insertions(+), 49 deletions(-)

diff --git a/src/layout/components/Navbar.vue b/src/layout/components/Navbar.vue
index 41a17d2..9c11199 100644
--- a/src/layout/components/Navbar.vue
+++ b/src/layout/components/Navbar.vue
@@ -171,6 +171,7 @@
 import variables from '@/styles/variables.scss'
 import RightPanel from '@/components/RightPanel'
 import Settings from '../components/Settings/index'
+import * as signalR from '@aspnet/signalr'
 
 export default {
   components: {
@@ -224,28 +225,21 @@
         ]
       },
       stu_torgname: '',
-      show: false
+      show: false,
+      signalr: null
     }
   },
   inject: [
     'reload'
   ],
   created() {
+    // this.$signalr.start()
     this.usercode = getCookie('navTabId')
     this.username = getCookie('username')
     this.stu_torgname = getCookie('stu_torgname') === '' ? '绯荤粺绠$悊鍛�' : getCookie('stu_torgname')
   },
   mounted() {
-    this.$signalr.off('SendAll')
-    this.$signalr.on('SendAll', (res) => {
-      this.$notify({
-        offset: 80,
-        type: 'warning',
-        title: '娑堟伅鎻愰啋',
-        message: res
-      })
-      this.$store.state.settings.isDot = true
-    })
+    this.getHubConnectionBuilder()
   },
   computed: {
     ...mapGetters([
@@ -277,6 +271,40 @@
     }
   },
   methods: {
+    // 寤虹珛hub杩炴帴
+    async getHubConnectionBuilder() {
+      const url = 'http://121.196.36.24:8019/chatHub'
+      this.signalr = new signalR.HubConnectionBuilder().withUrl(url, {
+        // skipNegotiation: true,
+        // transport: signalR.HttpTransportType.WebSockets
+      }).configureLogging(signalR.LogLevel.Information).build()
+
+      await this.signalr.start().then(() => {
+        if (window.Notification) {
+          if (Notification.permission === 'granted') {
+            console.log('鍏佽閫氱煡')
+          } else if (Notification.permission !== 'denied') {
+            console.log('闇�瑕侀�氱煡鏉冮檺')
+            Notification.requestPermission((permission) => {
+              console.log('鏉冮檺閫氱煡', permission)
+            })
+          } else if (Notification.permission === 'denied') {
+            // console.log('鎷掔粷閫氱煡')
+          }
+        } else {
+          console.error('娴忚鍣ㄤ笉鏀寔Notification')
+        }
+        // console.log('杩炴帴鎴愬姛')
+      })
+
+      this.signalr.off('SendCustomUserMessage')
+      this.signalr.on('SendCustomUserMessage', (res) => {
+        this.$notify({ offset: 80, type: 'warning', title: '娑堟伅鎻愰啋', message: res })
+        this.$store.state.settings.isDot = true
+      })
+      await this.signalr.invoke('AddUser', getCookie('admin'))
+    },
+
     systemSetting() {
       this.$refs.rightPanel.show = !this.$refs.rightPanel.show
       this.$refs.rightPanel.addEventClick()
@@ -304,7 +332,8 @@
       this.$store.dispatch('app/toggleSideBar')
     },
     async logout() {
-      this.$signalr.off('SendAll')
+      await this.signalr.stop()
+      this.signalr.off('SendCustomUserMessage')
       await this.$store.dispatch('user/logout')
       this.$message.success('閫�鍑烘垚鍔燂紒')
       this.$router.push(`/login`)
diff --git a/src/main.js b/src/main.js
index 81f0498..0754da0 100644
--- a/src/main.js
+++ b/src/main.js
@@ -70,9 +70,6 @@
 // 寮曠敤淇敼杩囩殑gantt
 Vue.use(vGanttChart)
 
-import signalr from './utils/signalR'
-Vue.prototype.$signalr = signalr.signal
-
 import { setCookie } from '@/utils/auth' // 寮曠敤淇敼杩囩殑gantt
 // 鍒ゆ柇鏄惁璧板師骞冲彴杩囨潵
 if (window.location.hash.indexOf('guid') !== -1) {
diff --git a/src/utils/signalR.js b/src/utils/signalR.js
index 2a38d8f..e0a3cbc 100644
--- a/src/utils/signalR.js
+++ b/src/utils/signalR.js
@@ -1,36 +1,38 @@
-import * as signalR from '@aspnet/signalr'
+// 姝ゆ枃浠剁殑浠g爜鏀惧埌layout/Navbar鏂囦欢涓嬩簡
 
-const url = 'http://121.196.36.24:8019/chatHub'
-const signal = new signalR.HubConnectionBuilder()
-  .withUrl(url, {
-    // skipNegotiation: true,
-    // transport: signalR.HttpTransportType.WebSockets
-  })
-  .configureLogging(signalR.LogLevel.Information)
-  .build()
-// signal.on('SendAll', (res) => {
-//   console.log(res, '鏀跺埌娑堟伅浜�')
+// import * as signalR from '@aspnet/signalr'
+//
+// const url = 'http://121.196.36.24:8019/chatHub'
+// const signal = new signalR.HubConnectionBuilder()
+//   .withUrl(url, {
+//     // skipNegotiation: true,
+//     // transport: signalR.HttpTransportType.WebSockets
+//   })
+//   .configureLogging(signalR.LogLevel.Information)
+//   .build()
+// // signal.on('SendAll', (res) => {
+// //   console.log(res, '鏀跺埌娑堟伅浜�')
+// // })
+// signal.start().then(() => {
+//   if (window.Notification) {
+//     if (Notification.permission === 'granted') {
+//       console.log('鍏佽閫氱煡')
+//     } else if (Notification.permission !== 'denied') {
+//       console.log('闇�瑕侀�氱煡鏉冮檺')
+//       Notification.requestPermission((permission) => {
+//         console.log('鏉冮檺閫氱煡', permission)
+//       })
+//     } else if (Notification.permission === 'denied') {
+//       // console.log('鎷掔粷閫氱煡')
+//     }
+//   } else {
+//     console.error('娴忚鍣ㄤ笉鏀寔Notification')
+//   }
+//   // console.log('杩炴帴鎴愬姛')
 // })
-signal.start().then(() => {
-  if (window.Notification) {
-    if (Notification.permission === 'granted') {
-      console.log('鍏佽閫氱煡')
-    } else if (Notification.permission !== 'denied') {
-      console.log('闇�瑕侀�氱煡鏉冮檺')
-      Notification.requestPermission((permission) => {
-        console.log('鏉冮檺閫氱煡', permission)
-      })
-    } else if (Notification.permission === 'denied') {
-      // console.log('鎷掔粷閫氱煡')
-    }
-  } else {
-    console.error('娴忚鍣ㄤ笉鏀寔Notification')
-  }
-  // console.log('杩炴帴鎴愬姛')
-})
-// signal.onclose((err) => {
-//   console.log('杩炴帴宸茬粡鏂紑 鎵ц鍑芥暟onclose', err)
-// })
-export default {
-  signal
-}
+// // signal.onclose((err) => {
+// //   console.log('杩炴帴宸茬粡鏂紑 鎵ц鍑芥暟onclose', err)
+// // })
+// export default {
+//   signal
+// }

--
Gitblit v1.9.3