From a4ea6380ed70468b1bbaca0328a65686960fcd52 Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期一, 20 四月 2020 16:23:30 +0800
Subject: [PATCH] add svLock

---
 client.go |   30 ++++++++++++++++++++++++------
 1 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/client.go b/client.go
index 2733153..d9a211b 100644
--- a/client.go
+++ b/client.go
@@ -1,6 +1,7 @@
 package gopherdiscovery
 
 import (
+	"encoding/json"
 	"errors"
 	"log"
 
@@ -24,7 +25,7 @@
 
 	// Service that needs to be discovered, for example for a web server could be
 	// http://192.168.1.1:8080
-	service string
+	service ServiceInfo
 
 	heartbeatmsg chan []byte
 
@@ -45,11 +46,16 @@
 	changes chan []byte
 }
 
-func Client(urlServer string, service string) (*DiscoveryClient, error) {
-	return ClientWithSub(urlServer, "", service)
+type ServiceInfo struct {
+	ServiceId string `json:"serviceId"`
+	Info []byte `json:"info"`
 }
 
-func ClientWithSub(urlServer string, urlPubSub string, service string) (*DiscoveryClient, error) {
+func Client(urlServer string, serviceId string) (*DiscoveryClient, error) {
+	return ClientWithSub(urlServer, "", serviceId)
+}
+
+func ClientWithSub(urlServer string, urlPubSub string, serviceId string) (*DiscoveryClient, error) {
 	var sock mangos.Socket
 	var err error
 	var subscriber *Subscriber
@@ -76,10 +82,13 @@
 		return nil, err
 	}
 
+	svInfo := ServiceInfo{
+		ServiceId: serviceId,
+	}
 	client := &DiscoveryClient{
 		urlServer:    urlServer,
 		urlPubSub:    urlPubSub,
-		service:      service,
+		service:      svInfo,
 		ctx:          ctx,
 		cancel:       cancel,
 		sock:         sock,
@@ -106,6 +115,10 @@
 	d.cancel()
 }
 
+func (d *DiscoveryClient) SetResp(i []byte) {
+	d.service.Info = i
+}
+
 func (d *DiscoveryClient) run() {
 	var err error
 	var msg []byte
@@ -120,7 +133,12 @@
 				log.Println("DiscoveryClient: Cannot receive the SURVEY", err.Error())
 				continue
 			}
-			err = d.sock.Send([]byte(d.service))
+			sendB, err := json.Marshal(d.service)
+			if err != nil {
+				log.Println("DiscoveryClient: marshal d.serviceInfo err", err.Error())
+				continue
+			}
+			err = d.sock.Send(sendB)
 			if err != nil {
 				log.Println("DiscoveryClient: Cannot send the SURVEY response", err.Error())
 				continue

--
Gitblit v1.8.0