From c65db5fe940fe1ef3efb6a174a20bae8cfa0e93b Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 4 Sep 2023 16:43:32 +0800 Subject: [PATCH 1/2] fix --- .../internal/logic/rendernotifylogic.go | 28 +++++++++++++++++++ server/websocket/internal/types/types.go | 3 +- server_api/websocket.api | 3 +- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index 6cfc70c2..6a68e932 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -73,6 +73,34 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *a return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "failed to upload render resource image") } uploadUnityRenderImageTakesTime := time.Now().UTC().UnixMilli() - unityRenderEndTime + //传了唯一标识,则精确查找 + if req.Wid != "" { + if value, ok := mapConnPool.Load(req.Wid); ok { + //断言连接 + ws, ok := value.(wsConnectItem) + if !ok { + logx.Error("渲染回调断言websocket连接失败") + return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "渲染回调断言websocket连接失败") + } + //记录收到unity渲染结果时间以及上传渲染图耗时时间 + ws.modifyRenderTaskProperty(renderImageControlChanItem{ + option: 2, + taskId: req.TaskId, + taskProperty: renderTask{ + unityRenderEndTime: unityRenderEndTime, + uploadUnityRenderImageTakesTime: uploadUnityRenderImageTakesTime, + }, + }) + //发送处理并删除任务 + ws.deleteRenderTask(renderImageControlChanItem{ + option: 0, //0删除 1添加 2修改耗时属性 + taskId: req.TaskId, + renderNotifyImageUrl: uploadRes.ResourceUrl, + }) + } + logx.Info("渲染回调成功,渲染结果图片为:", uploadRes.ResourceUrl) + return resp.SetStatusWithMessage(basic.CodeOK, "success") + } //遍历websocket链接把数据传进去 mapConnPool.Range(func(key, value any) bool { //断言连接 diff --git a/server/websocket/internal/types/types.go b/server/websocket/internal/types/types.go index f56eb527..a26c11a6 100644 --- a/server/websocket/internal/types/types.go +++ b/server/websocket/internal/types/types.go @@ -6,7 +6,8 @@ import ( ) type RenderNotifyReq struct { - TaskId string `json:"task_id"` //任务id + Wid string `json:"wid,optional"` //websocket连接唯一标识 + TaskId string `json:"task_id"` //任务id UserId int64 `json:"user_id"` GuestId int64 `json:"guest_id"` Image string `json:"image"` diff --git a/server_api/websocket.api b/server_api/websocket.api index 34e02ad4..f707417b 100644 --- a/server_api/websocket.api +++ b/server_api/websocket.api @@ -22,7 +22,8 @@ service websocket { //渲染完了通知接口 type RenderNotifyReq { - TaskId string `json:"task_id"` //任务id + Wid string `json:"wid,optional"` //websocket连接唯一标识 + TaskId string `json:"task_id"` //任务id UserId int64 `json:"user_id"` GuestId int64 `json:"guest_id"` Image string `json:"image"` From 9861475aa1d1af539c68f4ed3914a3ffcfb04c25 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 4 Sep 2023 16:54:55 +0800 Subject: [PATCH 2/2] fix --- .../internal/logic/rendernotifylogic.go | 47 ++++++------------- .../internal/logic/ws_render_image.go | 2 +- server/websocket/internal/types/types.go | 3 +- server_api/websocket.api | 3 +- 4 files changed, 17 insertions(+), 38 deletions(-) diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index 6a68e932..7b36a5a2 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -8,6 +8,7 @@ import ( "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/file" + "strings" "time" "github.com/zeromicro/go-zero/core/logx" @@ -38,6 +39,7 @@ func NewRenderNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Rend func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) { logx.Info("收到unity返回消息----") + req.TaskId = strings.Trim(req.TaskId, " ") if req.TaskId == "" { logx.Error("渲染回调参数错误:任务标识") return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param task_id") @@ -47,6 +49,15 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *a return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param image") } unityRenderEndTime := time.Now().UTC().UnixMilli() + //解析出真的taskId跟wid + splitSlice := strings.Split(req.TaskId, " ") + if len(splitSlice) != 2 { + logx.Error("渲染回调参数错误:task_id拆分得不到合理结果") + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param task_id!!") + } + //重新赋值(很重要) + req.TaskId = splitSlice[0] + wid := splitSlice[1] //存base64打印测试 /* f, _ := os.Create("b.txt") defer f.Close() @@ -73,41 +84,12 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *a return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "failed to upload render resource image") } uploadUnityRenderImageTakesTime := time.Now().UTC().UnixMilli() - unityRenderEndTime - //传了唯一标识,则精确查找 - if req.Wid != "" { - if value, ok := mapConnPool.Load(req.Wid); ok { - //断言连接 - ws, ok := value.(wsConnectItem) - if !ok { - logx.Error("渲染回调断言websocket连接失败") - return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "渲染回调断言websocket连接失败") - } - //记录收到unity渲染结果时间以及上传渲染图耗时时间 - ws.modifyRenderTaskProperty(renderImageControlChanItem{ - option: 2, - taskId: req.TaskId, - taskProperty: renderTask{ - unityRenderEndTime: unityRenderEndTime, - uploadUnityRenderImageTakesTime: uploadUnityRenderImageTakesTime, - }, - }) - //发送处理并删除任务 - ws.deleteRenderTask(renderImageControlChanItem{ - option: 0, //0删除 1添加 2修改耗时属性 - taskId: req.TaskId, - renderNotifyImageUrl: uploadRes.ResourceUrl, - }) - } - logx.Info("渲染回调成功,渲染结果图片为:", uploadRes.ResourceUrl) - return resp.SetStatusWithMessage(basic.CodeOK, "success") - } - //遍历websocket链接把数据传进去 - mapConnPool.Range(func(key, value any) bool { + if value, ok := mapConnPool.Load(wid); ok { //断言连接 ws, ok := value.(wsConnectItem) if !ok { logx.Error("渲染回调断言websocket连接失败") - return true + return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "渲染回调断言websocket连接失败") } //记录收到unity渲染结果时间以及上传渲染图耗时时间 ws.modifyRenderTaskProperty(renderImageControlChanItem{ @@ -124,8 +106,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *a taskId: req.TaskId, renderNotifyImageUrl: uploadRes.ResourceUrl, }) - return true - }) + } logx.Info("渲染回调成功,渲染结果图片为:", uploadRes.ResourceUrl) return resp.SetStatusWithMessage(basic.CodeOK, "success") } diff --git a/server/websocket/internal/logic/ws_render_image.go b/server/websocket/internal/logic/ws_render_image.go index 4151f0ef..ea79c50b 100644 --- a/server/websocket/internal/logic/ws_render_image.go +++ b/server/websocket/internal/logic/ws_render_image.go @@ -368,7 +368,7 @@ func (w *wsConnectItem) assembleRenderDataToUnity(taskId string, combineImage st //发送运行阶段消息(组装数据) w.sendAssembleRenderDataStepResponseMessage(info.RenderId) sendData := map[string]interface{}{ - "id": taskId, + "id": taskId + " " + w.uniqueId, //空格分开 "order_id": 0, "user_id": info.RenderData.UserId, "guest_id": info.RenderData.GuestId, diff --git a/server/websocket/internal/types/types.go b/server/websocket/internal/types/types.go index a26c11a6..08530673 100644 --- a/server/websocket/internal/types/types.go +++ b/server/websocket/internal/types/types.go @@ -6,8 +6,7 @@ import ( ) type RenderNotifyReq struct { - Wid string `json:"wid,optional"` //websocket连接唯一标识 - TaskId string `json:"task_id"` //任务id + TaskId string `json:"task_id"` //任务id + " " + wid的结合字符串 UserId int64 `json:"user_id"` GuestId int64 `json:"guest_id"` Image string `json:"image"` diff --git a/server_api/websocket.api b/server_api/websocket.api index f707417b..18287171 100644 --- a/server_api/websocket.api +++ b/server_api/websocket.api @@ -22,8 +22,7 @@ service websocket { //渲染完了通知接口 type RenderNotifyReq { - Wid string `json:"wid,optional"` //websocket连接唯一标识 - TaskId string `json:"task_id"` //任务id + TaskId string `json:"task_id"` //任务id + " " + wid的结合字符串 UserId int64 `json:"user_id"` GuestId int64 `json:"guest_id"` Image string `json:"image"`