Преглед изворни кода

feat: socket发送前加上副本号

lanjianrong пре 3 година
родитељ
комит
592a50bc8b
1 измењених фајлова са 11 додато и 2 уклоњено
  1. 11 2
      src/hooks/core/useWebSocketFn.ts

+ 11 - 2
src/hooks/core/useWebSocketFn.ts

@@ -1,6 +1,6 @@
 /* eslint-disable react-hooks/rules-of-hooks */
 import { useWebSocket } from 'ahooks'
-import { useEffect } from 'react'
+import { useEffect, useState } from 'react'
 import { useModel } from 'umi'
 
 export enum cmdType {
@@ -20,10 +20,17 @@ export default function useWebSocketFn() {
 
   const { currentUser } = initialState
 
+  const [copyCount, setCopyCount] = useState('')
   const { disconnect, connect, webSocketIns } = useWebSocket(
     `ws://cld2qa.com/summon/v1/chat/link?username=${currentUser.username}&id=${currentUser.staffId}`,
     {
       manual: true,
+      onMessage: msg => {
+        const data = JSON.parse(msg.data)
+        if (data.cmd === 1) {
+          setCopyCount(data.CopyCount)
+        }
+      },
       reconnectLimit: 0,
       reconnectInterval: 2000
     }
@@ -41,7 +48,9 @@ export default function useWebSocketFn() {
   }, [])
 
   function sendMsg(cmd: cmdType, options: any) {
-    webSocketIns?.send(JSON.stringify({ cmd, userid: currentUser.staffId, ...options }))
+    webSocketIns?.send(
+      JSON.stringify({ cmd, userid: `${currentUser.staffId}_${copyCount}`, ...options })
+    )
   }
   return { webSocketIns, sendMsg }
 }