1 Star 0 Fork 0

szmaozi / go-utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main1.go 3.32 KB
一键复制 编辑 原始数据 按行查看 历史
szmaozi 提交于 2023-11-24 19:59 . add mqttclient
/*
* Copyright (c) 2013 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Seth Hoenig
* Allan Stockdill-Mander
* Mike Robertson
*/
/*----------------------------------------------------------------------
This sample is designed to demonstrate the ability to set individual
callbacks on a per-subscription basis. There are three handlers in use:
brokerLoadHandler - $SYS/broker/load/#
brokerConnectionHandler - $SYS/broker/connection/#
brokerClientHandler - $SYS/broker/clients/#
The client will receive 100 messages total from those subscriptions,
and then print the total number of messages received from each.
It may take a few moments for the sample to complete running, as it
must wait for messages to be published.
https://www.cnblogs.com/saryli/p/11654665.html
-----------------------------------------------------------------------*/
package main
import (
"fmt"
"os"
"time"
MQTT "github.com/eclipse/paho.mqtt.golang"
)
var (
brokerLoad = make(chan bool)
brokerConnection = make(chan bool)
brokerClients = make(chan bool)
)
func brokerLoadHandler(client MQTT.Client, msg MQTT.Message) {
brokerLoad <- true
fmt.Printf("BrokerLoadHandler ")
fmt.Printf("[%s] ", msg.Topic())
fmt.Printf("%s\n", msg.Payload())
}
func brokerConnectionHandler(client MQTT.Client, msg MQTT.Message) {
brokerConnection <- true
fmt.Printf("BrokerConnectionHandler ")
fmt.Printf("[%s] ", msg.Topic())
fmt.Printf("%s\n", msg.Payload())
}
func brokerClientsHandler(client MQTT.Client, msg MQTT.Message) {
brokerClients <- true
fmt.Printf("BrokerClientsHandler ")
fmt.Printf("[%s] ", msg.Topic())
fmt.Printf("%s\n", msg.Payload())
}
func main() {
opts := MQTT.NewClientOptions().AddBroker("tcp://120.78.188.81:1883").SetClientID("router-sample")
opts.SetCleanSession(true)
opts.SetAutoReconnect(true)
opts.SetResumeSubs(true)
opts.SetKeepAlive(5 * time.Second)
opts.SetPingTimeout(5 * time.Second)
// opts.SetTLSConfig(&tls.Config{
// ClientAuth: tls.NoClientCert,
// ClientCAs: nil,
// InsecureSkipVerify: true,
// })
c := MQTT.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
if token := c.Subscribe("$SYS/broker/load/#", 0, brokerLoadHandler); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
os.Exit(1)
}
if token := c.Subscribe("$SYS/broker/connection/#", 0, brokerConnectionHandler); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
os.Exit(1)
}
if token := c.Subscribe("$SYS/broker/clients/#", 0, brokerClientsHandler); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
os.Exit(1)
}
loadCount := 0
connectionCount := 0
clientsCount := 0
for i := 0; i < 100; i++ {
select {
case <-brokerLoad:
loadCount++
case <-brokerConnection:
connectionCount++
case <-brokerClients:
clientsCount++
}
}
fmt.Printf("Received %3d Broker Load messages\n", loadCount)
fmt.Printf("Received %3d Broker Connection messages\n", connectionCount)
fmt.Printf("Received %3d Broker Clients messages\n", clientsCount)
c.Disconnect(250)
}
Go
1
https://gitee.com/szmaozi/go-utils.git
git@gitee.com:szmaozi/go-utils.git
szmaozi
go-utils
go-utils
29e02a007caf

搜索帮助