From e40a4d43f4df8349308cd5096e0a87f5aa8680c5 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 14:35:29 +0800 Subject: [PATCH] fix --- .../internal/logic/datatransferlogic.go | 21 +++------------- .../internal/logic/ws_render_image_logic.go | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 18 deletions(-) create mode 100644 server/websocket/internal/logic/ws_render_image_logic.go diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 73227ee6..81cecdd9 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -5,7 +5,6 @@ import ( "fmt" "fusenapi/constants" "fusenapi/server/websocket/internal/types" - "fusenapi/utils/basic" "github.com/google/uuid" "github.com/gorilla/websocket" "net/http" @@ -56,7 +55,7 @@ type wsConnectItem struct { renderImage map[string]struct{} //需要渲染的图片 } -func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.ResponseWriter, r *http.Request) (resp *basic.Response) { +func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.ResponseWriter, r *http.Request) { //升级websocket conn, err := upgrade.Upgrade(w, r, nil) if err != nil { @@ -130,8 +129,6 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp go ws.sendLoop() //心跳 ws.heartbeat() - - return resp.SetStatus(basic.CodeOK) } // 心跳 @@ -225,23 +222,11 @@ func (w *wsConnectItem) dealwithReciveData(data []byte) { logx.Error("invalid format of websocket message") return } + //分消息类型给到不同逻辑处理,可扩展 switch parseInfo.T { //图片渲染 case constants.WEBSOCKET_RENDER_IMAGE: - var renderImageData []types.RenderImageReqMsg - if err := json.Unmarshal([]byte(parseInfo.D), &renderImageData); err != nil { - logx.Error("invalid format of websocket render image message", err) - return - } - logx.Info("收到请求云渲染图片数据:", renderImageData) - w.mutex.Lock() - defer w.mutex.Unlock() - //把需要渲染的图片加进去 - for _, v := range renderImageData { - key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId) - w.renderImage[key] = struct{}{} - } - //TODO 发送数据到云渲染服务器 + w.SendToCloudRender([]byte(parseInfo.D)) default: } diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go new file mode 100644 index 00000000..2428cd17 --- /dev/null +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -0,0 +1,24 @@ +package logic + +import ( + "encoding/json" + "fusenapi/server/websocket/internal/types" + "github.com/zeromicro/go-zero/core/logx" +) + +// 渲染请求数据处理发送云渲染服务处理 +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 + } + logx.Info("收到请求云渲染图片数据:", renderImageData) + w.mutex.Lock() + defer w.mutex.Unlock() + //把需要渲染的图片加进去 + for _, v := range renderImageData { + key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId) + w.renderImage[key] = struct{}{} + } +}