修改WebSocket地址从nginx中读取

This commit is contained in:
GGJ
2025-04-14 11:13:18 +08:00
parent 1232322eb4
commit 7558f23c71
2 changed files with 12 additions and 8 deletions

View File

@@ -38,19 +38,23 @@ export default class SocketService {
} }
// 定义连接服务器的方法 // 定义连接服务器的方法
public connect(id: string): void { public async connect(id: string) {
if (!window.WebSocket) { if (!window.WebSocket) {
console.log('您的浏览器不支持WebSocket') console.log('您的浏览器不支持WebSocket')
return return
} }
const url = 'ws://192.168.1.67:10407/api/pushMessage/' + id const response = await fetch('/')
this.ws = new WebSocket(url) const mqttUrl = response.headers.get('X-Mqtt-Url')
setTimeout(() => {
const url = (mqttUrl || 'ws://192.168.1.67:10407/api/pushMessage/') + id
this.ws = new WebSocket(url)
this.ws.onopen = () => this.handleOpen() this.ws.onopen = () => this.handleOpen()
this.ws.onclose = () => this.handleClose() this.ws.onclose = () => this.handleClose()
this.ws.onerror = () => this.handleError() this.ws.onerror = () => this.handleError()
this.ws.onmessage = event => this.handleMessage(event) this.ws.onmessage = event => this.handleMessage(event)
}, 0)
} }
// 处理连接成功事件 // 处理连接成功事件

View File

@@ -73,7 +73,7 @@ import Shishishuju from './shishishuju/index.vue'
import router from '@/router' import router from '@/router'
defineOptions({ defineOptions({
name: 'harmonic-boot/monitor/online' // name: 'harmonic-boot/monitor/online'
}) })
const monitoringPoint = useMonitoringPoint() const monitoringPoint = useMonitoringPoint()