/* * Copyright (c) 2024 Contributors to the Eclipse Foundation * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * and Eclipse Distribution License v1.0 which accompany this distribution. * * The Eclipse Public License is available at * https://www.eclipse.org/legal/epl-2.0/ * and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.php. * * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause */ package main import ( "context" "flag" "fmt" "log" "net" "os" "os/signal" "syscall" "github.com/eclipse/paho.golang/paho" ) func main() { server := flag.String("server", "127.0.0.1:1883", "The MQTT server to connect to ex: 127.0.0.1:1883") topic := flag.String("topic", "#", "Topic to subscribe to") qos := flag.Int("qos", 0, "The QoS to subscribe to messages at") clientid := flag.String("clientid", "", "A clientid for the connection") username := flag.String("username", "", "A username to authenticate to the MQTT server") password := flag.String("password", "", "Password to match username") flag.Parse() logger := log.New(os.Stdout, "SUB: ", log.LstdFlags) msgChan := make(chan *paho.Publish) conn, err := net.Dial("tcp", *server) if err != nil { log.Fatalf("Failed to connect to %s: %s", *server, err) } c := paho.NewClient(paho.ClientConfig{ OnPublishReceived: []func(paho.PublishReceived) (bool, error){ // Noop handler func(pr paho.PublishReceived) (bool, error) { msgChan <- pr.Packet return true, nil }}, Conn: conn, }) c.SetDebugLogger(logger) c.SetErrorLogger(logger) cp := &paho.Connect{ KeepAlive: 30, ClientID: *clientid, CleanStart: true, Username: *username, Password: []byte(*password), } if *username != "" { cp.UsernameFlag = true } if *password != "" { cp.PasswordFlag = true } ca, err := c.Connect(context.Background(), cp) if err != nil { log.Fatalln(err) } if ca.ReasonCode != 0 { log.Fatalf("Failed to connect to %s : %d - %s", *server, ca.ReasonCode, ca.Properties.ReasonString) } fmt.Printf("Connected to %s\n", *server) ic := make(chan os.Signal, 1) signal.Notify(ic, os.Interrupt, syscall.SIGTERM) go func() { <-ic fmt.Println("signal received, exiting") if c != nil { d := &paho.Disconnect{ReasonCode: 0} c.Disconnect(d) } os.Exit(0) }() sa, err := c.Subscribe(context.Background(), &paho.Subscribe{ Subscriptions: []paho.SubscribeOptions{ {Topic: *topic, QoS: byte(*qos)}, }, }) if err != nil { log.Fatalln(err) } if sa.Reasons[0] != byte(*qos) { log.Fatalf("Failed to subscribe to %s : %d", *topic, sa.Reasons[0]) } log.Printf("Subscribed to %s", *topic) for m := range msgChan { log.Println("Received message:", string(m.Payload)) } }