From 9f9f7d3fd6668cd2572b294529e08d898425dc84 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 17:33:50 +0800 Subject: [PATCH] fix --- .../internal/logic/datatransferlogic.go | 21 +++++++++++++----- .../internal/logic/rendernotifylogic.go | 22 ++++++------------- .../logic/thirdpartyloginnotifylogic.go | 18 +++++---------- .../internal/logic/ws_render_image_logic.go | 10 +++++++-- 4 files changed, 36 insertions(+), 35 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 999bd381..f72dca20 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -79,12 +79,14 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp } defer conn.Close() w.Header().Set("Connection", "Upgrade") - rsp := types.DataTransferData{} //鉴权不成功10秒后断开 /*isAuth, _ := l.checkAuth(svcCtx, r) if !isAuth { time.Sleep(time.Second) //兼容下火狐 - rsp.T = constants.WEBSOCKET_UNAUTH + rsp := types.DataTransferData{ + T: constants.WEBSOCKET_UNAUTH, + D: nil, + } b, _ := json.Marshal(rsp) //先发一条正常信息 _ = conn.WriteMessage(websocket.TextMessage, b) @@ -110,9 +112,7 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp defer ws.close() //把连接成功消息发回去 time.Sleep(time.Second) //兼容下火狐 - rsp.T = constants.WEBSOCKET_CONNECT_SUCCESS - rsp.D = uniqueId - b, _ := json.Marshal(rsp) + b := ws.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, uniqueId) _ = conn.WriteMessage(websocket.TextMessage, b) //循环读客户端信息 go ws.readLoop() @@ -247,11 +247,22 @@ func (w *wsConnectItem) getRenderImageMapKey(productId, templateTagId int64, alg return fmt.Sprintf("%d-%d-%s", productId, templateTagId, algorithmVersion) } +// 格式化返回数据 +func (w *wsConnectItem) respondDataFormat(msgType string, data interface{}) []byte { + d := types.DataTransferData{ + T: msgType, + D: data, + } + b, _ := json.Marshal(d) + return b +} + // 处理接受到的数据 func (w *wsConnectItem) dealwithReciveData(data []byte) { var parseInfo types.DataTransferData if err := json.Unmarshal(data, &parseInfo); err != nil { logx.Error("invalid format of websocket message") + w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket message") return } d, _ := json.Marshal(parseInfo.D) diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index 06ab1172..b0088855 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -1,10 +1,6 @@ package logic import ( - "crypto/sha256" - "encoding/hex" - "encoding/json" - "fmt" "fusenapi/constants" "fusenapi/utils/basic" "time" @@ -45,7 +41,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param time") } //验证签名 sha256 - notifyByte, _ := json.Marshal(req.Info) + /*notifyByte, _ := json.Marshal(req.Info) h := sha256.New() h.Write([]byte(fmt.Sprintf(constants.RENDER_NOTIFY_SIGN_KEY, string(notifyByte), req.Time))) signHex := h.Sum(nil) @@ -53,7 +49,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi //fmt.Println(sign) if req.Sign != sign { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid sign") - } + }*/ //遍历websocket链接把数据传进去 mapConnPool.Range(func(key, value any) bool { //断言连接 @@ -67,15 +63,11 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi if !ok { return true } - rspData := types.DataTransferData{ - T: constants.WEBSOCKET_RENDER_IMAGE, - D: types.RenderImageRspMsg{ - ProductId: req.Info.ProductId, - TemplateTagId: req.Info.TemplateTagId, - Image: req.Info.Image, - }, - } - b, _ := json.Marshal(rspData) + b := ws.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, types.RenderImageRspMsg{ + ProductId: req.Info.ProductId, + TemplateTagId: req.Info.TemplateTagId, + Image: req.Info.Image, + }) //删除对应的需要渲染的图片map ws.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ Option: 0, //0删除 1添加 diff --git a/server/websocket/internal/logic/thirdpartyloginnotifylogic.go b/server/websocket/internal/logic/thirdpartyloginnotifylogic.go index ce077259..d363ffd9 100644 --- a/server/websocket/internal/logic/thirdpartyloginnotifylogic.go +++ b/server/websocket/internal/logic/thirdpartyloginnotifylogic.go @@ -1,10 +1,6 @@ package logic import ( - "crypto/sha256" - "encoding/hex" - "encoding/json" - "fmt" "fusenapi/constants" "fusenapi/utils/auth" "fusenapi/utils/basic" @@ -52,7 +48,7 @@ func (l *ThirdPartyLoginNotifyLogic) ThirdPartyLoginNotify(req *types.ThirdParty return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:token is required") } //验证签名 sha256 - notifyByte, _ := json.Marshal(req.Info) + /*notifyByte, _ := json.Marshal(req.Info) h := sha256.New() h.Write([]byte(fmt.Sprintf(constants.THIRD_PARTY_LOGIN_NOTIFY_SIGN_KEY, string(notifyByte), req.Time))) signHex := h.Sum(nil) @@ -60,7 +56,7 @@ func (l *ThirdPartyLoginNotifyLogic) ThirdPartyLoginNotify(req *types.ThirdParty //fmt.Println(sign) if req.Sign != sign { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid sign") - } + }*/ //查询对应websocket连接 val, ok := mapConnPool.Load(req.Info.WebsocketId) if !ok { @@ -70,13 +66,9 @@ func (l *ThirdPartyLoginNotifyLogic) ThirdPartyLoginNotify(req *types.ThirdParty if !ok { return resp.SetStatusWithMessage(basic.CodeServiceErr, "type of websocket connect object is err") } - data := types.DataTransferData{ - T: constants.WEBSOCKET_THIRD_PARTY_LOGIN_NOTIFY, - D: types.ThirdPartyLoginRspMsg{ - Token: req.Info.Token, - }, - } - b, _ := json.Marshal(data) + b := ws.respondDataFormat(constants.WEBSOCKET_THIRD_PARTY_LOGIN_NOTIFY, types.ThirdPartyLoginRspMsg{ + Token: req.Info.Token, + }) select { case <-ws.closeChan: return resp.SetStatusWithMessage(basic.CodeOK, "websocket connect object is closed") diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index 634b6267..4803a1f4 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -2,6 +2,7 @@ package logic import ( "encoding/json" + "fusenapi/constants" "fusenapi/server/websocket/internal/types" "github.com/zeromicro/go-zero/core/logx" ) @@ -22,8 +23,13 @@ type renderImageControlChanItem struct { func (w *wsConnectItem) SendToCloudRender(data []byte) { var renderImageData types.RenderImageReqMsg if err := json.Unmarshal(data, &renderImageData); err != nil { - logx.Error("invalid format of websocket render image message", err) - return + select { + case <-w.closeChan: + return + case w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message"): + logx.Error("invalid format of websocket render image message", err) + return + } } logx.Info("收到请求云渲染图片数据:", renderImageData) //把需要渲染的图片任务加进去