-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmqtt_test.go
53 lines (44 loc) · 1.2 KB
/
mqtt_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
mqtt2 "github.com/eclipse/paho.mqtt.golang"
mqtt "iotClient/protocol/mqtt"
"testing"
"time"
)
func initMqtt() mqtt.MqttClient {
var mqttSer mqtt.MqttClient = &mqtt.TcpClient{
Broker: "tcp://192.168.31.201:1883",
ClientId: "mqttx_123456",
MessagePubHandler: MessageH,
ConnectHandler: MessageOnConnect,
ConnectLostHandler: ConnectionLostHandler,
}
_ = mqttSer.InitMqtt()
return mqttSer
}
func Test_initMqtt(t *testing.T) {
m := initMqtt()
for i := 0; i < 100; i++ {
_ = m.Publish("xx-topic", fmt.Sprintf("test123456_%v", i))
time.Sleep(1 * time.Second)
}
select {}
}
func Test_Subscribe(t *testing.T) {
m := initMqtt()
m.Subscribe("test/123456", SubscribeFunc)
select {}
}
func MessageH(client mqtt2.Client, message mqtt2.Message) {
fmt.Printf("Received message: %s from topic: %s\n", message.Payload(), message.Topic())
}
func MessageOnConnect(client mqtt2.Client) {
fmt.Println("hello1")
}
func ConnectionLostHandler(client mqtt2.Client, err error) {
fmt.Println("hello2")
}
var SubscribeFunc mqtt.SubscribeHandler = func(client mqtt2.Client, message mqtt2.Message) {
fmt.Println(message.Topic(), string(message.Payload()))
}