From 16feddfe3ea34f443be0b859485c875d23c7cd00 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 12:27:34 +0800 Subject: [PATCH] fix --- .../internal/logic/datatransferlogic.go | 35 ++++++++----------- .../internal/logic/rendernotifylogic.go | 26 +++++++++++++- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 17792d10..73227ee6 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -241,37 +241,30 @@ func (w *wsConnectItem) dealwithReciveData(data []byte) { key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId) w.renderImage[key] = struct{}{} } + //TODO 发送数据到云渲染服务器 default: } } -// 把渲染好的数据放入outchan -func (w *wsConnectItem) setOutRenderImage(req *types.RenderNotifyReq) { - w.mutex.Lock() - defer w.mutex.Unlock() - for _, notifyItem := range req.NotifyList { - renderKey := w.getRenderImageMapKey(notifyItem.ProductId, notifyItem.SizeId, notifyItem.TemplateId) - //查询 - _, ok := w.renderImage[renderKey] - if !ok { - continue - } - responseData := types.RenderImageRspMsg{ - ProductId: notifyItem.ProductId, - SizeId: notifyItem.SizeId, - TemplateId: notifyItem.TemplateId, - Source: "我是渲染资源", - } - b, _ := json.Marshal(responseData) +// 把要传递给客户端的数据放入outchan +func (w *wsConnectItem) sendToOutChan(data [][]byte) { + for _, v := range data { select { case <-w.closeChan: return - case w.outChan <- b: + case w.outChan <- v: logx.Info("notify send render result to out chan") } - //删掉已经处理的渲染任务 - delete(w.renderImage, renderKey) + } +} + +// 删除对应需要渲染的map值 +func (w *wsConnectItem) deleteRenderMapVal(keys []string) { + w.mutex.Lock() + defer w.mutex.Unlock() + for _, v := range keys { + delete(w.renderImage, v) } } diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index c8495ed2..22fff393 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -58,11 +58,35 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi } //遍历websocket链接把数据传进去 mapConnPool.Range(func(key, value any) bool { + //断言连接 ws, ok := value.(wsConnectItem) if !ok { return false } - ws.setOutRenderImage(req) + dataList := make([][]byte, 0, len(req.NotifyList)) + deleteRenderKey := make([]string, 0, len(req.NotifyList)) + //遍历数据 + for _, notifyItem := range req.NotifyList { + renderKey := ws.getRenderImageMapKey(notifyItem.ProductId, notifyItem.SizeId, notifyItem.TemplateId) + //查询 + _, ok = ws.renderImage[renderKey] + if !ok { + continue + } + responseData := types.RenderImageRspMsg{ + ProductId: notifyItem.ProductId, + SizeId: notifyItem.SizeId, + TemplateId: notifyItem.TemplateId, + Source: "我是渲染资源", + } + b, _ := json.Marshal(responseData) + dataList = append(dataList, b) + deleteRenderKey = append(deleteRenderKey, renderKey) + } + //删除对应的需要渲染的图片map + ws.deleteRenderMapVal(deleteRenderKey) + //发送数据 + ws.sendToOutChan(dataList) return true }) return resp.SetStatus(basic.CodeOK)