|
|
@@ -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()
|