Merge "Add a golang firehose example"
This commit is contained in:
commit
5d12849bba
@ -196,6 +196,43 @@ This requires the `mqtt-hs`_ library to be installed.
|
|||||||
terminated <- MQTT.run conf
|
terminated <- MQTT.run conf
|
||||||
print terminated
|
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
|
Websocket Example
|
||||||
-----------------
|
-----------------
|
||||||
In addition to using the raw MQTT protocol firehose.o.o provides a websocket
|
In addition to using the raw MQTT protocol firehose.o.o provides a websocket
|
||||||
|
Loading…
x
Reference in New Issue
Block a user