| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package main |
| 17 |
|
| 18 |
import ( |
| 19 |
"context" |
| 20 |
"fmt" |
| 21 |
"net/url" |
| 22 |
"os" |
| 23 |
"os/signal" |
| 24 |
"strconv" |
| 25 |
"syscall" |
| 26 |
"time" |
| 27 |
|
| 28 |
"github.com/eclipse/paho.golang/autopaho" |
| 29 |
"github.com/eclipse/paho.golang/paho" |
| 30 |
) |
| 31 |
|
| 32 |
const clientID = "PahoGoClient" |
| 33 |
const topic = "PahoGoTestTopic" |
| 34 |
|
| 35 |
func main() { |
| 36 |
|
| 37 |
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) |
| 38 |
defer stop() |
| 39 |
|
| 40 |
|
| 41 |
u, err := url.Parse("mqtt://mqtt.eclipseprojects.io:1883") |
| 42 |
if err != nil { |
| 43 |
panic(err) |
| 44 |
} |
| 45 |
|
| 46 |
cliCfg := autopaho.ClientConfig{ |
| 47 |
ServerUrls: []*url.URL{u}, |
| 48 |
KeepAlive: 20, |
| 49 |
|
| 50 |
CleanStartOnInitialConnection: false, |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
SessionExpiryInterval: 60, |
| 56 |
OnConnectionUp: func(cm *autopaho.ConnectionManager, connAck *paho.Connack) { |
| 57 |
fmt.Println("mqtt connection up") |
| 58 |
|
| 59 |
|
| 60 |
if _, err := cm.Subscribe(context.Background(), &paho.Subscribe{ |
| 61 |
Subscriptions: []paho.SubscribeOptions{ |
| 62 |
{Topic: topic, QoS: 1}, |
| 63 |
}, |
| 64 |
}); err != nil { |
| 65 |
fmt.Printf("failed to subscribe (%s). This is likely to mean no messages will be received.", err) |
| 66 |
} |
| 67 |
fmt.Println("mqtt subscription made") |
| 68 |
}, |
| 69 |
OnConnectError: func(err error) { fmt.Printf("error whilst attempting connection: %s\n", err) }, |
| 70 |
|
| 71 |
ClientConfig: paho.ClientConfig{ |
| 72 |
|
| 73 |
ClientID: clientID, |
| 74 |
|
| 75 |
|
| 76 |
OnPublishReceived: []func(paho.PublishReceived) (bool, error){ |
| 77 |
func(pr paho.PublishReceived) (bool, error) { |
| 78 |
fmt.Printf("received message on topic %s; body: %s (retain: %t)\n", pr.Packet.Topic, pr.Packet.Payload, pr.Packet.Retain) |
| 79 |
return true, nil |
| 80 |
}}, |
| 81 |
OnClientError: func(err error) { fmt.Printf("client error: %s\n", err) }, |
| 82 |
OnServerDisconnect: func(d *paho.Disconnect) { |
| 83 |
if d.Properties != nil { |
| 84 |
fmt.Printf("server requested disconnect: %s\n", d.Properties.ReasonString) |
| 85 |
} else { |
| 86 |
fmt.Printf("server requested disconnect; reason code: %d\n", d.ReasonCode) |
| 87 |
} |
| 88 |
}, |
| 89 |
}, |
| 90 |
} |
| 91 |
|
| 92 |
c, err := autopaho.NewConnection(ctx, cliCfg) |
| 93 |
if err != nil { |
| 94 |
panic(err) |
| 95 |
} |
| 96 |
|
| 97 |
if err = c.AwaitConnection(ctx); err != nil { |
| 98 |
panic(err) |
| 99 |
} |
| 100 |
|
| 101 |
ticker := time.NewTicker(time.Second) |
| 102 |
msgCount := 0 |
| 103 |
defer ticker.Stop() |
| 104 |
for { |
| 105 |
select { |
| 106 |
case <-ticker.C: |
| 107 |
msgCount++ |
| 108 |
|
| 109 |
if _, err = c.Publish(ctx, &paho.Publish{ |
| 110 |
QoS: 1, |
| 111 |
Topic: topic, |
| 112 |
Payload: []byte("TestMessage: " + strconv.Itoa(msgCount)), |
| 113 |
}); err != nil { |
| 114 |
if ctx.Err() == nil { |
| 115 |
panic(err) |
| 116 |
} |
| 117 |
} |
| 118 |
continue |
| 119 |
case <-ctx.Done(): |
| 120 |
} |
| 121 |
break |
| 122 |
} |
| 123 |
|
| 124 |
fmt.Println("signal caught - exiting") |
| 125 |
<-c.Done() |
| 126 |
} |