张延森 5 年 前
コミット
edcb60039e
共有5 個のファイルを変更した51 個の追加7 個の削除を含む
  1. 4
    4
      config/dev.js
  2. 1
    1
      project.config.json
  3. 1
    1
      src/app.js
  4. 7
    1
      src/pages/im/index.js
  5. 38
    0
      src/utils/im.js

+ 4
- 4
config/dev.js ファイルの表示

@@ -9,10 +9,10 @@ module.exports = {
9 9
     // WSS_HOST: '"wss://dev.pawoma.cn"',
10 10
     // HOST: '"https://dev.jinchengjiaye.com"',//测试
11 11
     // WSS_HOST: '"wss://dev.jinchengjiaye.com"',
12
-    HOST: '"https://lt.pawoma.cn"',
13
-    WSS_HOST: '"wss://lt.pawoma.cn"',
14
-    // HOST: '"http://127.0.0.1:8080"',
15
-    // WSS_HOST: '"ws://127.0.0.1:8080"',
12
+    // HOST: '"https://lt.pawoma.cn"',
13
+    // WSS_HOST: '"wss://lt.pawoma.cn"',
14
+    HOST: '"http://127.0.0.1:8080"',
15
+    WSS_HOST: '"ws://127.0.0.1:8080"',
16 16
     Version: 'V3.5.9'
17 17
   },
18 18
   weapp: {},

+ 1
- 1
project.config.json ファイルの表示

@@ -2,7 +2,7 @@
2 2
 	"miniprogramRoot": "dist/",
3 3
 	"projectname": "mini-chengjiao",
4 4
 	"description": "知与行联调",
5
-	"appid": "wxd6f47a9bb3052175",
5
+	"appid": "wxd9ee3a9480a4e544",
6 6
 	"setting": {
7 7
 		"urlCheck": false,
8 8
 		"es6": false,

+ 1
- 1
src/app.js ファイルの表示

@@ -285,7 +285,7 @@ class App extends Component {
285 285
             const { unReadNum } = info
286 286
             store.dispatch({ type: ASSIGN_UNREADNUM, payload: { unReadNum } });
287 287
             socket.createSocket({ id: personId });
288
-            socket.wss.onMessage(data => {
288
+            socket.onMessage(data => {
289 289
               store.dispatch({ type: INCREASE_UNREADNUM })
290 290
             })
291 291
           })

+ 7
- 1
src/pages/im/index.js ファイルの表示

@@ -58,6 +58,9 @@ export default class Chat extends Component {
58 58
   }
59 59
 
60 60
   originList = []
61
+
62
+  unSubscribeMessage = () => {}
63
+
61 64
   componentWillMount() {
62 65
     savePoint({
63 66
       event: 'detail',
@@ -70,6 +73,8 @@ export default class Chat extends Component {
70 73
       })
71 74
       console.log('在线咨询')
72 75
     })
76
+
77
+    this.unSubscribeMessage = () => {}
73 78
   }
74 79
 
75 80
   componentDidMount() {
@@ -157,7 +162,7 @@ export default class Chat extends Component {
157 162
         });
158 163
       })
159 164
 
160
-      socket.wss.onMessage(res => {
165
+      this.unSubscribeMessage = socket.onMessage(res => {
161 166
         const data = JSON.parse(res.data)
162 167
 
163 168
         console.log('receive message ==>', data)
@@ -175,6 +180,7 @@ export default class Chat extends Component {
175 180
     // socket.closeSocket()
176 181
     const { recordId } = this.state
177 182
     recordId && updatePoint(recordId)
183
+    this.unSubscribeMessage()
178 184
   }
179 185
 
180 186
   sendCardMsg = () => {

+ 38
- 0
src/utils/im.js ファイルの表示

@@ -10,6 +10,9 @@ class IMManager {
10 10
     this.lockReconnect = false
11 11
     this.limit = 12
12 12
     this.selfClose = false
13
+    this.timeTicker = undefined
14
+    this.messageListeners = []
15
+    this.imMessageListener = undefined
13 16
   }
14 17
 
15 18
   reconnect() {
@@ -54,6 +57,20 @@ class IMManager {
54 57
       success && success(res)
55 58
     })
56 59
 
60
+    this.wss.onMessage((data) => {
61
+      if (data.data === 'ping') {
62
+        this.wss.send({ data: 'pong'  })
63
+      } else if (data.data === 'pong') {
64
+        console.log('[websocket]: 接收心跳')
65
+      } else {
66
+        if (this.imMessageListener) {
67
+          this.imMessageListener(data)
68
+        } else {
69
+          this.messageListeners.forEach(fn => fn(data))
70
+        }
71
+      }
72
+    })
73
+
57 74
     this.wss.onClose(res => {
58 75
       console.log('已关闭', res)
59 76
       close && close(res)
@@ -74,6 +91,16 @@ class IMManager {
74 91
         this.reconnect()
75 92
       }
76 93
     })
94
+
95
+    this.timeTicker = setInterval(() => {
96
+      if (this.limit <= 0) {
97
+        clearInterval(this.timeTicker)
98
+      }
99
+
100
+      if (this.socketOpen) {
101
+        this.wss.send({ data: 'ping', success: () => console.log('[websocket]: 发送心跳') })
102
+      }
103
+    }, 60000)
77 104
   };
78 105
 
79 106
   //组装消息
@@ -130,6 +157,17 @@ class IMManager {
130 157
           })
131 158
     }
132 159
   }
160
+
161
+  onMessage(callback, isIMPage) {
162
+    if (isIMPage) {
163
+      this.messageListeners = [callback]
164
+    } else {
165
+      this.imMessageListener = callback
166
+
167
+      // 取消订阅
168
+      return () => this.imMessageListener = undefined
169
+    }
170
+  }
133 171
 }
134 172
 
135 173
 export default new IMManager()