|
...
|
...
|
@@ -3,8 +3,9 @@ package mqtt |
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
pahomqtt "github.com/eclipse/paho.mqtt.golang"
|
|
|
|
"github.com/linmadan/egglib-go/log"
|
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
|
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
|
|
|
|
"runtime/debug"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
...
|
...
|
@@ -12,12 +13,15 @@ type SubscribeClient struct { |
|
|
|
topic string
|
|
|
|
handler pahomqtt.MessageHandler
|
|
|
|
client pahomqtt.Client
|
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessageHandler pahomqtt.MessageHandler
|
|
|
|
|
|
|
|
func NewSubscribeClient() *SubscribeClient {
|
|
|
|
return &SubscribeClient{}
|
|
|
|
func NewSubscribeClient(log log.Logger) *SubscribeClient {
|
|
|
|
return &SubscribeClient{
|
|
|
|
log: log,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (subscribeClient *SubscribeClient) options() *pahomqtt.ClientOptions {
|
|
...
|
...
|
@@ -34,28 +38,32 @@ func (subscribeClient *SubscribeClient) options() *pahomqtt.ClientOptions { |
|
|
|
|
|
|
|
func (subscribeClient *SubscribeClient) Connect() *SubscribeClient {
|
|
|
|
opts := subscribeClient.options()
|
|
|
|
fmt.Println("start connect......")
|
|
|
|
subscribeClient.log.Info("mqtt start connect......")
|
|
|
|
opts.OnConnectionLost = func(c pahomqtt.Client, err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
log.Logger.Error(fmt.Sprintf("%v", r))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
fmt.Println("Connect error:", err)
|
|
|
|
for {
|
|
|
|
fmt.Println("reconnect server")
|
|
|
|
token := subscribeClient.client.Connect()
|
|
|
|
token.Wait()
|
|
|
|
fmt.Println("server Connect status:", subscribeClient.client.IsConnectionOpen())
|
|
|
|
if subscribeClient.client.IsConnectionOpen() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
}
|
|
|
|
//defer func() {
|
|
|
|
// if r := recover(); r != nil {
|
|
|
|
// subscribeClient.log.Info(fmt.Sprintf("%v %s", r, debug.Stack()))
|
|
|
|
// }
|
|
|
|
//}()
|
|
|
|
subscribeClient.log.Info("mqtt connect lost,error:" + err.Error())
|
|
|
|
//for {
|
|
|
|
// subscribeClient.log.Info("reconnect server")
|
|
|
|
// token := subscribeClient.client.Connect()
|
|
|
|
// token.Wait()
|
|
|
|
// subscribeClient.log.Info(fmt.Sprintf("server Connect status:%v", subscribeClient.client.IsConnectionOpen()))
|
|
|
|
// if subscribeClient.client.IsConnectionOpen() {
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// time.Sleep(3 * time.Second)
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
opts.OnConnect = func(c pahomqtt.Client) {
|
|
|
|
subscribeClient.log.Info("mqtt reconnected")
|
|
|
|
c.Subscribe(subscribeClient.topic, 0, subscribeClient.handler)
|
|
|
|
}
|
|
|
|
opts.OnReconnecting = func(client pahomqtt.Client, options *pahomqtt.ClientOptions) {
|
|
|
|
subscribeClient.log.Info("mqtt reconnecting...")
|
|
|
|
}
|
|
|
|
subscribeClient.client = pahomqtt.NewClient(opts)
|
|
|
|
token := subscribeClient.client.Connect()
|
|
|
|
token.Wait()
|
|
...
|
...
|
@@ -70,13 +78,13 @@ func (subscribeClient *SubscribeClient) Subscribe(topic string, messageHandler p |
|
|
|
token.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartSubscribe(topic string, handler MessageHandler) {
|
|
|
|
func StartSubscribe(topic string, handler MessageHandler, log log.Logger) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
StartSubscribe(topic, handler)
|
|
|
|
log.Error(fmt.Sprintf("%s", debug.Stack()))
|
|
|
|
StartSubscribe(topic, handler, log)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
fmt.Println("start subscribe...")
|
|
|
|
NewSubscribeClient().Connect().Subscribe(topic, pahomqtt.MessageHandler(handler))
|
|
|
|
log.Info("mqtt start subscribe...")
|
|
|
|
NewSubscribeClient(log).Connect().Subscribe(topic, pahomqtt.MessageHandler(handler))
|
|
|
|
} |
...
|
...
|
|