32 lines
789 B
TypeScript
32 lines
789 B
TypeScript
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)
|
|
})
|
|
}
|