golang连接mqtt发送和订阅消费消息示例代码
代码语言:golang
所属分类:其他
代码描述:golang连接mqtt发送和订阅消费消息示例代码
代码标签: golang 连接 mqtt 发送 订阅 消费 消息 示例 代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
package main import ( "fmt" mqtt "github.com/eclipse/paho.mqtt.golang" "time" ) var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) { fmt.Printf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic()) } var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) { fmt.Println("Connected") } var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) { fmt.Printf("Connect lost: %v", err) } func main() { var broker = "127.0.0.1" var port = 1883 opts := mqtt.NewClientOptions() opts.AddBroker(fmt.Sprintf("tcp://%s:%d", broker, port)) opts.SetClientID("go_mqtt_client") opts.SetUsername("emqx") opts.Set.........完整代码请登录后点击上方下载按钮下载查看
网友评论0