|
@@ -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,
|