Mqtt - Go Packages
Có thể bạn quan tâm
mqtt
An mqtt client for Go that improves usability over the paho.mqtt.golang library it wraps. Made for 🧑.
installation
go get github.com/lucacasonato/mqtt import "github.com/lucacasonato/mqtt" // or import ( "github.com/lucacasonato/mqtt" )usage
creating a client & connecting
client, err := mqtt.NewClient(mqtt.ClientOptions{ // required Servers: []string{ "tcp://test.mosquitto.org:1883", }, // optional ClientID: "my-mqtt-client", Username: "admin", Password: "***", AutoReconnect: true, }) if err != nil { panic(err) } err = client.Connect(context.WithTimeout(2 * time.Second)) if err != nil { panic(err) }You can use any of these schemes for the broker tcp (unesecured), ssl (secured), ws (unsecured), wss (secured).
disconnecting from a client
client.Disconnect()publishing a message
bytes
err := client.Publish(context.WithTimeout(1 * time.Second), "api/v0/main/client1", []byte(0, 1 ,2, 3), mqtt.AtLeastOnce) if err != nil { panic(err) }string
err := client.PublishString(context.WithTimeout(1 * time.Second), "api/v0/main/client1", "hello world", mqtt.AtLeastOnce) if err != nil { panic(err) }json
err := client.PublishJSON(context.WithTimeout(1 * time.Second), "api/v0/main/client1", []string("hello", "world"), mqtt.AtLeastOnce) if err != nil { panic(err) }subscribing
err := client.Subscribe(context.WithTimeout(1 * time.Second), "api/v0/main/client1", mqtt.AtLeastOnce) if err != nil { panic(err) } err := client.SubscribeMultiple(context.WithTimeout(1 * time.Second), map[string]mqtt.QOS{ "api/v0/main/client1": mqtt.AtLeastOnce, }) if err != nil { panic(err) }handling
route := client.Handle("api/v0/main/client1", func(message mqtt.Message) { v := interface{}{} err := message.PayloadJSON(&v) if err != nil { panic(err) } fmt.Printf("recieved a message with content %v\n", v) }) // once you are done with the route you can stop handling it route.Stop()listening
messages, route := client.Listen("api/v0/main/client1") for { message := <-messages fmt.Printf("recieved a message with content %v\n", message.PayloadString()) } // once you are done with the route you can stop handling it route.Stop()Từ khóa » Mqtt Github C
-
Eclipse Paho C Client Library For The MQTT Protocol - GitHub
-
A Portable MQTT C Client For Embedded Systems And PCs Alike.
-
Eclipse Mosquitto - An Open Source MQTT Broker - GitHub
-
Eclipse Paho MQTT C/C++ Client For Embedded Platforms - GitHub
-
Awslabs/aws-c-mqtt: C99 Implementation Of The MQTT 3.1.1 ... - GitHub
-
Eclipse Paho MQTT-SN C/C++ Client For Embedded Platforms - GitHub
-
Paho MQTT C Client Library
-
MQTT C Client Library | EMQX 4.4 Documentation
-
Eclipse Paho MQTT C Client
-
Connecting To The MQTT Broker Using The Open-source C Language ...
-
Use The Paho MQTT C Client - Best Practices - Alibaba Cloud's
-
Eclipse Mosquitto
-
Eclipse Paho MQTT Go Client - Go Packages