Browse Source

Socket链接相关

liuyc 1 year ago
parent
commit
0dcb9f0d75
1 changed files with 35 additions and 44 deletions
  1. 35 44
      src/api/ws.js

+ 35 - 44
src/api/ws.js

@@ -1,51 +1,42 @@
-
 import website from "@/config/website";
-import { getStore} from "@/util/store";
-import { getUserInfo} from '@/api/user'
-const userInfo=getStore({name: 'userInfo'}) || [];
+import {getStore} from "@/util/store";
+import {getUserInfo} from '@/api/user'
+
+const userInfo = getStore({name: 'userInfo'}) || [];
 let ws = {};
 let socket
-ws.msgCount={
-    myMessageCount:'0',
-    userId:'1111111'
-}
-ws.setInitSocket= function(uid){
-	const user_id = userInfo.user_id;
-        socket = new WebSocket(website.socket + user_id);
-        socket.onopen = function (evt) {
-            console.log('websocket链接成功');
-        };
-      
-        socket.onclose = function (evt) {
-            console.log('websocket连接已断开')
-        };
-        let that=this
-        socket.onmessage = function ({data}) {
-            if (data) {
-                if(JSON.parse(data)!==null){
-                   ws.msgCount = JSON.parse(data)
-                    
-                }
-            }
-            console.log(data,'消息信息')
-        };
-        socket.onerror = function ({data}) {
-            console.log('发生错误:', data)
-        };
+ws.setInitSocket = function (uid) {
+  const user_id = userInfo.user_id;
+  socket = new WebSocket(website.socket + user_id);
+  socket.onopen = function (evt) {
+    console.log('link success');
+  };
+  socket.onclose = function (evt) {
+    console.log('link break')
+  };
+  let that = this
+  socket.onmessage = function ({data}) {
+    if (data) {
+      if (JSON.parse(data) !== null) {
+        ws.msgCount = JSON.parse(data)
+      }
+    }
+    //console.log(data, '消息信息')
+  };
+  socket.onerror = function ({data}) {
+    console.log('link error:', data)
+  };
 
 }
-ws.socketSend= function(uid){
-	const user_id = userInfo.user_id;
-        console.log('发送消息:',)
-        if (socket) {
-            socket.send(user_id);
-        } else {
-            setTimeout(()=>{
-                socket.send(user_id);
-            }, 1000)
-        }
-    
-
+ws.socketSend = function () {
+  const data = "1,1,manager";
+  //console.log('发送消息:', data)
+  if (socket) {
+    socket.send(data);
+  } else {
+    setTimeout(() => {
+      socket.send(data);
+    }, 1000)
+  }
 }
 export default ws;
-