| 1 |
/* |
| 2 |
* Copyright (c) 2024 Contributors to the Eclipse Foundation |
| 3 |
* |
| 4 |
* All rights reserved. This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 6 |
* and Eclipse Distribution License v1.0 which accompany this distribution. |
| 7 |
* |
| 8 |
* The Eclipse Public License is available at |
| 9 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 10 |
* and the Eclipse Distribution License is available at |
| 11 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 12 |
* |
| 13 |
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 14 |
*/ |
| 15 |
|
| 16 |
package main |
| 17 |
|
| 18 |
import ( |
| 19 |
"bufio" |
| 20 |
"context" |
| 21 |
"flag" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"log" |
| 25 |
"net" |
| 26 |
"os" |
| 27 |
"os/signal" |
| 28 |
"syscall" |
| 29 |
|
| 30 |
"github.com/eclipse/paho.golang/paho" |
| 31 |
) |
| 32 |
|
| 33 |
func main() { |
| 34 |
stdin := bufio.NewReader(os.Stdin) |
| 35 |
hostname, _ := os.Hostname() |
| 36 |
|
| 37 |
server := flag.String("server", "127.0.0.1:1883", "The full URL of the MQTT server to connect to") |
| 38 |
topic := flag.String("topic", hostname, "Topic to publish and receive the messages on") |
| 39 |
qos := flag.Int("qos", 0, "The QoS to send the messages at") |
| 40 |
name := flag.String("chatname", hostname, "The name to attach to your messages") |
| 41 |
clientid := flag.String("clientid", "", "A clientid for the connection") |
| 42 |
username := flag.String("username", "", "A username to authenticate to the MQTT server") |
| 43 |
password := flag.String("password", "", "Password to match username") |
| 44 |
flag.Parse() |
| 45 |
|
| 46 |
conn, err := net.Dial("tcp", *server) |
| 47 |
if err != nil { |
| 48 |
log.Fatalf("Failed to connect to %s: %s", *server, err) |
| 49 |
} |
| 50 |
|
| 51 |
c := paho.NewClient(paho.ClientConfig{ |
| 52 |
OnPublishReceived: []func(paho.PublishReceived) (bool, error){ |
| 53 |
func(pr paho.PublishReceived) (bool, error) { |
| 54 |
log.Printf("%s : %s", pr.Packet.Properties.User.Get("chatname"), string(pr.Packet.Payload)) |
| 55 |
return true, nil |
| 56 |
}}, |
| 57 |
Conn: conn, |
| 58 |
}) |
| 59 |
|
| 60 |
cp := &paho.Connect{ |
| 61 |
KeepAlive: 30, |
| 62 |
ClientID: *clientid, |
| 63 |
CleanStart: true, |
| 64 |
Username: *username, |
| 65 |
Password: []byte(*password), |
| 66 |
} |
| 67 |
|
| 68 |
if *username != "" { |
| 69 |
cp.UsernameFlag = true |
| 70 |
} |
| 71 |
if *password != "" { |
| 72 |
cp.PasswordFlag = true |
| 73 |
} |
| 74 |
|
| 75 |
ca, err := c.Connect(context.Background(), cp) |
| 76 |
if err != nil { |
| 77 |
log.Fatalln(err) |
| 78 |
} |
| 79 |
if ca.ReasonCode != 0 { |
| 80 |
log.Fatalf("Failed to connect to %s : %d - %s", *server, ca.ReasonCode, ca.Properties.ReasonString) |
| 81 |
} |
| 82 |
|
| 83 |
fmt.Printf("Connected to %s\n", *server) |
| 84 |
|
| 85 |
ic := make(chan os.Signal, 1) |
| 86 |
signal.Notify(ic, os.Interrupt, syscall.SIGTERM) |
| 87 |
go func() { |
| 88 |
<-ic |
| 89 |
fmt.Println("signal received, exiting") |
| 90 |
if c != nil { |
| 91 |
d := &paho.Disconnect{ReasonCode: 0} |
| 92 |
err := c.Disconnect(d) |
| 93 |
if err != nil { |
| 94 |
log.Fatalf("failed to send Disconnect: %s", err) |
| 95 |
} |
| 96 |
} |
| 97 |
os.Exit(0) |
| 98 |
}() |
| 99 |
|
| 100 |
if _, err := c.Subscribe(context.Background(), &paho.Subscribe{ |
| 101 |
Subscriptions: []paho.SubscribeOptions{ |
| 102 |
{Topic: *topic, QoS: byte(*qos), NoLocal: true}, |
| 103 |
}, |
| 104 |
}); err != nil { |
| 105 |
log.Fatalln(err) |
| 106 |
} |
| 107 |
|
| 108 |
log.Printf("Subscribed to %s", *topic) |
| 109 |
|
| 110 |
for { |
| 111 |
message, err := stdin.ReadString('\n') |
| 112 |
if err == io.EOF { |
| 113 |
os.Exit(0) |
| 114 |
} |
| 115 |
|
| 116 |
props := &paho.PublishProperties{} |
| 117 |
props.User.Add("chatname", *name) |
| 118 |
|
| 119 |
pb := &paho.Publish{ |
| 120 |
Topic: *topic, |
| 121 |
QoS: byte(*qos), |
| 122 |
Payload: []byte(message), |
| 123 |
Properties: props, |
| 124 |
} |
| 125 |
|
| 126 |
if _, err = c.Publish(context.Background(), pb); err != nil { |
| 127 |
log.Println(err) |
| 128 |
} |
| 129 |
} |
| 130 |
} |