Jelajahi Sumber

fix(notice): WebSocket 按页面协议自动选择 wss/ws

HTTPS 页面无法发起 ws:// 连接(浏览器抛 SecurityError),改为根据 window.location.protocol 自动使用 wss:// 或 ws://。
lihaiting 1 hari lalu
induk
melakukan
ec771714e5
1 mengubah file dengan 5 tambahan dan 2 penghapusan
  1. 5 2
      src/layout/components/header-notice.vue

+ 5 - 2
src/layout/components/header-notice.vue

@@ -217,12 +217,15 @@
     },
     created() {
       if (getToken()) {
+        // HTTPS 页面禁止发起 ws:// 连接(浏览器抛 SecurityError),按当前页面协议自动选 wss/ws
+        let protocol =
+          window.location.protocol === 'https:' ? 'wss://' : 'ws://';
         let url =
-          'ws://' +
+          protocol +
           window.location.host +
           '/ws/websocket/UnreadNotifyMessageCount?token=' +
           getToken();
-     
+
         let socket = new WebSocket(url);
         socket.onmessage = (event) => {
           if (event.data) {