Browse Source

消息修复

caner 1 year ago
parent
commit
9e49da360c
3 changed files with 7 additions and 13 deletions
  1. 0 1
      src/App.vue
  2. 1 3
      src/pages/room/index.vue
  3. 6 9
      src/services/mqtt.service.ts

+ 0 - 1
src/App.vue

@@ -28,7 +28,6 @@ const themeOverrides = Theme
 const show = computed(() => store.loading)
 
 watch(() => store.mqtt_message, (val) => {
-  console.log('顶级监听', val)
   if (val.type === 'MqttConnect') {
     store.setLoading(true)
     router.push('/room')

+ 1 - 3
src/pages/room/index.vue

@@ -63,15 +63,13 @@ watch(() => store.mqtt_message, async (value: { type: string, data?: RTCSessionD
   const { type, data } = value
   // 接收远程offer
   if (type === 'offer') {
-    console.log('offer', data)
     await RTC.Peer?.setRemoteDescription(data!)
     const answerd = await RTC.Peer?.createAnswer()
     await RTC.Peer?.setLocalDescription(answerd)
   }
   // 发送本地answer
   if (type === 'answer') {
-    console.log('send answer', data)
-    mqtt.send(data)
+    mqtt.send(value)
   }
 })
 

+ 6 - 9
src/services/mqtt.service.ts

@@ -43,7 +43,7 @@ export default class MqttService {
    * @returns
    */
   async send(params: object, qos = 0) {
-    console.log('发送', this.client_channel, this.mqtt_id, params)
+    console.log('发送mqtt', this.client_channel, this.mqtt_id, params)
     if (!this.client_channel || !this.mqtt_id) return null
     const newParams = JSON.stringify({ ...params, from_id: this.client_id })
     return await publish(this.mqtt_id, this.client_channel, qos, false, newParams)
@@ -80,17 +80,14 @@ export default class MqttService {
         try {
           const { payload } = event.message
           const data = JSON.parse(Buffer.from(payload).toString())
-          // 排除自己发的消息
-          if (data.from_id === this.client_id) return
-          // 订阅房间,通知对方开始RTC
-          if (data.type === 'join') {
-            this.client_channel = data.channel
-            this.send({ type: 'startRTC' })
-          }
+          // 通知对方开始RTC
+          if (data.type === 'join' && data.channel) { this.client_channel = data.channel; this.send({ type: 'startRTC' }) }
+          // 排除自己的消息
+          if (!data.from_id || data.from_id === this.client_id) return
           // 离开就退出mqtt
           if (data.type === 'leave') this.disconnect()
-
           this.store.setMqttMessage({ ...data })
+          console.log('接收mqtt', data)
         } catch (error) {
           console.log('mqtt消息解析失败')
         }