Browse Source

add heartbeat

sainw 1 year ago
parent
commit
1e77d4b6ec
2 changed files with 8 additions and 2 deletions
  1. 7 1
      client/client.go
  2. 1 1
      example/main.go

+ 7 - 1
client/client.go

@@ -48,7 +48,12 @@ func NewClient(address string, id string, key string) (*FhoClient, error) {
 	if err != nil {
 		return nil, err
 	}
-	return &FhoClient{id: id, key: key, dataService: pb.NewDataServiceClient(conn)}, nil
+	client := &FhoClient{id: id, key: key, dataService: pb.NewDataServiceClient(conn)}
+	go func(c *FhoClient) {
+		Heartbeat(c, func(isConnected bool) {})
+	}(client)
+
+	return client, nil
 }
 
 func (f *FhoClient) query(object string, updatedAfter time.Time) ([]map[string]interface{}, error) {
@@ -148,6 +153,7 @@ func Heartbeat(c *FhoClient, fn func(isConnected bool)) error {
 	start:
 		_, err := s.stream.Recv()
 		if err != nil {
+			log.Println("stream error:", err)
 			if s.isConnected || !isInit {
 				fn(false)
 				s.isConnected = false

+ 1 - 1
example/main.go

@@ -78,7 +78,7 @@ func query(client *fho.FhoClient) {
 }
 
 func listenProducts(client *fho.FhoClient) error {
-	err := fho.ListenOnProducts(client, time.Now().Add(time.Hour*-114), func(products []*fho.Product) error {
+	err := fho.ListenOnProducts(client, time.Now().Add(time.Hour*-1114), func(products []*fho.Product) error {
 		for _, p := range products {
 			fmt.Println("Listening=====>")
 			printProduct(p)