| 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 |
"syscall" |
| 25 |
|
| 26 |
"github.com/eclipse/paho.golang/autopaho" |
| 27 |
"github.com/eclipse/paho.golang/paho" |
| 28 |
) |
| 29 |
|
| 30 |
const clientID = "PahoGoClient" |
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
router := paho.NewStandardRouter() |
| 47 |
router.DefaultHandler(func(p *paho.Publish) { fmt.Printf("defaulthandler received message with topic: %s\n", p.Topic) }) |
| 48 |
|
| 49 |
cliCfg := autopaho.ClientConfig{ |
| 50 |
ServerUrls: []*url.URL{u}, |
| 51 |
KeepAlive: 20, |
| 52 |
|
| 53 |
CleanStartOnInitialConnection: true, |
| 54 |
SessionExpiryInterval: 0, |
| 55 |
OnConnectError: func(err error) { fmt.Printf("error whilst attempting connection: %s\n", err) }, |
| 56 |
|
| 57 |
ClientConfig: paho.ClientConfig{ |
| 58 |
|
| 59 |
ClientID: clientID, |
| 60 |
|
| 61 |
|
| 62 |
OnPublishReceived: []func(paho.PublishReceived) (bool, error){ |
| 63 |
func(pr paho.PublishReceived) (bool, error) { |
| 64 |
router.Route(pr.Packet.Packet()) |
| 65 |
return true, nil |
| 66 |
}}, |
| 67 |
OnClientError: func(err error) { fmt.Printf("client error: %s\n", err) }, |
| 68 |
OnServerDisconnect: func(d *paho.Disconnect) { |
| 69 |
if d.Properties != nil { |
| 70 |
fmt.Printf("server requested disconnect: %s\n", d.Properties.ReasonString) |
| 71 |
} else { |
| 72 |
fmt.Printf("server requested disconnect; reason code: %d\n", d.ReasonCode) |
| 73 |
} |
| 74 |
}, |
| 75 |
}, |
| 76 |
} |
| 77 |
|
| 78 |
c, err := autopaho.NewConnection(ctx, cliCfg) |
| 79 |
if err != nil { |
| 80 |
panic(err) |
| 81 |
} |
| 82 |
|
| 83 |
if err = c.AwaitConnection(ctx); err != nil { |
| 84 |
panic(err) |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if _, err := c.Subscribe(context.Background(), &paho.Subscribe{ |
| 90 |
Subscriptions: []paho.SubscribeOptions{ |
| 91 |
{Topic: "test/#", QoS: 1}, |
| 92 |
}, |
| 93 |
}); err != nil { |
| 94 |
panic(fmt.Sprintf("failed to subscribe (%s). This is likely to mean no messages will be received.", err)) |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
router.RegisterHandler("test/test/#", func(p *paho.Publish) { fmt.Printf("test/test/# received message with topic: %s\n", p.Topic) }) |
| 100 |
router.RegisterHandler("test/test/foo", func(p *paho.Publish) { fmt.Printf("test/test/foo received message with topic: %s\n", p.Topic) }) |
| 101 |
router.RegisterHandler("test/nomatch", func(p *paho.Publish) { fmt.Printf("test/nomatch received message with topic: %s\n", p.Topic) }) |
| 102 |
router.RegisterHandler("test/quit", func(p *paho.Publish) { stop() }) |
| 103 |
|
| 104 |
|
| 105 |
topics := []string{"test/test", "test/test/foo", "test/xxNoMatch", "test/quit"} |
| 106 |
for _, t := range topics { |
| 107 |
if _, err := c.Publish(ctx, &paho.Publish{ |
| 108 |
QoS: 1, |
| 109 |
Topic: t, |
| 110 |
Payload: []byte("TestMessage on topic: " + t), |
| 111 |
}); err != nil { |
| 112 |
if ctx.Err() == nil { |
| 113 |
panic(err) |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
<-c.Done() |
| 119 |
} |