本文整理匯總了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
}