Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "github.com/eclipse/paho.mqtt.golang"
- "sync"
- "github.com/sirupsen/logrus"
- )
- // MQ is MQTT client struct
- type MQ struct {
- Addr string
- PubClientID string
- SubClientID string
- Login string
- Password string
- TopicIn string
- TopicOut string
- QoS byte
- onceIn sync.Once
- onceOut sync.Once
- pubClient mqtt.Client
- subClient mqtt.Client
- }
- func main() {
- mqClient := MQ{
- Addr: fmt.Sprintf("%s:%d", "datarouter.maximus.lan", 1883),
- PubClientID: "5f81487b-c75e-412c-9184-a028f8da7419",
- SubClientID: "6be68ef4-1c46-4b4f-9146-5e132761a995",
- Login: "test",
- Password: "SkXTKTBN6C",
- TopicIn: "services/integration/IN",
- TopicOut: "services/integration/OUT",
- QoS: 0,
- }
- message := "test message"
- test := mqClient.GetPubClient()
- fmt.Printf("%+v", test)
- t := mqClient.GetPubClient().Publish(mqClient.TopicOut, mqClient.QoS, false, message)
- t.Wait()
- z := t.Error()
- if z != nil {
- fmt.Printf("%+v", z)
- logrus.WithFields(logrus.Fields{
- "error": t.Error(),
- "message": string(message),
- }).Error("Can't send message to queue")
- }
- fmt.Printf("%+v", t)
- }
- // GetPubClient returns PUB MQTT connection
- func (m *MQ) GetPubClient() mqtt.Client {
- m.onceOut.Do(func() {
- mqtt.ERROR = logrus.New()
- opts := mqtt.NewClientOptions()
- opts = opts.AddBroker("tcp://" + m.Addr)
- opts.ClientID = m.PubClientID
- opts.Username = m.Login
- opts.Password = m.Password
- m.pubClient = mqtt.NewClient(opts)
- token := m.pubClient.Connect()
- token.Wait()
- if token.Error() != nil {
- }
- })
- return m.pubClient
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement