| 1 | Router Example |
| 2 | === |
| 3 | |
| 4 | AutoPaho provides the option to specify callbacks (`ClientConfig.OnPublishReceived`) that will be called everytime |
| 5 | a message is received. It's fairly common for users to want multiple callbacks with the message topic determining which |
| 6 | callback is called. Routers can provide this functionality. |
| 7 | |
| 8 | To use them first create a router: |
| 9 | |
| 10 | ``` |
| 11 | router := paho.NewStandardRouter() |
| 12 | ``` |
| 13 | |
| 14 | Configure `ClientConfig.OnPublishReceived` so the router is called: |
| 15 | |
| 16 | ```go |
| 17 | autopaho.ClientConfig{ |
| 18 | OnPublishReceived: []func (paho.PublishReceived) (bool, error){ |
| 19 | func (pr paho.PublishReceived) (bool, error) { |
| 20 | router.Route(pr.Packet.Packet()) |
| 21 | return true, nil // we assume that the router handles all messages (todo: amend router API) |
| 22 | }}, |
| 23 | } |
| 24 | ``` |
| 25 | |
| 26 | Now you can add/remove routes: |
| 27 | |
| 28 | ``` |
| 29 | router.DefaultHandler(func(p *paho.Publish) { fmt.Printf("defaulthandler received message with topic: %s\n", p.Topic) }) |
| 30 | router.RegisterHandler("test/test/#", func(p *paho.Publish) { fmt.Printf("test/test/# received message with topic: %s\n", p.Topic) }) |
| 31 | router.UnregisterHandler("test/test/#") |
| 32 | ``` |