Add a golang firehose example
This commit adds a firehose example for go to the firehose docs. Change-Id: I29c1abd9c152fc8fb196b2a4e1734c6642d266dc
This commit is contained in:
parent
d2e3cc232f
commit
b3ba0e0046
@ -196,6 +196,43 @@ This requires the `mqtt-hs`_ library to be installed.
|
||||
terminated <- MQTT.run conf
|
||||
print terminated
|
||||
|
||||
Go
|
||||
''
|
||||
.. code-block:: go
|
||||
package main
|
||||
import (
|
||||
"fmt"
|
||||
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
func onMessageReceived(client MQTT.Client, msg MQTT.Message) {
|
||||
fmt.Printf("TOPIC: %s\n", msg.Topic())
|
||||
fmt.Printf("MSG: %s\n", msg.Payload())
|
||||
}
|
||||
func main() {
|
||||
hostname, _ := os.Hostname()
|
||||
opts := &MQTT.ClientOptions{
|
||||
ClientID: hostname+strconv.Itoa(time.Now().Second()),
|
||||
}
|
||||
opts.AddBroker("tcp://firehose.openstack.org:1883")
|
||||
opts.OnConnect = func(c MQTT.Client) {
|
||||
if token := c.Subscribe("#", 0, onMessageReceived); token.Wait() && token.Error() != nil {
|
||||
fmt.Println(token.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
client := MQTT.NewClient(opts)
|
||||
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
||||
panic(token.Error())
|
||||
}
|
||||
for {
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Websocket Example
|
||||
-----------------
|
||||
In addition to using the raw MQTT protocol firehose.o.o provides a websocket
|
||||
|
Loading…
x
Reference in New Issue
Block a user