Last active
March 31, 2021 13:22
-
-
Save prohazko2/e02984fcacd185a48aaf0120e253e62d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"os/signal" | |
"syscall" | |
mqtt "github.com/eclipse/paho.mqtt.golang" | |
) | |
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() { | |
opts := mqtt.NewClientOptions() | |
opts.AddBroker("mqtt://dev.rightech.io:1883") | |
opts.SetClientID("") | |
opts.SetUsername("") | |
opts.SetPassword("") | |
opts.SetDefaultPublishHandler(messagePubHandler) | |
opts.OnConnect = connectHandler | |
opts.OnConnectionLost = connectLostHandler | |
client := mqtt.NewClient(opts) | |
if token := client.Connect(); token.Wait() && token.Error() != nil { | |
panic(token.Error()) | |
} | |
exit := make(chan os.Signal, 1) | |
signal.Notify(exit, syscall.SIGINT, syscall.SIGTERM) | |
<-exit | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment