Created
January 25, 2017 09:02
-
-
Save atotto/6406c0e579c6cd8c920ba53ba952f0f5 to your computer and use it in GitHub Desktop.
github.com/eclipse/paho.mqtt.golang example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mqtt_test | |
import ( | |
"sync" | |
"testing" | |
mqtt "github.com/eclipse/paho.mqtt.golang" | |
) | |
func TestMqttPubSub(t *testing.T) { | |
const TOPIC = "mytopic/test" | |
opts := mqtt.NewClientOptions().AddBroker("tcp://localhost:1883") | |
client := mqtt.NewClient(opts) | |
if token := client.Connect(); token.Wait() && token.Error() != nil { | |
t.Fatal(token.Error()) | |
} | |
var wg sync.WaitGroup | |
wg.Add(1) | |
if token := client.Subscribe(TOPIC, 0, func(client mqtt.Client, msg mqtt.Message) { | |
if string(msg.Payload()) != "mymessage" { | |
t.Fatalf("want mymessage, got %s", msg.Payload()) | |
} | |
wg.Done() | |
}); token.Wait() && token.Error() != nil { | |
t.Fatal(token.Error()) | |
} | |
if token := client.Publish(TOPIC, 0, false, "mymessage"); token.Wait() && token.Error() != nil { | |
t.Fatal(token.Error()) | |
} | |
wg.Wait() | |
} |
Author
atotto
commented
Jan 25, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment