Browse Source

增加自定义服务地址

Caner 2 years ago
parent
commit
0df20c21e8
3 changed files with 25 additions and 21 deletions
  1. 17 13
      electron/main.js
  2. 1 1
      src/App.vue
  3. 7 7
      src/components/login.vue

+ 17 - 13
electron/main.js

@@ -146,19 +146,23 @@ class MainSerivce {
 
 
   #initMqtt(data) {
   #initMqtt(data) {
     this.#closeMqtt()
     this.#closeMqtt()
-    this.#client = mqtt.connect(data.url, { username: data.room, password: data.name })
-    // 监听
-    this.#client.on('connect', () => this.mainWin.webContents.send('message', { type: 'connect' }))
-    this.#client.on('message', (_, msg, __) => {
-      try {
-        const db = JSON.parse(msg.toString())
-        if (db.type === 'join') this.#mqttChannel = db.channel
-        this.mainWin.webContents.send('message', db)
-      } catch (error) {
-        console.log(error);
-      }
-    })
-    this.#client.on('error', (e) => this.#closeMqtt())
+    try {
+      this.#client = mqtt.connect(data.url, { username: data.room, password: data.name })
+      // 监听
+      this.#client.on('connect', () => this.mainWin.webContents.send('message', { type: 'connect' }))
+      this.#client.on('message', (_, msg, __) => {
+        try {
+          const db = JSON.parse(msg.toString())
+          if (db.type === 'join') this.#mqttChannel = db.channel
+          this.mainWin.webContents.send('message', db)
+        } catch (error) {
+          console.log(error);
+        }
+      })
+      this.#client.on('error', (e) => { this.mainWin.webContents.send('message', { type: 'disconnect' }); this.#closeMqtt() })
+    } catch (error) {
+      this.mainWin.webContents.send('message', { type: 'disconnect' })
+    }
   }
   }
 
 
   #closeMqtt() {
   #closeMqtt() {

+ 1 - 1
src/App.vue

@@ -130,7 +130,7 @@ function sendAudio(blob: Blob) {
 function titleEvent(type: string) {
 function titleEvent(type: string) {
   if (type === 'maxWin') winMaxOrMin.value = !winMaxOrMin.value
   if (type === 'maxWin') winMaxOrMin.value = !winMaxOrMin.value
   if (type === 'loginOut') {
   if (type === 'loginOut') {
-    close('手动退出')
+    close()
     window.$electron.send('closeMqtt')
     window.$electron.send('closeMqtt')
   } else {
   } else {
     window.$electron?.send(type, winMaxOrMin.value)
     window.$electron?.send(type, winMaxOrMin.value)

+ 7 - 7
src/components/login.vue

@@ -11,15 +11,15 @@ const emit = defineEmits<{(evt: 'update:modelValue', value: string): void
 }>()
 }>()
 
 
 function login() {
 function login() {
-  if (name.value && room.value) emit('loginBack', { name: name.value, room: room.value, url: url.value })
+  if (name.value && room.value && url.value && url.value.includes('mqtt://')) emit('loginBack', { name: name.value, room: room.value, url: url.value })
 }
 }
 
 
 function titleEvent(type: string) {
 function titleEvent(type: string) {
   window.$electron?.send(type)
   window.$electron?.send(type)
 }
 }
 
 
-watch([ name, room ], () => {
-  if (name.value && room.value) emit('update:modelValue', '')
+watch([ name, room, url ], () => {
+  if (name.value && room.value && url.value) emit('update:modelValue', '')
 })
 })
 </script>
 </script>
 <template>
 <template>
@@ -47,8 +47,8 @@ watch([ name, room ], () => {
           <input
           <input
             v-model="url"
             v-model="url"
             type="text"
             type="text"
-            placeholder="请输入地址"
-            maxlength="20"
+            placeholder="mqtt://*******"
+            maxlength="50"
           >
           >
         </div>
         </div>
         <div>
         <div>
@@ -78,8 +78,8 @@ watch([ name, room ], () => {
         <span>{{ err }}</span>
         <span>{{ err }}</span>
         <div>
         <div>
           <button
           <button
-            :disabled="!name && !room"
-            :class="{ resetStyle: name && room }"
+            :disabled="!name && !room && !url && !url.includes('mqtt://')"
+            :class="{ resetStyle: name && room && url && url.includes('mqtt://')}"
             @click="login"
             @click="login"
           >
           >
             加入
             加入