Browse Source

更新消息格式

caner 1 year ago
parent
commit
8f51f0a005
2 changed files with 9 additions and 7 deletions
  1. 1 1
      src/pages/room/index.vue
  2. 8 6
      src/services/mqtt.service.ts

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

@@ -71,7 +71,7 @@ watch(() => store.mqtt_message, async (value: { type: string, data?: RTCSessionD
   // 发送本地answer
   if (type === 'answer') {
     console.log('send answer', data)
-    mqtt.send(JSON.stringify(data))
+    mqtt.send(data)
   }
 })
 

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

@@ -38,14 +38,15 @@ export default class MqttService {
 
   /**
    * 发送消息
-   * @param params string
+   * @param params object
    * @param qos number  0 | 1 | 2
    * @returns
    */
-  async send(params: string, qos = 0) {
+  async send(params: object, qos = 0) {
     console.log('发送', this.client_channel, this.mqtt_id, params)
     if (!this.client_channel || !this.mqtt_id) return null
-    return await publish(this.mqtt_id, this.client_channel, qos, false, params)
+    const newParams = JSON.stringify({ ...params, from_id: this.client_id })
+    return await publish(this.mqtt_id, this.client_channel, qos, false, newParams)
   }
 
   /**
@@ -75,15 +76,16 @@ export default class MqttService {
       }
 
       // 收到消息
-      // TODO:需要排除自己发的消息
       if (event.message) {
         try {
           const { payload } = event.message
           const data = JSON.parse(Buffer.from(payload).toString())
-          // 订阅对方,通知对方开始RTC
+          // 排除自己发的消息
+          if (data.from_id === this.client_id) return
+          // 订阅房间,通知对方开始RTC
           if (data.type === 'join') {
             this.client_channel = data.channel
-            this.send(JSON.stringify({ type: 'startRTC' }))
+            this.send({ type: 'startRTC' })
           }
           // 离开就退出mqtt
           if (data.type === 'leave') this.disconnect()