From e03a22cc6e359108bd2d4f8e53e7e1618099e500 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期三, 28 四月 2021 18:04:38 +0800
Subject: [PATCH] start heartbeat after register

---
 micronode.go |   16 +++++-----------
 1 files changed, 5 insertions(+), 11 deletions(-)

diff --git a/micronode.go b/micronode.go
index a826412..69ffde0 100644
--- a/micronode.go
+++ b/micronode.go
@@ -42,6 +42,8 @@
 		SubCh:    make(chan *bhome_msg.MsgPublish, 512),
 	}
 
+	go startHeartbeat(ctx, handle)
+
 	return mn, nil
 }
 
@@ -64,16 +66,16 @@
 	return errors.New("ms.handle is nil")
 }
 
-func (ms *MicroNode) startHeartbeat() {
+func startHeartbeat(ctx context.Context, h *BHBus) {
 	t := time.NewTicker(1 * time.Second)
 	defer t.Stop()
 
 	for {
 		select {
-		case <-ms.ctx.Done():
+		case <-ctx.Done():
 			return
 		case <-t.C:
-			ms.handle.HeartBeat()
+			h.HeartBeat()
 		default:
 			time.Sleep(500 * time.Millisecond)
 		}
@@ -81,13 +83,7 @@
 }
 
 func (ms *MicroNode) StartClient() {
-	ms.mtx.Lock()
-	defer ms.mtx.Unlock()
-	if !ms.started {
-		ms.started = true
 
-		go ms.startHeartbeat()
-	}
 }
 
 func (ms *MicroNode) StartServer(funcMap map[string]MicroFunc) {
@@ -97,8 +93,6 @@
 		ms.mtx.Unlock()
 
 		ms.handlers = funcMap
-
-		go ms.startHeartbeat()
 
 		for {
 			select {

--
Gitblit v1.8.0