step: migrate to vue.js
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import mqtt from 'mqtt'
|
||||
|
||||
const mqttBrokerHost = '91.99.29.8'
|
||||
const mqttBrokerPort = 9001
|
||||
const mqttBrokerPath = '/mqtt'
|
||||
const mqttUseTls = false
|
||||
const mqttTopic = 'king-arthur/page/startup'
|
||||
const mqttMessage = 'start-hit'
|
||||
|
||||
let client: any = null
|
||||
|
||||
export function getMqttClient(): any {
|
||||
return client
|
||||
}
|
||||
|
||||
export function publishStartHit(): void {
|
||||
const protocol = mqttUseTls ? 'wss' : 'ws'
|
||||
const brokerUrl = `${protocol}://${mqttBrokerHost}:${mqttBrokerPort}${mqttBrokerPath}`
|
||||
const tempClient = mqtt.connect(brokerUrl)
|
||||
|
||||
tempClient.on('connect', () => {
|
||||
tempClient.publish(mqttTopic, mqttMessage, { qos: 1 }, () => {
|
||||
tempClient.end()
|
||||
})
|
||||
})
|
||||
|
||||
tempClient.on('error', (error: any) => {
|
||||
console.error('MQTT publish failed:', error)
|
||||
tempClient.end(true)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user