mirror of
https://github.com/shirou/mqttcli.git
synced 2025-04-30 13:48:50 +08:00
not subscribe if publish or subscribe not set.
This commit is contained in:
parent
c4ca530d38
commit
5da1819b94
12
mqtt.go
12
mqtt.go
@ -59,10 +59,12 @@ func (m *MQTTClient) Disconnect() error {
|
|||||||
func (m *MQTTClient) SubscribeOnConnect(client *MQTT.Client) {
|
func (m *MQTTClient) SubscribeOnConnect(client *MQTT.Client) {
|
||||||
log.Infof("client connected")
|
log.Infof("client connected")
|
||||||
|
|
||||||
token := client.SubscribeMultiple(m.Subscribed, m.onMessageReceived)
|
if len(m.Subscribed) > 0 {
|
||||||
token.Wait()
|
token := client.SubscribeMultiple(m.Subscribed, m.onMessageReceived)
|
||||||
if token.Error() != nil {
|
token.Wait()
|
||||||
log.Error(token.Error())
|
if token.Error() != nil {
|
||||||
|
log.Error(token.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +73,7 @@ func (m *MQTTClient) ConnectionLost(client *MQTT.Client, reason error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *MQTTClient) onMessageReceived(client *MQTT.Client, message MQTT.Message) {
|
func (m *MQTTClient) onMessageReceived(client *MQTT.Client, message MQTT.Message) {
|
||||||
log.Infof("topic:%s / msg:%s", message.Topic(), message.Payload())
|
log.Infof("topic:%s / msg:%s", message.Topic(), message.Payload())
|
||||||
fmt.Println(string(message.Payload()))
|
fmt.Println(string(message.Payload()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user