/* * 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 ( "bufio" "context" "flag" "fmt" "io" "log" "net" "os" "os/signal" "syscall" "github.com/eclipse/paho.golang/paho" ) func main() { stdin := bufio.NewReader(os.Stdin) hostname, _ := os.Hostname() server := flag.String("server", "127.0.0.1:1883", "The full URL of the MQTT server to connect to") topic := flag.String("topic", hostname, "Topic to publish the messages on") qos := flag.Int("qos", 0, "The QoS to send the messages at") retained := flag.Bool("retained", false, "Are the messages sent with the retained flag") 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() conn, err := net.Dial("tcp", *server) if err != nil { log.Fatalf("Failed to connect to %s: %s", *server, err) } c := paho.NewClient(paho.ClientConfig{ Conn: conn, }) cp := &paho.Connect{ KeepAlive: 30, ClientID: *clientid, CleanStart: true, Username: *username, Password: []byte(*password), } if *username != "" { cp.UsernameFlag = true } if *password != "" { cp.PasswordFlag = true } log.Println(cp.UsernameFlag, cp.PasswordFlag) 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) }() for { message, err := stdin.ReadString('\n') if err == io.EOF { os.Exit(0) } if _, err = c.Publish(context.Background(), &paho.Publish{ Topic: *topic, QoS: byte(*qos), Retain: *retained, Payload: []byte(message), }); err != nil { log.Println("error sending message:", err) continue } log.Println("sent") } }