HTTPS 页面无法发起 ws:// 连接(浏览器抛 SecurityError),改为根据 window.location.protocol 自动使用 wss:// 或 ws://。
@@ -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) {