Эх сурвалжийг харах

change data type from []byte to any

Joyit 1 сар өмнө
parent
commit
af139dac46

+ 2 - 0
config/config.go

@@ -7,6 +7,8 @@ const (
 	NEXT_SUBSCRIBE_MSG    = "NEXT SUBSCRIBE"
 	NEXT_MIDDLE           = 112
 	NEXT_MIDDLE_MSG       = "NEXT MIDDLE"
+	CONVERT_FAILED        = 113
+	CONVERT_FAILED_MSG    = "CONVERT FAILED"
 	FORBIDDEN             = 120
 	FORBIDDEN_MSG         = "FORBIDDEN"
 	SYSTEM_ERROR          = 123

+ 3 - 3
examples/client-tcp2.go

@@ -41,12 +41,12 @@ func main() {
 	})
 
 	// 订阅频道
-	hub.Subscribe(regexp.MustCompile("/tinymq/server"), "hello", func(request *tinymq.RequestData) (state uint8, result []byte) {
+	hub.Subscribe(regexp.MustCompile("/tinymq/server"), "hello", func(request *tinymq.RequestData) (state uint8, result any) {
 		log.Println("[client RECV]<-", string(request.Data))
-		return 1, []byte("tiny client")
+		return 1, "tiny client"
 	},
 	)
-	hub.Subscribe(regexp.MustCompile("/tinymq/server"), "nodata", func(request *tinymq.RequestData) (state uint8, result []byte) {
+	hub.Subscribe(regexp.MustCompile("/tinymq/server"), "nodata", func(request *tinymq.RequestData) (state uint8, result any) {
 		log.Println("[client RECV]<-", string(request.Data))
 		return 1, nil
 	},

+ 3 - 3
examples/client-ws2.go

@@ -44,12 +44,12 @@ func main() {
 	})
 
 	// 订阅频道
-	hub.Subscribe(regexp.MustCompile(remoteChannel), "hello", func(request *tinymq.RequestData) (state uint8, result []byte) {
+	hub.Subscribe(regexp.MustCompile(remoteChannel), "hello", func(request *tinymq.RequestData) (state uint8, result any) {
 		log.Println("[client RECV]<-", string(request.Data))
-		return 1, []byte("tiny client")
+		return 1, "tiny client"
 	},
 	)
-	hub.Subscribe(regexp.MustCompile(remoteChannel), "nodata", func(request *tinymq.RequestData) (state uint8, result []byte) {
+	hub.Subscribe(regexp.MustCompile(remoteChannel), "nodata", func(request *tinymq.RequestData) (state uint8, result any) {
 		log.Println("[client RECV]<-", string(request.Data))
 		return 1, nil
 	},

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 3 - 3
examples/server.go


+ 42 - 18
hub.go

@@ -239,7 +239,7 @@ func (h *Hub) sendRequest(gd *GetData) (count int) {
 						go h.outResponse(&ResponseData{
 							Id:    id,
 							State: config.GET_TIMEOUT,
-							Data:  []byte(fmt.Sprintf("[%s] %s %s", config.GET_TIMEOUT_MSG, gd.Channel.String(), gd.Cmd)),
+							Data:  fmt.Appendf(nil, "[%s] %s %s", config.GET_TIMEOUT_MSG, gd.Channel.String(), gd.Cmd),
 							conn:  conn,
 						})
 						// 检查是否已经很久时间没有使用连接了
@@ -318,15 +318,33 @@ func (h *Hub) requestFromNet(request *RequestData) {
 				if state == config.NEXT_SUBSCRIBE {
 					continue
 				}
+				var byteData []byte
+				switch data := data.(type) {
+				case []byte:
+					byteData = data
+				case string:
+					byteData = []byte(data)
+				default:
+					if data != nil {
+						// 自动转换数据为json格式
+						var err error
+						byteData, err = json.Marshal(data)
+						if err != nil {
+							log.Println(err.Error())
+							state = config.CONVERT_FAILED
+							byteData = fmt.Appendf(nil, "[%s] %s %s", config.CONVERT_FAILED_MSG, request.conn.channel, request.Cmd)
+						}
+					}
+				}
 				// 如果id为0表示不需要回应
 				if request.Id != 0 {
 					request.conn.sendResponse <- &ResponseData{
 						Id:    request.Id,
 						State: state,
-						Data:  data,
+						Data:  byteData,
 					}
 					if h.cf.PrintMsg {
-						log.Println("[RESP]->", request.Id, channel, "["+cmd+"]", state, subStr(string(data), 200))
+						log.Println("[RESP]->", request.Id, channel, "["+cmd+"]", state, subStr(string(byteData), 200))
 					}
 				}
 				return
@@ -462,31 +480,37 @@ func (h *Hub) GetOneWithTimeout(channel *regexp.Regexp, cmd string, data any, ti
 }
 
 // 推送消息出去,不需要返回数据
-func (h *Hub) Push(channel *regexp.Regexp, cmd string, data []byte) {
-	// 排除空频道
-	if channel == nil {
-		return
-	}
-	gd := &GetData{
-		Channel:  channel,
-		Cmd:      cmd,
-		Data:     data,
-		Timeout:  h.cf.ReadWait,
-		backchan: nil,
-	}
-	h.sendRequest(gd)
+func (h *Hub) Push(channel *regexp.Regexp, cmd string, data any) {
+	h.PushWithMax(channel, cmd, data, 0)
 }
 
 // 推送最大对应数量的消息出去,不需要返回数据
-func (h *Hub) PushWithMax(channel *regexp.Regexp, cmd string, data []byte, max int) {
+func (h *Hub) PushWithMax(channel *regexp.Regexp, cmd string, data any, max int) {
 	// 排除空频道
 	if channel == nil {
 		return
 	}
+	var reqData []byte
+	switch data := data.(type) {
+	case []byte:
+		reqData = data
+	case string:
+		reqData = []byte(data)
+	default:
+		if data != nil {
+			// 自动转换数据为json格式
+			var err error
+			reqData, err = json.Marshal(data)
+			if err != nil {
+				log.Println(err.Error())
+				return
+			}
+		}
+	}
 	gd := &GetData{
 		Channel:  channel,
 		Cmd:      cmd,
-		Data:     data,
+		Data:     reqData,
 		Max:      max,
 		Timeout:  h.cf.ReadWait,
 		backchan: nil,

+ 1 - 1
type.go

@@ -14,7 +14,7 @@ import (
 type MiddleFunc func(request *RequestData) (response *ResponseData)
 
 // 订阅频道响应函数
-type SubscribeBack func(request *RequestData) (state uint8, result []byte)
+type SubscribeBack func(request *RequestData) (state uint8, result any)
 
 // GET 获取数据的回调函数,如果返回 false 则提前结束
 type GetBack func(response *ResponseData) (ok bool)

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно