Joyit 3 周之前
父節點
當前提交
e8198294cb
共有 2 個文件被更改,包括 15 次插入11 次删除
  1. 2 2
      examples/client-ws2.go
  2. 13 9
      hub.go

+ 2 - 2
examples/client-ws2.go

@@ -64,11 +64,11 @@ func main() {
 	}
 
 	log.Println("start get data")
-	count := hub.Get(remoteFilter, "hello", "hello in get model", func(response *tinymq.ResponseData) (ok bool) {
+	count, err := hub.Get(remoteFilter, "hello", "hello in get model", func(response *tinymq.ResponseData) (ok bool) {
 		log.Println("get state and data: ", response.State, string(response.Data))
 		return true
 	})
-	log.Println("end get data with count: ", count)
+	log.Println("end get data with count and err:", count, err)
 
 	// 获取信息
 	rsp := hub.GetOne(remoteFilter, "hello", "hello from client,hello from client,hello from client")

+ 13 - 9
hub.go

@@ -250,11 +250,11 @@ func (h *Hub) outResponse(response *ResponseData) {
 
 // 发送数据到网络接口
 // 返回发送的数量
-func (h *Hub) sendRequest(gd *GetData) (count int) {
+func (h *Hub) sendRequest(gd *GetData) (count int, err error) {
 	outData, err := h.convertData(gd.Data)
 	if err != nil {
 		log.Println(err)
-		return 0
+		return 0, err
 	}
 	doit := func(_ int, line *Line) bool {
 		// 检查连接是否OK
@@ -331,13 +331,14 @@ func (h *Hub) sendRequest(gd *GetData) (count int) {
 		if i == 0 && h.connectHostFunc != nil && h.filterToChannelFunc != nil {
 			channel := h.filterToChannelFunc(gd.Filter)
 			if channel == "" {
+				err = errors.New(IdMsg(NO_MATCH_CONNECT))
 				log.Println("not channel found")
-				return 0
+				return 0, err
 			}
 			err := h.ConnectToServer(channel, false, nil)
 			if err != nil {
 				log.Println(err)
-				return 0
+				return 0, err
 			}
 		} else {
 			time.Sleep(time.Millisecond * 400) // 故意将时间缩小一点
@@ -430,9 +431,9 @@ func (h *Hub) requestFromNet(request *RequestData) {
 // 当前调用将会阻塞,直到命令都执行结束,最后返回执行的数量
 // 如果 backFunc 返回为 false 则提前结束
 // 最大数量和超时时间如果为0的话表示使用默认值
-func (h *Hub) GetWithStruct(gd *GetData, backFunc GetBackFunc) (count int) {
+func (h *Hub) GetWithStruct(gd *GetData, backFunc GetBackFunc) (count int, err error) {
 	if gd.Filter == nil {
-		return 0
+		return 0, errors.New("not filter found")
 	}
 	if gd.Timeout <= 0 {
 		gd.Timeout = h.cf.WriteWait
@@ -440,9 +441,12 @@ func (h *Hub) GetWithStruct(gd *GetData, backFunc GetBackFunc) (count int) {
 	if gd.backchan == nil {
 		gd.backchan = make(chan *ResponseData, 32)
 	}
-	sendMax := h.sendRequest(gd)
+	sendMax, err := h.sendRequest(gd)
+	if err != nil {
+		return 0, err
+	}
 	if sendMax <= 0 {
-		return 0
+		return 0, nil
 	}
 	// 避免出现异常时线程无法退出
 	timer := time.NewTimer(time.Millisecond * time.Duration(gd.Timeout+h.cf.WriteWait*2))
@@ -485,7 +489,7 @@ func (h *Hub) GetWithStruct(gd *GetData, backFunc GetBackFunc) (count int) {
 // 请求频道并获取数据,采用回调的方式返回结果
 // 当前调用将会阻塞,直到命令都执行结束,最后返回执行的数量
 // 如果 backFunc 返回为 false 则提前结束
-func (h *Hub) Get(filter FilterFunc, cmd string, data any, backFunc GetBackFunc) (count int) {
+func (h *Hub) Get(filter FilterFunc, cmd string, data any, backFunc GetBackFunc) (count int, err error) {
 	return h.GetWithStruct(&GetData{
 		Filter: filter,
 		Cmd:    cmd,