package main
import (
"fmt"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
func main() {
option := mqtt.NewClientOptions()
option.SetKeepAlive(30 * time.Second)
option.AddBroker("tcp://192.168.218.132:1883")
n := 0
option.SetDefaultPublishHandler(func(c mqtt.Client, m mqtt.Message) {
n++
if n%10000 == 0 {
n = 0
fmt.Printf("%s\n", m.Payload())
}
})
client := mqtt.NewClient(option)
token := client.Connect()
token.Wait()
fmt.Println("Connect()", token.Error())
tt := client.Subscribe("test2", 0, nil)
tt.Wait()
fmt.Println("Subscribe", tt.Error())
time.Sleep(time.Second * 3600)
client.Disconnect(250)
}
package main
import (
"fmt"
"strconv"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
func main() {
for i := 1; i < 100; i++ {
time.Sleep(time.Microsecond * 100)
go sender(strconv.Itoa(i) + "-")
}
time.Sleep(999999 * time.Second)
}
func sender(name string) {
option := mqtt.NewClientOptions()
option.SetKeepAlive(30 * time.Second)
option.AddBroker("tcp://192.168.218.132:1883")
client := mqtt.NewClient(option)
token := client.Connect()
token.Wait()
fmt.Println("Connect()", token.Error())
for i := 0; i < 99999; i++ {
tt := client.Publish("test2", 0, false, name+strconv.Itoa(i))
tt.Wait()
//fmt.Println("Publish", tt.Error())
time.Sleep(time.Microsecond * 100)
}
}