Kaynağa Gözat

Socket链接相关

liuyc 1 yıl önce
ebeveyn
işleme
62cf21efbb
1 değiştirilmiş dosya ile 9 ekleme ve 7 silme
  1. 9 7
      src/plugins/HcSocket.js

+ 9 - 7
src/plugins/HcSocket.js

@@ -1,5 +1,5 @@
 import website from '~src/config/index'
-import { isNullES } from 'js-fast-way'
+import {isNullES} from 'js-fast-way'
 
 // 长链接推送插件
 export default class HcSocket {
@@ -9,29 +9,31 @@ export default class HcSocket {
     static create(data, change) {
         const socket = new WebSocket(website.socket + data)
         socket.onopen = function () {
-            console.log('websocket 链接成功')
+            console.log('link success')
         }
         socket.onclose = function () {
-            console.log('websocket 链接已断开')
+            console.log('link break')
         }
-        socket.onmessage = function ({ data }) {
+        socket.onmessage = function ({data}) {
             if (typeof change !== 'function') {
                 return false
             } else {
                 change(JSON.parse(data))
             }
+            //console.log("收到消息:", data)
         }
-        socket.onerror = function ({ data }) {
-            console.log('websocket 发生错误:', data)
+        socket.onerror = function ({data}) {
+            console.log('link error:', data)
         }
         this.socket = socket
     }
 
     //发送消息
     static async send(data) {
+        const dataStr = data + ",business";
         const is_socket = await this.isSocket()
         if (!is_socket) return false
-        this.socket.send(data)
+        this.socket.send(dataStr)
     }
 
     //链接是否存在