本文整理汇总了Golang中git/eclipse/org/gitroot/paho/org/eclipse/paho/mqtt/golang/git.Client.Subscribe方法的典型用法代码示例。如果您正苦于以下问题:Golang Client.Subscribe方法的具体用法?Golang Client.Subscribe怎么用?Golang Client.Subscribe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类git/eclipse/org/gitroot/paho/org/eclipse/paho/mqtt/golang/git.Client
的用法示例。
在下文中一共展示了Client.Subscribe方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: setupSubscriptions
func setupSubscriptions(client *mqtt.Client, wg *sync.WaitGroup) {
if topicFiltersString != "" {
wg.Add(numberMessagesExpected)
topicFilters := strings.Split(topicFiltersString, ",")
for _, filter := range topicFilters {
client.Subscribe(filter, qos, func(client *mqtt.Client, msg mqtt.Message) {
if !msg.Retained() || !ignoreRetained {
payload := bytes.NewBuffer(msg.Payload()).String()
if ignorePayload {
fmt.Printf("r:%v, t:%v, s:%v\n", msg.Retained(), msg.Topic(), len(payload))
} else {
infoF("r:%v, t:%v, s:%v", msg.Retained(), msg.Topic(), len(payload))
fmt.Printf("%v\n", payload)
}
wg.Done()
} else {
info("ignoring retained msg")
}
})
}
infoF("waiting for %v msgs.", numberMessagesExpected)
} else {
info("No subscriptions...")
}
}
示例2: Subscribe
// メッセージを受信する。
func Subscribe(client *MQTT.Client, topic string, qos byte) *SubscribeResult {
var result *SubscribeResult = &SubscribeResult{}
result.Count = 0
var handler MQTT.MessageHandler = func(client *MQTT.Client, msg MQTT.Message) {
result.Count++
if Debug {
fmt.Printf("Received message : topic=%s, message=%s\n", msg.Topic(), msg.Payload())
}
}
token := client.Subscribe(topic, qos, handler)
if token.Wait() && token.Error() != nil {
fmt.Printf("Subscribe error: %s\n", token.Error())
}
return result
}