本文整理汇总了Golang中git/eclipse/org/gitroot/paho/org/eclipse/paho/mqtt/golang/git.Client类的典型用法代码示例。如果您正苦于以下问题:Golang Client类的具体用法?Golang Client怎么用?Golang Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Client类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Publish
// メッセージを送信する。
func Publish(client *MQTT.Client, topic string, qos byte, retain bool, message string) {
token := client.Publish(topic, qos, retain, message)
if token.Wait() && token.Error() != nil {
fmt.Printf("Publish error: %s\n", token.Error())
}
}
示例2: loop
func loop(reader io.Reader, client *MQTT.Client) {
channel := make(chan data.SensorData)
scanner := bufio.NewScanner(reader)
go func() {
for scanner.Scan() {
line := strings.SplitN(scanner.Text(), ":", 2)
if nil == sensors.Sensors[line[0]] {
jww.DEBUG.Println(scanner.Text())
} else {
d := sensors.Sensors[line[0]].Parse(line[0], line[1])
channel <- d
}
}
close(channel)
}()
for data := range channel {
topic := "/gowx/sample"
buf := new(bytes.Buffer)
encoder := json.NewEncoder(buf)
encoder.Encode(data)
payload := buf.Bytes()
if token := client.Publish(topic, 0, false, payload); token.Wait() && token.Error() != nil {
jww.ERROR.Println("Failed to send message.", token.Error())
}
jww.DEBUG.Printf("Publishing %s -> %s\n", topic, buf.Bytes())
}
}
示例3: reportEvent
func (d *Device) reportEvent(client *MQTT.Client) {
for {
time.Sleep(3 * time.Second)
event := protocol.Event{}
params, err := tlv.MakeTLVs([]interface{}{"hello event."})
if err != nil {
fmt.Println(err)
return
}
event.Params = params
event.Head.No = 1
event.Head.SubDeviceid = 1
event.Head.ParamsCount = uint16(len(params))
payload, err := event.Marshal()
if err != nil {
fmt.Println(err)
return
}
client.Publish("e", 1, false, payload)
}
}
示例4: 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...")
}
}
示例5: SubscribeOnConnect
func (m *MQTTClient) SubscribeOnConnect(client *MQTT.Client) {
log.Infof("client connected")
if len(m.Subscribed) > 0 {
token := client.SubscribeMultiple(m.Subscribed, m.onMessageReceived)
token.Wait()
if token.Error() != nil {
log.Error(token.Error())
}
}
}
示例6: publishMessage
func publishMessage(client *mqtt.Client) {
if pubTopic != "" {
token := client.Publish(pubTopic, qos, false, pubMessage)
token.WaitTimeout(10 * time.Second)
if token.Error() != nil {
infoF("FATAL: Could not publish: %v", token.Error())
os.Exit(-1)
}
} else {
info("nothing to publish")
}
}
示例7: SubscribeOnConnect
func (b *Broker) SubscribeOnConnect(client *MQTT.Client) {
log.Infof("client connected")
b.connected = true
if b.Subscribed.Length() > 0 {
// subscribe
token := client.SubscribeMultiple(b.Subscribed.List(), b.onMessageReceived)
token.Wait()
if token.Error() != nil {
log.Error(token.Error())
}
}
}
示例8: 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
}
示例9: SendData
func SendData(client *MQTT.Client, endChan chan struct{}) {
ticker := time.NewTicker(1 * time.Second)
for {
select {
case <-endChan:
return
case <-ticker.C:
var p Payload
p.DataPoints.Load1, p.DataPoints.Load5, p.DataPoints.Load15 = getLoadAvg()
p.DataPoints.CPUTemp = getCPUTemp(host.CPUTempFile())
p.Timestamp = time.Now()
payloadBytes, err := json.Marshal(p)
if err == nil {
client.Publish(config.PubTopic, 0, false, payloadBytes)
} else {
fmt.Println(err.Error())
}
}
}
}
示例10: reportStatus
func (d *Device) reportStatus(client *MQTT.Client) {
payloadHead := protocol.DataHead{
Flag: 0,
Timestamp: uint64(time.Now().Unix() * 1000),
}
param := []interface{}{uint8(1)}
params, err := tlv.MakeTLVs(param)
if err != nil {
fmt.Println(err)
return
}
sub := protocol.SubData{
Head: protocol.SubDataHead{
SubDeviceid: uint16(1),
PropertyNum: uint16(1),
ParamsCount: uint16(len(params)),
},
Params: params,
}
status := protocol.Data{
Head: payloadHead,
SubData: []protocol.SubData{},
}
status.SubData = append(status.SubData, sub)
payload, err := status.Marshal()
if err != nil {
fmt.Println(err)
return
}
client.Publish("s", 1, false, payload)
}
示例11: Disconnect
// Brokerとの接続を切断する。
func Disconnect(client *MQTT.Client) {
client.Disconnect(10)
}
示例12: makeHandler
func makeHandler(c *MQTT.Client, cmd string) func(event lirc.Event) {
return func(event lirc.Event) {
c.Publish("/lights/set", 0, false, cmd)
}
}
示例13: Disconnect
// Brokerとの接続を切断する。
func Disconnect(client *MQTT.Client) {
client.ForceDisconnect()
}