From 999dc36e764e5927526418f29508378127dd8b56 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 11:53:06 +0800 Subject: [PATCH 01/56] fix --- .../internal/handler/datatransferhandler.go | 257 +---------------- .../internal/handler/rendernotifyhandler.go | 72 +---- .../internal/logic/datatransferlogic.go | 264 +++++++++++++++++- .../internal/logic/rendernotifylogic.go | 36 ++- server/websocket/internal/types/types.go | 7 +- server_api/websocket.api | 8 +- 6 files changed, 300 insertions(+), 344 deletions(-) diff --git a/server/websocket/internal/handler/datatransferhandler.go b/server/websocket/internal/handler/datatransferhandler.go index 3e5177d7..8ef09842 100644 --- a/server/websocket/internal/handler/datatransferhandler.go +++ b/server/websocket/internal/handler/datatransferhandler.go @@ -1,270 +1,15 @@ package handler import ( - "encoding/json" - "fmt" - "fusenapi/constants" "fusenapi/server/websocket/internal/logic" "fusenapi/server/websocket/internal/svc" - "fusenapi/server/websocket/internal/types" - "fusenapi/utils/auth" - "github.com/google/uuid" - "github.com/gorilla/websocket" - "github.com/zeromicro/go-zero/core/logx" "net/http" - "sync" - "time" ) -var ( - //升级 - upgrade = websocket.Upgrader{ - //允许跨域 - CheckOrigin: func(r *http.Request) bool { - return true - }, - } - //连接map池 - mapConnPool = sync.Map{} -) - -// 每个连接的连接属性 -type wsConnectItem struct { - conn *websocket.Conn //websocket的连接 - closeChan chan struct{} //关闭chan - isClose bool //是否已经关闭 - flag string - inChan chan []byte //接受消息缓冲通道 - outChan chan []byte //发送回客户端的消息 - mutex sync.Mutex - renderImage map[string]struct{} //需要渲染的图片 -} - func DataTransferHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // 创建一个业务逻辑层实例 - var req types.DataTransferReq l := logic.NewDataTransferLogic(r.Context(), svcCtx) - l.DataTransfer(&req, nil) - //升级websocket - conn, err := upgrade.Upgrade(w, r, nil) - if err != nil { - logx.Error("http upgrade websocket err:", err) - return - } - defer conn.Close() - rsp := types.DataTransferRsp{} - isAuth := true - // 解析JWT token,并对空用户进行判断 - claims, err := svcCtx.ParseJwtToken(r) - // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 - if err != nil { - rsp.T = constants.WEBSOCKET_UNAUTH - rsp.D = "unAuth" - b, _ := json.Marshal(rsp) - _ = conn.WriteMessage(websocket.TextMessage, b) - isAuth = false - } - if claims != nil { - // 从token中获取对应的用户信息 - _, err = auth.GetUserInfoFormMapClaims(claims) - // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 - if err != nil { - rsp.T = constants.WEBSOCKET_UNAUTH - rsp.D = "unAuth!!" - b, _ := json.Marshal(rsp) - _ = conn.WriteMessage(websocket.TextMessage, b) - isAuth = false - } - } else { - // 如果claims为nil,则认为用户身份为白板用户 - rsp.T = constants.WEBSOCKET_UNAUTH - rsp.D = "unAuth!!!" - b, _ := json.Marshal(rsp) - _ = conn.WriteMessage(websocket.TextMessage, b) - isAuth = false - } - //不是授权的连接(10秒后关闭) - if !isAuth { - select { - case <-time.After(time.Second * 10): - conn.Close() - return - } - } - //生成连接唯一标识 - flag := uuid.New().String() + time.Now().Format("20060102150405") - ws := wsConnectItem{ - conn: conn, - flag: flag, - closeChan: make(chan struct{}, 1), - inChan: make(chan []byte, 100), - outChan: make(chan []byte, 100), - renderImage: make(map[string]struct{}), - isClose: false, - } - //保存连接 - mapConnPool.Store(flag, ws) - defer ws.close() - //把连接成功消息发回去 - rsp.T = constants.WEBSOCKET_CONNECT_SUCCESS - rsp.D = flag - b, _ := json.Marshal(rsp) - conn.WriteMessage(websocket.TextMessage, b) - //循环读客户端信息 - go ws.readLoop() - //循环把数据发送给客户端 - go ws.writeLoop() - //推消息到云渲染 - go ws.sendLoop() - //心跳 - ws.heartbeat() - + l.DataTransfer(svcCtx, w, r) } } - -// 心跳 -func (w *wsConnectItem) heartbeat() { - rsp := types.DataTransferRsp{ - T: constants.WEBSOCKET_HEARTBEAT, - D: "", - } - b, _ := json.Marshal(rsp) - for { - time.Sleep(time.Second * 10) - select { - case <-w.closeChan: - return - default: - - } - //发送心跳信息 - if err := w.conn.WriteMessage(websocket.TextMessage, b); err != nil { - logx.Error("发送心跳信息异常,关闭连接:", w.flag, err) - w.close() - return - } - } -} - -// 关闭连接 -func (w *wsConnectItem) close() { - w.mutex.Lock() - defer w.mutex.Unlock() - logx.Info("websocket:", w.flag, " is closing...") - w.conn.Close() - mapConnPool.Delete(w.flag) - if !w.isClose { - w.isClose = true - close(w.closeChan) - close(w.outChan) - close(w.inChan) - } - logx.Info("websocket:", w.flag, " is closed") -} - -func (w *wsConnectItem) writeLoop() { - for { - select { - case <-w.closeChan: //如果关闭了 - return - case data := <-w.outChan: - w.conn.WriteMessage(websocket.TextMessage, data) - } - } -} - -// 接受客户端发来的消息 -func (w *wsConnectItem) readLoop() { - for { - select { - case <-w.closeChan: //如果关闭了 - return - default: - - } - _, data, err := w.conn.ReadMessage() - if err != nil { - logx.Error("接受信息错误:", err) - //关闭连接 - w.close() - return - } - //消息传入缓冲通道 - w.inChan <- data - } -} - -// 把收到的消息发往不同的地方处理 -func (w *wsConnectItem) sendLoop() { - for { - select { - case <-w.closeChan: - return - case data := <-w.inChan: - w.dealwithReciveData(data) - } - } -} - -// 处理接受到的数据 -func (w *wsConnectItem) dealwithReciveData(data []byte) { - var parseInfo types.DataTransferReq - if err := json.Unmarshal(data, &parseInfo); err != nil { - 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{}{} - } - 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) - select { - case <-w.closeChan: - return - case w.outChan <- b: - logx.Info("notify send render result to out chan") - } - //删掉已经处理的渲染任务 - delete(w.renderImage, renderKey) - } -} - -// 获取需要渲染图片的map key -func (w *wsConnectItem) getRenderImageMapKey(productId, sizeId, templateId int64) string { - return fmt.Sprintf("%d-%d-%d", productId, sizeId, templateId) -} diff --git a/server/websocket/internal/handler/rendernotifyhandler.go b/server/websocket/internal/handler/rendernotifyhandler.go index 005ee044..ab16cdad 100644 --- a/server/websocket/internal/handler/rendernotifyhandler.go +++ b/server/websocket/internal/handler/rendernotifyhandler.go @@ -1,18 +1,12 @@ package handler import ( - "crypto/sha256" - "encoding/hex" - "encoding/json" - "fmt" - "fusenapi/constants" - "fusenapi/utils/basic" - "github.com/zeromicro/go-zero/rest/httpx" - "net/http" - "time" - + "fusenapi/server/websocket/internal/logic" "fusenapi/server/websocket/internal/svc" "fusenapi/server/websocket/internal/types" + "fusenapi/utils/basic" + "net/http" + "reflect" ) func RenderNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { @@ -20,56 +14,18 @@ func RenderNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { var req types.RenderNotifyReq _, err := basic.RequestParse(w, r, svcCtx, &req) if err != nil { - httpx.OkJsonCtx(r.Context(), w, basic.Response{ - Code: basic.CodeRequestParamsErr.Code, - Message: "err param", - Data: nil, - }) return } - if len(req.NotifyList) == 0 { - httpx.OkJsonCtx(r.Context(), w, basic.Response{ - Code: basic.CodeRequestParamsErr.Code, - Message: "invalid param,notify list is empty", - Data: nil, - }) - return + // 创建一个业务逻辑层实例 + l := logic.NewRenderNotifyLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.RenderNotify(&req) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) } - if time.Now().Unix()-120 > req.Time /*|| req.Time > time.Now().Unix() */ { - httpx.OkJsonCtx(r.Context(), w, basic.Response{ - Code: basic.CodeRequestParamsErr.Code, - Message: "invalid param,time is expired", - Data: nil, - }) - return - } - //验证签名 sha256 - notifyByte, _ := json.Marshal(req.NotifyList) - h := sha256.New() - h.Write([]byte(fmt.Sprintf(constants.RENDER_NOTIFY_SIGN_KEY, string(notifyByte), req.Time))) - signHex := h.Sum(nil) - sign := hex.EncodeToString(signHex) - if req.Sign != sign { - httpx.OkJsonCtx(r.Context(), w, basic.Response{ - Code: basic.CodeRequestParamsErr.Code, - Message: "invalid sign", - Data: nil, - }) - return - } - //遍历链接 - mapConnPool.Range(func(key, value any) bool { - ws, ok := value.(wsConnectItem) - if !ok { - return false - } - ws.setOutRenderImage(req) - return true - }) - httpx.OkJsonCtx(r.Context(), w, basic.Response{ - Code: 200, - Message: "success", - Data: nil, - }) } } diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 7b5db86b..17792d10 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -1,14 +1,20 @@ package logic import ( - "fusenapi/utils/auth" + "encoding/json" + "fmt" + "fusenapi/constants" + "fusenapi/server/websocket/internal/types" "fusenapi/utils/basic" + "github.com/google/uuid" + "github.com/gorilla/websocket" + "net/http" + "sync" + "time" "context" "fusenapi/server/websocket/internal/svc" - "fusenapi/server/websocket/internal/types" - "github.com/zeromicro/go-zero/core/logx" ) @@ -26,18 +32,250 @@ func NewDataTransferLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Data } } -// 处理进入前逻辑w,r -// func (l *DataTransferLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { -// } +var ( + //升级 + upgrade = websocket.Upgrader{ + //允许跨域 + CheckOrigin: func(r *http.Request) bool { + return true + }, + } + //连接map池 + mapConnPool = sync.Map{} +) -// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 -// func (l *DataTransferLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { -// // httpx.OkJsonCtx(r.Context(), w, resp) -// } +// 每个连接的连接属性 +type wsConnectItem struct { + conn *websocket.Conn //websocket的连接 + closeChan chan struct{} //关闭chan + isClose bool //是否已经关闭 + flag string + inChan chan []byte //接受消息缓冲通道 + outChan chan []byte //发送回客户端的消息 + mutex sync.Mutex + renderImage map[string]struct{} //需要渲染的图片 +} -func (l *DataTransferLogic) DataTransfer(req *types.DataTransferReq, userinfo *auth.UserInfo) (resp *basic.Response) { - // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) - // userinfo 传入值时, 一定不为null +func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.ResponseWriter, r *http.Request) (resp *basic.Response) { + //升级websocket + conn, err := upgrade.Upgrade(w, r, nil) + if err != nil { + logx.Error("http upgrade websocket err:", err) + return + } + defer conn.Close() + rsp := types.DataTransferData{} + /*isAuth := true + // 解析JWT token,并对空用户进行判断 + claims, err := svcCtx.ParseJwtToken(r) + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + rsp.T = constants.WEBSOCKET_UNAUTH + rsp.D = "unAuth" + b, _ := json.Marshal(rsp) + _ = conn.WriteMessage(websocket.TextMessage, b) + isAuth = false + } + if claims != nil { + // 从token中获取对应的用户信息 + _, err = auth.GetUserInfoFormMapClaims(claims) + // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + rsp.T = constants.WEBSOCKET_UNAUTH + rsp.D = "unAuth!!" + b, _ := json.Marshal(rsp) + _ = conn.WriteMessage(websocket.TextMessage, b) + isAuth = false + } + } else { + // 如果claims为nil,则认为用户身份为白板用户 + rsp.T = constants.WEBSOCKET_UNAUTH + rsp.D = "unAuth!!!" + b, _ := json.Marshal(rsp) + _ = conn.WriteMessage(websocket.TextMessage, b) + isAuth = false + } + //不是授权的连接(10秒后关闭) + if !isAuth { + select { + case <-time.After(time.Second * 10): + conn.Close() + return + } + }*/ + //生成连接唯一标识 + flag := uuid.New().String() + time.Now().Format("20060102150405") + ws := wsConnectItem{ + conn: conn, + flag: flag, + closeChan: make(chan struct{}, 1), + inChan: make(chan []byte, 100), + outChan: make(chan []byte, 100), + renderImage: make(map[string]struct{}), + isClose: false, + } + //保存连接 + mapConnPool.Store(flag, ws) + defer ws.close() + //把连接成功消息发回去 + rsp.T = constants.WEBSOCKET_CONNECT_SUCCESS + rsp.D = flag + b, _ := json.Marshal(rsp) + conn.WriteMessage(websocket.TextMessage, b) + //循环读客户端信息 + go ws.readLoop() + //循环把数据发送给客户端 + go ws.writeLoop() + //推消息到云渲染 + go ws.sendLoop() + //心跳 + ws.heartbeat() return resp.SetStatus(basic.CodeOK) } + +// 心跳 +func (w *wsConnectItem) heartbeat() { + rsp := types.DataTransferData{ + T: constants.WEBSOCKET_HEARTBEAT, + D: "", + } + b, _ := json.Marshal(rsp) + for { + time.Sleep(time.Second * 10) + select { + case <-w.closeChan: + return + default: + + } + //发送心跳信息 + if err := w.conn.WriteMessage(websocket.TextMessage, b); err != nil { + logx.Error("发送心跳信息异常,关闭连接:", w.flag, err) + w.close() + return + } + } +} + +// 关闭连接 +func (w *wsConnectItem) close() { + w.mutex.Lock() + defer w.mutex.Unlock() + logx.Info("websocket:", w.flag, " is closing...") + w.conn.Close() + mapConnPool.Delete(w.flag) + if !w.isClose { + w.isClose = true + close(w.closeChan) + close(w.outChan) + close(w.inChan) + } + logx.Info("websocket:", w.flag, " is closed") +} + +func (w *wsConnectItem) writeLoop() { + for { + select { + case <-w.closeChan: //如果关闭了 + return + case data := <-w.outChan: + w.conn.WriteMessage(websocket.TextMessage, data) + } + } +} + +// 接受客户端发来的消息 +func (w *wsConnectItem) readLoop() { + for { + select { + case <-w.closeChan: //如果关闭了 + return + default: + + } + _, data, err := w.conn.ReadMessage() + if err != nil { + logx.Error("接受信息错误:", err) + //关闭连接 + w.close() + return + } + //消息传入缓冲通道 + w.inChan <- data + } +} + +// 把收到的消息发往不同的地方处理 +func (w *wsConnectItem) sendLoop() { + for { + select { + case <-w.closeChan: + return + case data := <-w.inChan: + w.dealwithReciveData(data) + } + } +} + +// 处理接受到的数据 +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") + 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{}{} + } + 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) + select { + case <-w.closeChan: + return + case w.outChan <- b: + logx.Info("notify send render result to out chan") + } + //删掉已经处理的渲染任务 + delete(w.renderImage, renderKey) + } +} + +// 获取需要渲染图片的map key +func (w *wsConnectItem) getRenderImageMapKey(productId, sizeId, templateId int64) string { + return fmt.Sprintf("%d-%d-%d", productId, sizeId, templateId) +} diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index 49803d29..f354b736 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -1,8 +1,13 @@ package logic import ( - "fusenapi/utils/auth" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "fmt" + "fusenapi/constants" "fusenapi/utils/basic" + "time" "context" @@ -35,9 +40,30 @@ func NewRenderNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Rend // // httpx.OkJsonCtx(r.Context(), w, resp) // } -func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) { - // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) - // userinfo 传入值时, 一定不为null - +func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basic.Response) { + if len(req.NotifyList) == 0 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "notify list is empty") + } + if time.Now().Unix()-120 > req.Time /*|| req.Time > time.Now().Unix() */ { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "time is expire") + } + //验证签名 sha256 + notifyByte, _ := json.Marshal(req.NotifyList) + h := sha256.New() + h.Write([]byte(fmt.Sprintf(constants.RENDER_NOTIFY_SIGN_KEY, string(notifyByte), req.Time))) + signHex := h.Sum(nil) + sign := hex.EncodeToString(signHex) + if req.Sign != sign { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid") + } + //遍历websocket链接 + mapConnPool.Range(func(key, value any) bool { + ws, ok := value.(wsConnectItem) + if !ok { + return false + } + ws.setOutRenderImage(req) + return true + }) return resp.SetStatus(basic.CodeOK) } diff --git a/server/websocket/internal/types/types.go b/server/websocket/internal/types/types.go index 6a1cf750..8178bcc6 100644 --- a/server/websocket/internal/types/types.go +++ b/server/websocket/internal/types/types.go @@ -5,12 +5,7 @@ import ( "fusenapi/utils/basic" ) -type DataTransferReq struct { - T string `json:"t"` //消息类型 - D string `json:"d"` //传递的消息 -} - -type DataTransferRsp struct { +type DataTransferData struct { T string `json:"t"` //消息类型 D string `json:"d"` //传递的消息 } diff --git a/server_api/websocket.api b/server_api/websocket.api index d0daf98b..dadbd2ab 100644 --- a/server_api/websocket.api +++ b/server_api/websocket.api @@ -11,18 +11,14 @@ import "basic.api" service websocket { //websocket数据交互 @handler DataTransferHandler - get /api/websocket/data_transfer(DataTransferReq) returns (response); + get /api/websocket/data_transfer(request) returns (response); //渲染完了通知接口 @handler RenderNotifyHandler post /api/websocket/render_notify(RenderNotifyReq) returns (response); } //websocket数据交互 -type DataTransferReq { - T string `json:"t"` //消息类型 - D string `json:"d"` //传递的消息 -} -type DataTransferRsp { +type DataTransferData { T string `json:"t"` //消息类型 D string `json:"d"` //传递的消息 } From b9afdf1dda4c01934915e73dcf96b42a48a1029c Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 11:53:57 +0800 Subject: [PATCH 02/56] fix --- server/websocket/internal/logic/rendernotifylogic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index f354b736..c8495ed2 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -56,7 +56,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi if req.Sign != sign { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid") } - //遍历websocket链接 + //遍历websocket链接把数据传进去 mapConnPool.Range(func(key, value any) bool { ws, ok := value.(wsConnectItem) if !ok { 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 03/56] 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) From 736f57c0c25e30a5531bd66be41caa530dd58120 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 14:17:30 +0800 Subject: [PATCH 04/56] fix --- model/gmodel/fs_user_gen.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/gmodel/fs_user_gen.go b/model/gmodel/fs_user_gen.go index 05b8cdd1..81d0f008 100644 --- a/model/gmodel/fs_user_gen.go +++ b/model/gmodel/fs_user_gen.go @@ -30,7 +30,7 @@ type FsUser struct { IsPhoneAdvertisement *int64 `gorm:"default:0;" json:"is_phone_advertisement"` // 是否接收短信广告 IsOpenRender *int64 `gorm:"default:0;" json:"is_open_render"` // 是否打开个性化渲染(1:开启,0:关闭) IsThousandFace *int64 `gorm:"default:0;" json:"is_thousand_face"` // 是否已经存在千人千面(1:存在,0:不存在) - IsLowRendering *int64 `gorm:"default:0;" json:"is_low_rendering"` // + IsLowRendering *int64 `gorm:"default:0;" json:"is_low_rendering"` // 是否开启低渲染模型渲染 IsRemoveBg *int64 `gorm:"default:1;" json:"is_remove_bg"` // 用户上传logo是否去除背景 } type FsUserModel struct { 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 05/56] 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{}{} + } +} From 918b5903d7368faf2a5c35b89d66edf199ed90c3 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 15:01:59 +0800 Subject: [PATCH 06/56] fix --- .../internal/logic/datatransferlogic.go | 67 ++++++++++++------- .../internal/logic/rendernotifylogic.go | 9 +-- .../internal/logic/ws_render_image_logic.go | 7 +- 3 files changed, 50 insertions(+), 33 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 81cecdd9..6a3b206f 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -45,14 +45,19 @@ var ( // 每个连接的连接属性 type wsConnectItem struct { - conn *websocket.Conn //websocket的连接 - closeChan chan struct{} //关闭chan - isClose bool //是否已经关闭 - flag string - inChan chan []byte //接受消息缓冲通道 - outChan chan []byte //发送回客户端的消息 - mutex sync.Mutex - renderImage map[string]struct{} //需要渲染的图片 + conn *websocket.Conn //websocket的连接 + closeChan chan struct{} //关闭chan + isClose bool //是否已经关闭 + flag string + inChan chan []byte //接受消息缓冲通道 + outChan chan []byte //发送回客户端的消息 + mutex sync.Mutex + renderImage map[string]struct{} //需要渲染的图片 + renderImageControlChan chan renderImageControlChanItem +} +type renderImageControlChanItem struct { + Option int // 0删除 1添加 + Key string //map的key } func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.ResponseWriter, r *http.Request) { @@ -105,13 +110,14 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp //生成连接唯一标识 flag := uuid.New().String() + time.Now().Format("20060102150405") ws := wsConnectItem{ - conn: conn, - flag: flag, - closeChan: make(chan struct{}, 1), - inChan: make(chan []byte, 100), - outChan: make(chan []byte, 100), - renderImage: make(map[string]struct{}), - isClose: false, + conn: conn, + flag: flag, + closeChan: make(chan struct{}, 1), + inChan: make(chan []byte, 100), + outChan: make(chan []byte, 100), + renderImage: make(map[string]struct{}), + renderImageControlChan: make(chan renderImageControlChanItem, 100), + isClose: false, } //保存连接 mapConnPool.Store(flag, ws) @@ -127,6 +133,8 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp go ws.writeLoop() //推消息到云渲染 go ws.sendLoop() + //操作连接中渲染任务的增加/删除 + go ws.operationRenderTask() //心跳 ws.heartbeat() } @@ -165,12 +173,28 @@ func (w *wsConnectItem) close() { if !w.isClose { w.isClose = true close(w.closeChan) - close(w.outChan) - close(w.inChan) } logx.Info("websocket:", w.flag, " is closed") } +// 操作连接中渲染任务的增加/删除 +func (w *wsConnectItem) operationRenderTask() { + for { + select { + case <-w.closeChan: + return + case data := <-w.renderImageControlChan: + switch data.Option { + case 0: //删除 + delete(w.renderImage, data.Key) + case 1: //新增 + w.renderImage[data.Key] = struct{}{} + default: + + } + } + } +} func (w *wsConnectItem) writeLoop() { for { select { @@ -244,15 +268,6 @@ func (w *wsConnectItem) sendToOutChan(data [][]byte) { } } -// 删除对应需要渲染的map值 -func (w *wsConnectItem) deleteRenderMapVal(keys []string) { - w.mutex.Lock() - defer w.mutex.Unlock() - for _, v := range keys { - delete(w.renderImage, v) - } -} - // 获取需要渲染图片的map key func (w *wsConnectItem) getRenderImageMapKey(productId, sizeId, templateId int64) string { return fmt.Sprintf("%d-%d-%d", productId, sizeId, templateId) diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index 22fff393..aa504999 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -64,7 +64,6 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi return false } 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) @@ -81,10 +80,12 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi } b, _ := json.Marshal(responseData) dataList = append(dataList, b) - deleteRenderKey = append(deleteRenderKey, renderKey) + //删除对应的需要渲染的图片map + ws.renderImageControlChan <- renderImageControlChanItem{ + Option: 0, + Key: renderKey, + } } - //删除对应的需要渲染的图片map - ws.deleteRenderMapVal(deleteRenderKey) //发送数据 ws.sendToOutChan(dataList) return true diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index 2428cd17..46c742ba 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -14,11 +14,12 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) { 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{}{} + w.renderImageControlChan <- renderImageControlChanItem{ + Option: 1, + Key: key, + } } } From ae58ebe35a382183c83882ae0a0edcfb08b9a476 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 15:05:53 +0800 Subject: [PATCH 07/56] fix --- .../websocket/internal/logic/datatransferlogic.go | 14 +++++++------- .../websocket/internal/logic/rendernotifylogic.go | 6 +++--- .../internal/logic/ws_render_image_logic.go | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 6a3b206f..6566d2d7 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -52,8 +52,8 @@ type wsConnectItem struct { inChan chan []byte //接受消息缓冲通道 outChan chan []byte //发送回客户端的消息 mutex sync.Mutex - renderImage map[string]struct{} //需要渲染的图片 - renderImageControlChan chan renderImageControlChanItem + renderImageTask map[string]struct{} //需要渲染的图片 + renderImageTaskCtlChan chan renderImageControlChanItem } type renderImageControlChanItem struct { Option int // 0删除 1添加 @@ -115,8 +115,8 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp closeChan: make(chan struct{}, 1), inChan: make(chan []byte, 100), outChan: make(chan []byte, 100), - renderImage: make(map[string]struct{}), - renderImageControlChan: make(chan renderImageControlChanItem, 100), + renderImageTask: make(map[string]struct{}), + renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100), isClose: false, } //保存连接 @@ -183,12 +183,12 @@ func (w *wsConnectItem) operationRenderTask() { select { case <-w.closeChan: return - case data := <-w.renderImageControlChan: + case data := <-w.renderImageTaskCtlChan: switch data.Option { case 0: //删除 - delete(w.renderImage, data.Key) + delete(w.renderImageTask, data.Key) case 1: //新增 - w.renderImage[data.Key] = struct{}{} + w.renderImageTask[data.Key] = struct{}{} default: } diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index aa504999..01f630de 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -67,8 +67,8 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi //遍历数据 for _, notifyItem := range req.NotifyList { renderKey := ws.getRenderImageMapKey(notifyItem.ProductId, notifyItem.SizeId, notifyItem.TemplateId) - //查询 - _, ok = ws.renderImage[renderKey] + //查询有无该渲染任务 + _, ok = ws.renderImageTask[renderKey] if !ok { continue } @@ -81,7 +81,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi b, _ := json.Marshal(responseData) dataList = append(dataList, b) //删除对应的需要渲染的图片map - ws.renderImageControlChan <- renderImageControlChanItem{ + ws.renderImageTaskCtlChan <- renderImageControlChanItem{ Option: 0, Key: renderKey, } diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index 46c742ba..40c07f1e 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -14,10 +14,10 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) { return } logx.Info("收到请求云渲染图片数据:", renderImageData) - //把需要渲染的图片加进去 + //把需要渲染的图片任务加进去 for _, v := range renderImageData { key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId) - w.renderImageControlChan <- renderImageControlChanItem{ + w.renderImageTaskCtlChan <- renderImageControlChanItem{ Option: 1, Key: key, } From 34d5e62824e6a9c34147386db96fb9ac4241eece Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 15:06:29 +0800 Subject: [PATCH 08/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 6566d2d7..fe187443 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -52,8 +52,8 @@ type wsConnectItem struct { inChan chan []byte //接受消息缓冲通道 outChan chan []byte //发送回客户端的消息 mutex sync.Mutex - renderImageTask map[string]struct{} //需要渲染的图片 - renderImageTaskCtlChan chan renderImageControlChanItem + renderImageTask map[string]struct{} //需要渲染的图片 + renderImageTaskCtlChan chan renderImageControlChanItem //渲染任务新增移除的控制通道 } type renderImageControlChanItem struct { Option int // 0删除 1添加 From e07d55efca9089cfa7fd6136602cbbc372a30180 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 15:08:43 +0800 Subject: [PATCH 09/56] fix --- .../internal/logic/datatransferlogic.go | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index fe187443..44810927 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -239,23 +239,6 @@ func (w *wsConnectItem) sendLoop() { } } -// 处理接受到的数据 -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") - return - } - //分消息类型给到不同逻辑处理,可扩展 - switch parseInfo.T { - //图片渲染 - case constants.WEBSOCKET_RENDER_IMAGE: - w.SendToCloudRender([]byte(parseInfo.D)) - default: - - } -} - // 把要传递给客户端的数据放入outchan func (w *wsConnectItem) sendToOutChan(data [][]byte) { for _, v := range data { @@ -272,3 +255,20 @@ func (w *wsConnectItem) sendToOutChan(data [][]byte) { func (w *wsConnectItem) getRenderImageMapKey(productId, sizeId, templateId int64) string { return fmt.Sprintf("%d-%d-%d", productId, sizeId, templateId) } + +// 处理接受到的数据 +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") + return + } + //分消息类型给到不同逻辑处理,可扩展 + switch parseInfo.T { + //图片渲染 + case constants.WEBSOCKET_RENDER_IMAGE: + w.SendToCloudRender([]byte(parseInfo.D)) + default: + + } +} From a896d42bc1f3762c30471d3dd2d448e3776ae2ac Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 15:12:24 +0800 Subject: [PATCH 10/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 44810927..ec9969dd 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -195,6 +195,8 @@ func (w *wsConnectItem) operationRenderTask() { } } } + +// 读取输出返回给客户端 func (w *wsConnectItem) writeLoop() { for { select { From 36aad6a0887b812998f6eb74d9092a6aae456c00 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 15:15:37 +0800 Subject: [PATCH 11/56] fix --- .../internal/logic/datatransferlogic.go | 19 ------------------- .../internal/logic/ws_render_image_logic.go | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index ec9969dd..3df09209 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -177,25 +177,6 @@ func (w *wsConnectItem) close() { logx.Info("websocket:", w.flag, " is closed") } -// 操作连接中渲染任务的增加/删除 -func (w *wsConnectItem) operationRenderTask() { - for { - select { - case <-w.closeChan: - return - case data := <-w.renderImageTaskCtlChan: - switch data.Option { - case 0: //删除 - delete(w.renderImageTask, data.Key) - case 1: //新增 - w.renderImageTask[data.Key] = struct{}{} - default: - - } - } - } -} - // 读取输出返回给客户端 func (w *wsConnectItem) writeLoop() { for { diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index 40c07f1e..6a075acb 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -23,3 +23,22 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) { } } } + +// 操作连接中渲染任务的增加/删除 +func (w *wsConnectItem) operationRenderTask() { + for { + select { + case <-w.closeChan: + return + case data := <-w.renderImageTaskCtlChan: + switch data.Option { + case 0: //删除任务 + delete(w.renderImageTask, data.Key) + case 1: //新增任务 + w.renderImageTask[data.Key] = struct{}{} + default: + + } + } + } +} From 88a59bbdea0ddb16eb2e02a5650d9a9bb5509494 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 16:00:19 +0800 Subject: [PATCH 12/56] fix --- model/gmodel/fs_pay_gen.go | 1 + model/gmodel/fs_resources_gen.go | 26 +++++++++++++++++++ model/gmodel/fs_resources_logic.go | 2 ++ model/gmodel/var_gen.go | 2 ++ .../internal/logic/datatransferlogic.go | 16 +++++++++++- 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 model/gmodel/fs_resources_gen.go create mode 100644 model/gmodel/fs_resources_logic.go diff --git a/model/gmodel/fs_pay_gen.go b/model/gmodel/fs_pay_gen.go index 0ee65802..e30685b6 100644 --- a/model/gmodel/fs_pay_gen.go +++ b/model/gmodel/fs_pay_gen.go @@ -7,6 +7,7 @@ import ( // fs_pay 支付记录 type FsPay struct { Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // + PayNo *string `gorm:"default:'';" json:"pay_no"` // 支付编号 UserId *int64 `gorm:"index;default:0;" json:"user_id"` // 用户id OrderNumber *string `gorm:"default:'';" json:"order_number"` // 订单编号 TradeNo *string `gorm:"index;default:'';" json:"trade_no"` // 第三方支付编号 diff --git a/model/gmodel/fs_resources_gen.go b/model/gmodel/fs_resources_gen.go new file mode 100644 index 00000000..432a8113 --- /dev/null +++ b/model/gmodel/fs_resources_gen.go @@ -0,0 +1,26 @@ +package gmodel + +import ( + "gorm.io/gorm" + "time" +) + +// fs_resources 资源表 +type FsResources struct { + ResourceId string `gorm:"primary_key;default:'';" json:"resource_id"` // 资源 ID + UserId *int64 `gorm:"index;default:0;" json:"user_id"` // 用户 ID + GuestId *int64 `gorm:"index;default:0;" json:"guest_id"` // 访客 ID + ResourceType *string `gorm:"index;default:'';" json:"resource_type"` // 资源类型 + ResourceUrl *string `gorm:"default:'';" json:"resource_url"` // 资源 URL + UploadedAt *time.Time `gorm:"index;default:'0000-00-00 00:00:00';" json:"uploaded_at"` // 上传时间 + Metadata *string `gorm:"default:'';" json:"metadata"` // 元数据,json格式,存储图像分率 + MetaKey1 *string `gorm:"index;default:'';" json:"meta_key1"` // 需要关键信息查询的自定义属性1,可以动态增加 +} +type FsResourcesModel struct { + db *gorm.DB + name string +} + +func NewFsResourcesModel(db *gorm.DB) *FsResourcesModel { + return &FsResourcesModel{db: db, name: "fs_resources"} +} diff --git a/model/gmodel/fs_resources_logic.go b/model/gmodel/fs_resources_logic.go new file mode 100644 index 00000000..e68225aa --- /dev/null +++ b/model/gmodel/fs_resources_logic.go @@ -0,0 +1,2 @@ +package gmodel +// TODO: 使用model的属性做你想做的 \ No newline at end of file diff --git a/model/gmodel/var_gen.go b/model/gmodel/var_gen.go index a5916431..2fac48a5 100644 --- a/model/gmodel/var_gen.go +++ b/model/gmodel/var_gen.go @@ -79,6 +79,7 @@ type AllModelsGen struct { FsQuotationRemarkTemplate *FsQuotationRemarkTemplateModel // fs_quotation_remark_template 报价单备注模板 FsQuotationSaler *FsQuotationSalerModel // fs_quotation_saler 报价单业务员表 FsRefundReason *FsRefundReasonModel // fs_refund_reason + FsResources *FsResourcesModel // fs_resources 资源表 FsStandardLogo *FsStandardLogoModel // fs_standard_logo 标准logo FsTags *FsTagsModel // fs_tags 产品分类表 FsToolLogs *FsToolLogsModel // fs_tool_logs 3d设计工具日志表 @@ -169,6 +170,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen { FsQuotationRemarkTemplate: NewFsQuotationRemarkTemplateModel(gdb), FsQuotationSaler: NewFsQuotationSalerModel(gdb), FsRefundReason: NewFsRefundReasonModel(gdb), + FsResources: NewFsResourcesModel(gdb), FsStandardLogo: NewFsStandardLogoModel(gdb), FsTags: NewFsTagsModel(gdb), FsToolLogs: NewFsToolLogsModel(gdb), diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 3df09209..29ad842a 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -1,12 +1,14 @@ package logic import ( + "bytes" "encoding/json" "fmt" "fusenapi/constants" "fusenapi/server/websocket/internal/types" "github.com/google/uuid" "github.com/gorilla/websocket" + "github.com/zeromicro/go-zero/rest/httpx" "net/http" "sync" "time" @@ -32,12 +34,24 @@ func NewDataTransferLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Data } var ( - //升级 + buffPool = sync.Pool{ + New: func() interface{} { + return bytes.Buffer{} + }, + } + //升级websocket upgrade = websocket.Upgrader{ + //握手超时时间15s + HandshakeTimeout: time.Second * 15, //允许跨域 CheckOrigin: func(r *http.Request) bool { return true }, + Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) { + httpx.ErrorCtx(r.Context(), w, reason) + return + }, + WriteBufferPool: &buffPool, } //连接map池 mapConnPool = sync.Map{} From e25cc97ea0c629d78c8e49e5a77098e156f4b667 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 16:23:38 +0800 Subject: [PATCH 13/56] fix --- .../internal/logic/datatransferlogic.go | 34 +++++++++---------- .../internal/logic/ws_render_image_logic.go | 19 ++++++++--- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 29ad842a..f2b31ab9 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -166,13 +166,12 @@ func (w *wsConnectItem) heartbeat() { case <-w.closeChan: return default: - - } - //发送心跳信息 - if err := w.conn.WriteMessage(websocket.TextMessage, b); err != nil { - logx.Error("发送心跳信息异常,关闭连接:", w.flag, err) - w.close() - return + //发送心跳信息 + if err := w.conn.WriteMessage(websocket.TextMessage, b); err != nil { + logx.Error("发送心跳信息异常,关闭连接:", w.flag, err) + w.close() + return + } } } } @@ -210,17 +209,16 @@ func (w *wsConnectItem) readLoop() { case <-w.closeChan: //如果关闭了 return default: - + _, data, err := w.conn.ReadMessage() + if err != nil { + logx.Error("接受信息错误:", err) + //关闭连接 + w.close() + return + } + //消息传入缓冲通道 + w.inChan <- data } - _, data, err := w.conn.ReadMessage() - if err != nil { - logx.Error("接受信息错误:", err) - //关闭连接 - w.close() - return - } - //消息传入缓冲通道 - w.inChan <- data } } @@ -264,7 +262,7 @@ func (w *wsConnectItem) dealwithReciveData(data []byte) { switch parseInfo.T { //图片渲染 case constants.WEBSOCKET_RENDER_IMAGE: - w.SendToCloudRender([]byte(parseInfo.D)) + go 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 index 6a075acb..cfa118a8 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fusenapi/server/websocket/internal/types" "github.com/zeromicro/go-zero/core/logx" + "time" ) // 渲染请求数据处理发送云渲染服务处理 @@ -14,12 +15,22 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) { return } logx.Info("收到请求云渲染图片数据:", renderImageData) + timer := time.NewTimer(time.Second * 10) //把需要渲染的图片任务加进去 for _, v := range renderImageData { - key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId) - w.renderImageTaskCtlChan <- renderImageControlChanItem{ - Option: 1, - Key: key, + select { + case <-w.closeChan: //连接关闭了 + return + case <-timer.C: //10秒超时了 + return + default: + //加入渲染任务 + key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId) + w.renderImageTaskCtlChan <- renderImageControlChanItem{ + Option: 1, + Key: key, + } + // TODO 数据发送给云渲染服务器 } } } From 7dc9760d94f3df93684e43fa37d437a815eb58ab Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 16:27:24 +0800 Subject: [PATCH 14/56] fix --- server/websocket/internal/logic/ws_render_image_logic.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index cfa118a8..8521ee91 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fusenapi/server/websocket/internal/types" "github.com/zeromicro/go-zero/core/logx" - "time" ) // 渲染请求数据处理发送云渲染服务处理 @@ -15,14 +14,11 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) { return } logx.Info("收到请求云渲染图片数据:", renderImageData) - timer := time.NewTimer(time.Second * 10) //把需要渲染的图片任务加进去 for _, v := range renderImageData { select { case <-w.closeChan: //连接关闭了 return - case <-timer.C: //10秒超时了 - return default: //加入渲染任务 key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId) From 3deda93e7e715ab9b7c5757fdb20b1aea09cf58d Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 16:47:53 +0800 Subject: [PATCH 15/56] fix --- constants/websocket.go | 10 ++++++---- .../websocket/internal/logic/datatransferlogic.go | 3 ++- .../websocket/internal/logic/rendernotifylogic.go | 15 +++++++++------ .../internal/logic/ws_render_image_logic.go | 2 ++ server/websocket/internal/types/types.go | 4 ++-- server_api/websocket.api | 4 ++-- 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/constants/websocket.go b/constants/websocket.go index a9a6aa5d..71f1fc36 100644 --- a/constants/websocket.go +++ b/constants/websocket.go @@ -5,13 +5,15 @@ type websocket string // websocket消息类型 const ( //鉴权失败 - WEBSOCKET_UNAUTH = "unAuth" + WEBSOCKET_UNAUTH = "WEBSOCKET_UNAUTH" //ws连接成功 - WEBSOCKET_CONNECT_SUCCESS = "connect-success" + WEBSOCKET_CONNECT_SUCCESS = "WEBSOCKET_CONNECT_SUCCESS" //心跳信息 - WEBSOCKET_HEARTBEAT = "heartbeat" + WEBSOCKET_HEARTBEAT = "WEBSOCKET_HEARTBEAT" //图片渲染 - WEBSOCKET_RENDER_IMAGE = "render-image" + WEBSOCKET_RENDER_IMAGE = "WEBSOCKET_RENDER_IMAGE" + //数据格式错误 + WEBSOCKET_ERR_DATA_FORMAT = "WEBSOCKET_ERR_DATA_FORMAT" ) // 云渲染通知需要的签名字符串 diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index f2b31ab9..ec39e0d9 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -258,11 +258,12 @@ func (w *wsConnectItem) dealwithReciveData(data []byte) { logx.Error("invalid format of websocket message") return } + d, _ := json.Marshal(parseInfo.D) //分消息类型给到不同逻辑处理,可扩展 switch parseInfo.T { //图片渲染 case constants.WEBSOCKET_RENDER_IMAGE: - go w.SendToCloudRender([]byte(parseInfo.D)) + go w.SendToCloudRender(d) default: } diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index 01f630de..064b459d 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -72,13 +72,16 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi if !ok { continue } - responseData := types.RenderImageRspMsg{ - ProductId: notifyItem.ProductId, - SizeId: notifyItem.SizeId, - TemplateId: notifyItem.TemplateId, - Source: "我是渲染资源", + rspData := types.DataTransferData{ + T: constants.WEBSOCKET_RENDER_IMAGE, + D: types.RenderImageRspMsg{ + ProductId: notifyItem.ProductId, + SizeId: notifyItem.SizeId, + TemplateId: notifyItem.TemplateId, + Source: "我是渲染资源", + }, } - b, _ := json.Marshal(responseData) + b, _ := json.Marshal(rspData) dataList = append(dataList, b) //删除对应的需要渲染的图片map ws.renderImageTaskCtlChan <- renderImageControlChanItem{ diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index 8521ee91..c8b7f816 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -2,12 +2,14 @@ package logic import ( "encoding/json" + "fmt" "fusenapi/server/websocket/internal/types" "github.com/zeromicro/go-zero/core/logx" ) // 渲染请求数据处理发送云渲染服务处理 func (w *wsConnectItem) SendToCloudRender(data []byte) { + fmt.Println(string(data)) var renderImageData []types.RenderImageReqMsg if err := json.Unmarshal(data, &renderImageData); err != nil { logx.Error("invalid format of websocket render image message", err) diff --git a/server/websocket/internal/types/types.go b/server/websocket/internal/types/types.go index 8178bcc6..d3336021 100644 --- a/server/websocket/internal/types/types.go +++ b/server/websocket/internal/types/types.go @@ -6,8 +6,8 @@ import ( ) type DataTransferData struct { - T string `json:"t"` //消息类型 - D string `json:"d"` //传递的消息 + T string `json:"t"` //消息类型 + D interface{} `json:"d"` //传递的消息 } type RenderImageReqMsg struct { diff --git a/server_api/websocket.api b/server_api/websocket.api index dadbd2ab..0d445c71 100644 --- a/server_api/websocket.api +++ b/server_api/websocket.api @@ -19,8 +19,8 @@ service websocket { //websocket数据交互 type DataTransferData { - T string `json:"t"` //消息类型 - D string `json:"d"` //传递的消息 + T string `json:"t"` //消息类型 + D interface{} `json:"d"` //传递的消息 } type RenderImageReqMsg { //websocket接受需要云渲染的图片 ProductId int64 `json:"product_id"` From 5a92db70ebc86e9cff0bbd6852d8a7444d2ce44d Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 16:50:06 +0800 Subject: [PATCH 16/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 1 - 1 file changed, 1 deletion(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index ec39e0d9..66b2e626 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -131,7 +131,6 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp outChan: make(chan []byte, 100), renderImageTask: make(map[string]struct{}), renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100), - isClose: false, } //保存连接 mapConnPool.Store(flag, ws) From 436ed9ae4f76d0a7c12730bbaf753a0ca1414f36 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 16:58:14 +0800 Subject: [PATCH 17/56] fix --- .../websocket/internal/logic/datatransferlogic.go | 14 ++++++-------- .../websocket/internal/logic/rendernotifylogic.go | 10 ++++------ .../internal/logic/ws_render_image_logic.go | 4 +--- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 66b2e626..c7f05b3a 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -234,14 +234,12 @@ func (w *wsConnectItem) sendLoop() { } // 把要传递给客户端的数据放入outchan -func (w *wsConnectItem) sendToOutChan(data [][]byte) { - for _, v := range data { - select { - case <-w.closeChan: - return - case w.outChan <- v: - logx.Info("notify send render result to out chan") - } +func (w *wsConnectItem) sendToOutChan(data []byte) { + select { + case <-w.closeChan: + return + case w.outChan <- data: + logx.Info("notify send render result to out chan") } } diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index 064b459d..97570811 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -63,7 +63,6 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi if !ok { return false } - dataList := make([][]byte, 0, len(req.NotifyList)) //遍历数据 for _, notifyItem := range req.NotifyList { renderKey := ws.getRenderImageMapKey(notifyItem.ProductId, notifyItem.SizeId, notifyItem.TemplateId) @@ -78,19 +77,18 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi ProductId: notifyItem.ProductId, SizeId: notifyItem.SizeId, TemplateId: notifyItem.TemplateId, - Source: "我是渲染资源", + Source: notifyItem.Source, }, } b, _ := json.Marshal(rspData) - dataList = append(dataList, b) //删除对应的需要渲染的图片map ws.renderImageTaskCtlChan <- renderImageControlChanItem{ - Option: 0, + Option: 0, //0删除 1添加 Key: renderKey, } + //发送数据到out chan + ws.sendToOutChan(b) } - //发送数据 - ws.sendToOutChan(dataList) return true }) return resp.SetStatus(basic.CodeOK) diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index c8b7f816..3fca11ca 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -2,14 +2,12 @@ package logic import ( "encoding/json" - "fmt" "fusenapi/server/websocket/internal/types" "github.com/zeromicro/go-zero/core/logx" ) // 渲染请求数据处理发送云渲染服务处理 func (w *wsConnectItem) SendToCloudRender(data []byte) { - fmt.Println(string(data)) var renderImageData []types.RenderImageReqMsg if err := json.Unmarshal(data, &renderImageData); err != nil { logx.Error("invalid format of websocket render image message", err) @@ -25,7 +23,7 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) { //加入渲染任务 key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId) w.renderImageTaskCtlChan <- renderImageControlChanItem{ - Option: 1, + Option: 1, //0删除 1添加 Key: key, } // TODO 数据发送给云渲染服务器 From d5ff15849d8467c0f4915488f8126713030e4b3f Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 17:06:53 +0800 Subject: [PATCH 18/56] fix --- .../internal/logic/datatransferlogic.go | 39 +++++++++---------- .../internal/logic/rendernotifylogic.go | 4 +- .../internal/logic/ws_render_image_logic.go | 20 ++++++++-- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index c7f05b3a..78ff3004 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -57,21 +57,16 @@ var ( mapConnPool = sync.Map{} ) -// 每个连接的连接属性 +// 每个连接的连接基本属性 type wsConnectItem struct { - conn *websocket.Conn //websocket的连接 - closeChan chan struct{} //关闭chan - isClose bool //是否已经关闭 - flag string - inChan chan []byte //接受消息缓冲通道 - outChan chan []byte //发送回客户端的消息 - mutex sync.Mutex - renderImageTask map[string]struct{} //需要渲染的图片 - renderImageTaskCtlChan chan renderImageControlChanItem //渲染任务新增移除的控制通道 -} -type renderImageControlChanItem struct { - Option int // 0删除 1添加 - Key string //map的key + conn *websocket.Conn //websocket的连接 + closeChan chan struct{} //关闭chan + isClose bool //是否已经关闭 + flag string //ws连接唯一标识 + inChan chan []byte //接受消息缓冲通道 + outChan chan []byte //发送回客户端的消息 + mutex sync.Mutex //互斥锁 + renderProperty renderProperty //扩展云渲染属性 } func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.ResponseWriter, r *http.Request) { @@ -124,13 +119,15 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp //生成连接唯一标识 flag := uuid.New().String() + time.Now().Format("20060102150405") ws := wsConnectItem{ - conn: conn, - flag: flag, - closeChan: make(chan struct{}, 1), - inChan: make(chan []byte, 100), - outChan: make(chan []byte, 100), - renderImageTask: make(map[string]struct{}), - renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100), + conn: conn, + flag: flag, + closeChan: make(chan struct{}, 1), + inChan: make(chan []byte, 100), + outChan: make(chan []byte, 100), + renderProperty: renderProperty{ + renderImageTask: make(map[string]struct{}), + renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100), + }, } //保存连接 mapConnPool.Store(flag, ws) diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index 97570811..c203443e 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -67,7 +67,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi for _, notifyItem := range req.NotifyList { renderKey := ws.getRenderImageMapKey(notifyItem.ProductId, notifyItem.SizeId, notifyItem.TemplateId) //查询有无该渲染任务 - _, ok = ws.renderImageTask[renderKey] + _, ok = ws.renderProperty.renderImageTask[renderKey] if !ok { continue } @@ -82,7 +82,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi } b, _ := json.Marshal(rspData) //删除对应的需要渲染的图片map - ws.renderImageTaskCtlChan <- renderImageControlChanItem{ + ws.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ Option: 0, //0删除 1添加 Key: renderKey, } diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index 3fca11ca..b93e4b67 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -6,6 +6,18 @@ import ( "github.com/zeromicro/go-zero/core/logx" ) +// 云渲染属性 +type renderProperty struct { + renderImageTask map[string]struct{} //需要渲染的图片任务 + renderImageTaskCtlChan chan renderImageControlChanItem //渲染任务新增移除的控制通道 +} + +// 渲染任务新增移除的控制通道的数据 +type renderImageControlChanItem struct { + Option int // 0删除 1添加 + Key string //map的key +} + // 渲染请求数据处理发送云渲染服务处理 func (w *wsConnectItem) SendToCloudRender(data []byte) { var renderImageData []types.RenderImageReqMsg @@ -22,7 +34,7 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) { default: //加入渲染任务 key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId) - w.renderImageTaskCtlChan <- renderImageControlChanItem{ + w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ Option: 1, //0删除 1添加 Key: key, } @@ -37,12 +49,12 @@ func (w *wsConnectItem) operationRenderTask() { select { case <-w.closeChan: return - case data := <-w.renderImageTaskCtlChan: + case data := <-w.renderProperty.renderImageTaskCtlChan: switch data.Option { case 0: //删除任务 - delete(w.renderImageTask, data.Key) + delete(w.renderProperty.renderImageTask, data.Key) case 1: //新增任务 - w.renderImageTask[data.Key] = struct{}{} + w.renderProperty.renderImageTask[data.Key] = struct{}{} default: } From 3c5b0d7e421acb09825052fac689b886411b0e69 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 17:09:04 +0800 Subject: [PATCH 19/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 78ff3004..1d5fc074 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -34,6 +34,7 @@ func NewDataTransferLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Data } var ( + //临时缓存对象池 buffPool = sync.Pool{ New: func() interface{} { return bytes.Buffer{} From be045b26151057a523e0a206fd26630c13b9279e Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 17:13:14 +0800 Subject: [PATCH 20/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 1d5fc074..ae4663cb 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -61,7 +61,7 @@ var ( // 每个连接的连接基本属性 type wsConnectItem struct { conn *websocket.Conn //websocket的连接 - closeChan chan struct{} //关闭chan + closeChan chan struct{} //ws连接关闭chan isClose bool //是否已经关闭 flag string //ws连接唯一标识 inChan chan []byte //接受消息缓冲通道 From 0142e04ef55fa2d8754c68efd2a25b5df17d1470 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 17:30:35 +0800 Subject: [PATCH 21/56] fix --- .../internal/logic/datatransferlogic.go | 57 +++++++++---------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index ae4663cb..0d8571f6 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -6,6 +6,7 @@ import ( "fmt" "fusenapi/constants" "fusenapi/server/websocket/internal/types" + "fusenapi/utils/auth" "github.com/google/uuid" "github.com/gorilla/websocket" "github.com/zeromicro/go-zero/rest/httpx" @@ -79,44 +80,17 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp } defer conn.Close() rsp := types.DataTransferData{} - /*isAuth := true - // 解析JWT token,并对空用户进行判断 - claims, err := svcCtx.ParseJwtToken(r) - // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 - if err != nil { + //鉴权不成功10秒后断开 + if !l.checkAuth(svcCtx, r) { rsp.T = constants.WEBSOCKET_UNAUTH rsp.D = "unAuth" b, _ := json.Marshal(rsp) _ = conn.WriteMessage(websocket.TextMessage, b) - isAuth = false - } - if claims != nil { - // 从token中获取对应的用户信息 - _, err = auth.GetUserInfoFormMapClaims(claims) - // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 - if err != nil { - rsp.T = constants.WEBSOCKET_UNAUTH - rsp.D = "unAuth!!" - b, _ := json.Marshal(rsp) - _ = conn.WriteMessage(websocket.TextMessage, b) - isAuth = false - } - } else { - // 如果claims为nil,则认为用户身份为白板用户 - rsp.T = constants.WEBSOCKET_UNAUTH - rsp.D = "unAuth!!!" - b, _ := json.Marshal(rsp) - _ = conn.WriteMessage(websocket.TextMessage, b) - isAuth = false - } - //不是授权的连接(10秒后关闭) - if !isAuth { select { - case <-time.After(time.Second * 10): - conn.Close() + case <-time.After(time.Second * 10): //10秒后断开 return } - }*/ + } //生成连接唯一标识 flag := uuid.New().String() + time.Now().Format("20060102150405") ws := wsConnectItem{ @@ -150,6 +124,27 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp ws.heartbeat() } +// 鉴权 +func (l *DataTransferLogic) checkAuth(svcCtx *svc.ServiceContext, r *http.Request) bool { + // 解析JWT token,并对空用户进行判断 + claims, err := svcCtx.ParseJwtToken(r) + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + return false + } + if claims != nil { + // 从token中获取对应的用户信息 + _, err = auth.GetUserInfoFormMapClaims(claims) + // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + return false + } + } else { + return false + } + return true +} + // 心跳 func (w *wsConnectItem) heartbeat() { rsp := types.DataTransferData{ From 7280c38b16149f6bc6612200b71c29f87ba4a921 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 17:39:56 +0800 Subject: [PATCH 22/56] fix --- .../internal/logic/datatransferlogic.go | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 0d8571f6..e0ac8a3f 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -81,16 +81,16 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp defer conn.Close() rsp := types.DataTransferData{} //鉴权不成功10秒后断开 - if !l.checkAuth(svcCtx, r) { + /*if !l.checkAuth(svcCtx, r) { rsp.T = constants.WEBSOCKET_UNAUTH rsp.D = "unAuth" b, _ := json.Marshal(rsp) + //先发一条正常信息 _ = conn.WriteMessage(websocket.TextMessage, b) - select { - case <-time.After(time.Second * 10): //10秒后断开 - return - } - } + //发送关闭信息 + _ = conn.WriteMessage(websocket.CloseMessage, nil) + return + }*/ //生成连接唯一标识 flag := uuid.New().String() + time.Now().Format("20060102150405") ws := wsConnectItem{ @@ -147,11 +147,6 @@ func (l *DataTransferLogic) checkAuth(svcCtx *svc.ServiceContext, r *http.Reques // 心跳 func (w *wsConnectItem) heartbeat() { - rsp := types.DataTransferData{ - T: constants.WEBSOCKET_HEARTBEAT, - D: "", - } - b, _ := json.Marshal(rsp) for { time.Sleep(time.Second * 10) select { @@ -159,7 +154,7 @@ func (w *wsConnectItem) heartbeat() { return default: //发送心跳信息 - if err := w.conn.WriteMessage(websocket.TextMessage, b); err != nil { + if err := w.conn.WriteMessage(websocket.PongMessage, nil); err != nil { logx.Error("发送心跳信息异常,关闭连接:", w.flag, err) w.close() return @@ -173,6 +168,8 @@ func (w *wsConnectItem) close() { w.mutex.Lock() defer w.mutex.Unlock() logx.Info("websocket:", w.flag, " is closing...") + //发送关闭信息 + _ = w.conn.WriteMessage(websocket.CloseMessage, nil) w.conn.Close() mapConnPool.Delete(w.flag) if !w.isClose { From 06e4f1f96e51077be65d2d2e2e22ca14f409af6d Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 17:47:02 +0800 Subject: [PATCH 23/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index e0ac8a3f..a606852a 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -9,7 +9,6 @@ import ( "fusenapi/utils/auth" "github.com/google/uuid" "github.com/gorilla/websocket" - "github.com/zeromicro/go-zero/rest/httpx" "net/http" "sync" "time" @@ -49,10 +48,6 @@ var ( CheckOrigin: func(r *http.Request) bool { return true }, - Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) { - httpx.ErrorCtx(r.Context(), w, reason) - return - }, WriteBufferPool: &buffPool, } //连接map池 From fcd20e7fc235b9192243e9d16cc0ce96e80f90c9 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 17:48:51 +0800 Subject: [PATCH 24/56] fix --- server/websocket/internal/logic/rendernotifylogic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index c203443e..5b3e3d2d 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -54,7 +54,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi signHex := h.Sum(nil) sign := hex.EncodeToString(signHex) if req.Sign != sign { - return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid") + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid sign") } //遍历websocket链接把数据传进去 mapConnPool.Range(func(key, value any) bool { From 9a8bdcb5baec8707748bf42041cb07fcea694329 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 17:55:14 +0800 Subject: [PATCH 25/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index a606852a..88097476 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -193,15 +193,18 @@ func (w *wsConnectItem) readLoop() { case <-w.closeChan: //如果关闭了 return default: - _, data, err := w.conn.ReadMessage() + msgType, data, err := w.conn.ReadMessage() if err != nil { logx.Error("接受信息错误:", err) //关闭连接 w.close() return } - //消息传入缓冲通道 - w.inChan <- data + //ping的消息不处理 + if msgType != websocket.PingMessage { + //消息传入缓冲通道 + w.inChan <- data + } } } } From fdaddf9184a71a9e18f093b1ee3ad960af7fe3b4 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 18:00:46 +0800 Subject: [PATCH 26/56] fix --- constants/websocket.go | 2 -- server/websocket/internal/logic/datatransferlogic.go | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/constants/websocket.go b/constants/websocket.go index 71f1fc36..93648380 100644 --- a/constants/websocket.go +++ b/constants/websocket.go @@ -8,8 +8,6 @@ const ( WEBSOCKET_UNAUTH = "WEBSOCKET_UNAUTH" //ws连接成功 WEBSOCKET_CONNECT_SUCCESS = "WEBSOCKET_CONNECT_SUCCESS" - //心跳信息 - WEBSOCKET_HEARTBEAT = "WEBSOCKET_HEARTBEAT" //图片渲染 WEBSOCKET_RENDER_IMAGE = "WEBSOCKET_RENDER_IMAGE" //数据格式错误 diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 88097476..6c317518 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -142,12 +142,12 @@ func (l *DataTransferLogic) checkAuth(svcCtx *svc.ServiceContext, r *http.Reques // 心跳 func (w *wsConnectItem) heartbeat() { + tick := time.Tick(time.Second * 10) for { - time.Sleep(time.Second * 10) select { case <-w.closeChan: return - default: + case <-tick: //发送心跳信息 if err := w.conn.WriteMessage(websocket.PongMessage, nil); err != nil { logx.Error("发送心跳信息异常,关闭连接:", w.flag, err) From 265fd81b279be63f326c7b5e1d4e7785718f0c99 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 18:03:27 +0800 Subject: [PATCH 27/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 6c317518..b0961617 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -106,7 +106,7 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp rsp.T = constants.WEBSOCKET_CONNECT_SUCCESS rsp.D = flag b, _ := json.Marshal(rsp) - conn.WriteMessage(websocket.TextMessage, b) + _ = conn.WriteMessage(websocket.TextMessage, b) //循环读客户端信息 go ws.readLoop() //循环把数据发送给客户端 @@ -181,7 +181,11 @@ func (w *wsConnectItem) writeLoop() { case <-w.closeChan: //如果关闭了 return case data := <-w.outChan: - w.conn.WriteMessage(websocket.TextMessage, data) + if err := w.conn.WriteMessage(websocket.TextMessage, data); err != nil { + logx.Error("websocket write loop err:", err) + w.close() + return + } } } } From 32e954ccc870ffc549e75e236c6f4b67d69345c2 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 18:09:09 +0800 Subject: [PATCH 28/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index b0961617..b67eaa80 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -42,6 +42,7 @@ var ( } //升级websocket upgrade = websocket.Upgrader{ + ReadBufferSize: 1024 * 10, //最大可读取大小 10M //握手超时时间15s HandshakeTimeout: time.Second * 15, //允许跨域 From 9255a0073f195faa04202517752531186def7ba5 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 18:14:04 +0800 Subject: [PATCH 29/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index b67eaa80..cac946a2 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -88,7 +88,7 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp return }*/ //生成连接唯一标识 - flag := uuid.New().String() + time.Now().Format("20060102150405") + flag := uuid.New().String() + "" ws := wsConnectItem{ conn: conn, flag: flag, From 857a632ceac53c1d103d2f67035aa12b2f5468d9 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 18:20:15 +0800 Subject: [PATCH 30/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 1 - 1 file changed, 1 deletion(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index cac946a2..49e81fe0 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -79,7 +79,6 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp //鉴权不成功10秒后断开 /*if !l.checkAuth(svcCtx, r) { rsp.T = constants.WEBSOCKET_UNAUTH - rsp.D = "unAuth" b, _ := json.Marshal(rsp) //先发一条正常信息 _ = conn.WriteMessage(websocket.TextMessage, b) From 636bc0aee715f8d2f6cd072a90495e059007bac0 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 18:32:39 +0800 Subject: [PATCH 31/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 49e81fe0..c8270928 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -148,6 +148,8 @@ func (w *wsConnectItem) heartbeat() { case <-w.closeChan: return case <-tick: + //设置写超时时间10s + _ = w.conn.SetWriteDeadline(time.Now().Add(time.Second * 10)) //发送心跳信息 if err := w.conn.WriteMessage(websocket.PongMessage, nil); err != nil { logx.Error("发送心跳信息异常,关闭连接:", w.flag, err) From f609fa364dc5b95477c681af9a2b8ebea85ebb65 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 19:19:20 +0800 Subject: [PATCH 32/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index c8270928..2250611e 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -142,7 +142,7 @@ func (l *DataTransferLogic) checkAuth(svcCtx *svc.ServiceContext, r *http.Reques // 心跳 func (w *wsConnectItem) heartbeat() { - tick := time.Tick(time.Second * 10) + tick := time.Tick(time.Second * 2) for { select { case <-w.closeChan: @@ -166,6 +166,7 @@ func (w *wsConnectItem) close() { defer w.mutex.Unlock() logx.Info("websocket:", w.flag, " is closing...") //发送关闭信息 + _ = w.conn.SetWriteDeadline(time.Now().Add(time.Second)) _ = w.conn.WriteMessage(websocket.CloseMessage, nil) w.conn.Close() mapConnPool.Delete(w.flag) From 72543d519f6fdba25099c38fcdf939a402d903f1 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 19:29:23 +0800 Subject: [PATCH 33/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 2250611e..49e81fe0 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -142,14 +142,12 @@ func (l *DataTransferLogic) checkAuth(svcCtx *svc.ServiceContext, r *http.Reques // 心跳 func (w *wsConnectItem) heartbeat() { - tick := time.Tick(time.Second * 2) + tick := time.Tick(time.Second * 10) for { select { case <-w.closeChan: return case <-tick: - //设置写超时时间10s - _ = w.conn.SetWriteDeadline(time.Now().Add(time.Second * 10)) //发送心跳信息 if err := w.conn.WriteMessage(websocket.PongMessage, nil); err != nil { logx.Error("发送心跳信息异常,关闭连接:", w.flag, err) @@ -166,7 +164,6 @@ func (w *wsConnectItem) close() { defer w.mutex.Unlock() logx.Info("websocket:", w.flag, " is closing...") //发送关闭信息 - _ = w.conn.SetWriteDeadline(time.Now().Add(time.Second)) _ = w.conn.WriteMessage(websocket.CloseMessage, nil) w.conn.Close() mapConnPool.Delete(w.flag) From 2b30911ec64750541d54d324f05fcfd491a366a2 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 10:06:27 +0800 Subject: [PATCH 34/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 49e81fe0..0a043c6e 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -49,7 +49,8 @@ var ( CheckOrigin: func(r *http.Request) bool { return true }, - WriteBufferPool: &buffPool, + WriteBufferPool: &buffPool, + EnableCompression: true, } //连接map池 mapConnPool = sync.Map{} From 2cb4549f008a4fb6721c44fd8ac30ce473e826f3 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 10:08:30 +0800 Subject: [PATCH 35/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 0a043c6e..01df20fe 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -143,7 +143,7 @@ func (l *DataTransferLogic) checkAuth(svcCtx *svc.ServiceContext, r *http.Reques // 心跳 func (w *wsConnectItem) heartbeat() { - tick := time.Tick(time.Second * 10) + tick := time.Tick(time.Second * 5) for { select { case <-w.closeChan: From 2a06a28ad68701b168faf9eae4e40aef392dc7a8 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 10:46:08 +0800 Subject: [PATCH 36/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 01df20fe..7ba85da9 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -76,9 +76,11 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp return } defer conn.Close() + w.Header().Set("Connection", "Upgrade") rsp := types.DataTransferData{} //鉴权不成功10秒后断开 - /*if !l.checkAuth(svcCtx, r) { + if !l.checkAuth(svcCtx, r) { + time.Sleep(time.Second) //兼容下火狐 rsp.T = constants.WEBSOCKET_UNAUTH b, _ := json.Marshal(rsp) //先发一条正常信息 @@ -86,7 +88,7 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp //发送关闭信息 _ = conn.WriteMessage(websocket.CloseMessage, nil) return - }*/ + } //生成连接唯一标识 flag := uuid.New().String() + "" ws := wsConnectItem{ @@ -104,6 +106,7 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp mapConnPool.Store(flag, ws) defer ws.close() //把连接成功消息发回去 + time.Sleep(time.Second) //兼容下火狐 rsp.T = constants.WEBSOCKET_CONNECT_SUCCESS rsp.D = flag b, _ := json.Marshal(rsp) From 84692531313be7f9959517cf77fa39037fea32f4 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 12:32:03 +0800 Subject: [PATCH 37/56] fix --- .../internal/logic/datatransferlogic.go | 41 +++++++------ .../internal/logic/rendernotifylogic.go | 57 +++++++++---------- .../internal/logic/ws_render_image_logic.go | 23 ++++---- server/websocket/internal/types/types.go | 34 ++++++----- server_api/websocket.api | 36 ++++++------ 5 files changed, 101 insertions(+), 90 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 7ba85da9..f6935f10 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -65,6 +65,7 @@ type wsConnectItem struct { inChan chan []byte //接受消息缓冲通道 outChan chan []byte //发送回客户端的消息 mutex sync.Mutex //互斥锁 + userId int64 //用户id renderProperty renderProperty //扩展云渲染属性 } @@ -79,16 +80,17 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp w.Header().Set("Connection", "Upgrade") rsp := types.DataTransferData{} //鉴权不成功10秒后断开 - if !l.checkAuth(svcCtx, r) { - time.Sleep(time.Second) //兼容下火狐 - rsp.T = constants.WEBSOCKET_UNAUTH - b, _ := json.Marshal(rsp) - //先发一条正常信息 - _ = conn.WriteMessage(websocket.TextMessage, b) - //发送关闭信息 - _ = conn.WriteMessage(websocket.CloseMessage, nil) - return - } + /* isAuth, userInfo := l.checkAuth(svcCtx, r) + if !isAuth { + time.Sleep(time.Second) //兼容下火狐 + rsp.T = constants.WEBSOCKET_UNAUTH + b, _ := json.Marshal(rsp) + //先发一条正常信息 + _ = conn.WriteMessage(websocket.TextMessage, b) + //发送关闭信息 + _ = conn.WriteMessage(websocket.CloseMessage, nil) + return + }*/ //生成连接唯一标识 flag := uuid.New().String() + "" ws := wsConnectItem{ @@ -102,6 +104,9 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100), }, } + /*if userInfo != nil { + ws.userId = userInfo.UserId + }*/ //保存连接 mapConnPool.Store(flag, ws) defer ws.close() @@ -124,24 +129,24 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp } // 鉴权 -func (l *DataTransferLogic) checkAuth(svcCtx *svc.ServiceContext, r *http.Request) bool { +func (l *DataTransferLogic) checkAuth(svcCtx *svc.ServiceContext, r *http.Request) (isAuth bool, userInfo *auth.UserInfo) { // 解析JWT token,并对空用户进行判断 claims, err := svcCtx.ParseJwtToken(r) // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { - return false + return false, nil } if claims != nil { // 从token中获取对应的用户信息 - _, err = auth.GetUserInfoFormMapClaims(claims) + userInfo, err = auth.GetUserInfoFormMapClaims(claims) // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 if err != nil { - return false + return false, nil } } else { - return false + return false, nil } - return true + return true, userInfo } // 心跳 @@ -240,8 +245,8 @@ func (w *wsConnectItem) sendToOutChan(data []byte) { } // 获取需要渲染图片的map key -func (w *wsConnectItem) getRenderImageMapKey(productId, sizeId, templateId int64) string { - return fmt.Sprintf("%d-%d-%d", productId, sizeId, templateId) +func (w *wsConnectItem) getRenderImageMapKey(productId, mapsSourceId, templateId, renderDesignId int64) string { + return fmt.Sprintf("%d-%d-%d-%d", productId, mapsSourceId, templateId, renderDesignId) } // 处理接受到的数据 diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index 5b3e3d2d..68a49e0d 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -41,18 +41,16 @@ func NewRenderNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Rend // } func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basic.Response) { - if len(req.NotifyList) == 0 { - return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "notify list is empty") - } if time.Now().Unix()-120 > req.Time /*|| req.Time > time.Now().Unix() */ { - return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "time is expire") + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param time") } //验证签名 sha256 - notifyByte, _ := json.Marshal(req.NotifyList) + 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) sign := hex.EncodeToString(signHex) + fmt.Println(sign) if req.Sign != sign { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid sign") } @@ -61,34 +59,31 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi //断言连接 ws, ok := value.(wsConnectItem) if !ok { - return false + return true } - //遍历数据 - for _, notifyItem := range req.NotifyList { - renderKey := ws.getRenderImageMapKey(notifyItem.ProductId, notifyItem.SizeId, notifyItem.TemplateId) - //查询有无该渲染任务 - _, ok = ws.renderProperty.renderImageTask[renderKey] - if !ok { - continue - } - rspData := types.DataTransferData{ - T: constants.WEBSOCKET_RENDER_IMAGE, - D: types.RenderImageRspMsg{ - ProductId: notifyItem.ProductId, - SizeId: notifyItem.SizeId, - TemplateId: notifyItem.TemplateId, - Source: notifyItem.Source, - }, - } - b, _ := json.Marshal(rspData) - //删除对应的需要渲染的图片map - ws.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ - Option: 0, //0删除 1添加 - Key: renderKey, - } - //发送数据到out chan - ws.sendToOutChan(b) + renderKey := ws.getRenderImageMapKey(req.Info.ProductId, req.Info.MapsSourceId, req.Info.TemplateId, req.Info.RenderDesignId) + //查询有无该渲染任务 + _, ok = ws.renderProperty.renderImageTask[renderKey] + if !ok { + return true } + rspData := types.DataTransferData{ + T: constants.WEBSOCKET_RENDER_IMAGE, + D: types.RenderImageRspMsg{ + ProductId: req.Info.ProductId, + MapsSourceId: req.Info.MapsSourceId, + TemplateId: req.Info.TemplateId, + RenderSource: req.Info.RenderSource, + }, + } + b, _ := json.Marshal(rspData) + //删除对应的需要渲染的图片map + ws.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ + Option: 0, //0删除 1添加 + Key: renderKey, + } + //发送数据到out chan + ws.sendToOutChan(b) return true }) return resp.SetStatus(basic.CodeOK) diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index b93e4b67..5dc17dd4 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -28,17 +28,20 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) { logx.Info("收到请求云渲染图片数据:", renderImageData) //把需要渲染的图片任务加进去 for _, v := range renderImageData { - select { - case <-w.closeChan: //连接关闭了 - return - default: - //加入渲染任务 - key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId) - w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ - Option: 1, //0删除 1添加 - Key: key, + for _, productId := range v.ProductIds { + select { + case <-w.closeChan: //连接关闭了 + return + default: + //加入渲染任务 + key := w.getRenderImageMapKey(productId, v.MapsSourceId, v.TemplateId, v.RenderDesignId) + w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ + Option: 1, //0删除 1添加 + Key: key, + } + // TODO 数据发送给云渲染服务器 + v.UserId = w.userId //赋值 } - // TODO 数据发送给云渲染服务器 } } } diff --git a/server/websocket/internal/types/types.go b/server/websocket/internal/types/types.go index d3336021..27260328 100644 --- a/server/websocket/internal/types/types.go +++ b/server/websocket/internal/types/types.go @@ -11,29 +11,33 @@ type DataTransferData struct { } type RenderImageReqMsg struct { - ProductId int64 `json:"product_id"` - SizeId int64 `json:"size_id"` - TemplateId int64 `json:"template_id"` + ProductIds []int64 `json:"product_ids"` //产品 id + MapsSourceId int64 `json:"maps_source_id"` //贴图数据id + TemplateId int64 `json:"template_id"` //模板id + UserId int64 `json:"user_id"` //用户id(这个前端不用传,后台获取) + RenderDesignId int64 `json:"render_design_id"` //渲染设计id } type RenderImageRspMsg struct { - ProductId int64 `json:"product_id"` - SizeId int64 `json:"size_id"` - TemplateId int64 `json:"template_id"` - Source string `json:"source"` + ProductId int64 `json:"product_id"` //产品 id + MapsSourceId int64 `json:"maps_source_id"` //贴图数据id + TemplateId int64 `json:"template_id"` //模板id + RenderDesignId int64 `json:"render_design_id"` //渲染设计id + RenderSource interface{} `json:"render_source"` //渲染结果数据 } type RenderNotifyReq struct { - Sign string `json:"sign"` - Time int64 `json:"time"` - NotifyList []NotifyItem `json:"notify_list"` + Sign string `json:"sign"` + Time int64 `json:"time"` + Info NotifyInfo `json:"info"` } -type NotifyItem struct { - ProductId int64 `json:"product_id"` - SizeId int64 `json:"size_id"` - TemplateId int64 `json:"template_id"` - Source string `json:"source"` +type NotifyInfo struct { + ProductId int64 `json:"product_id"` + MapsSourceId int64 `json:"maps_source_id"` + TemplateId int64 `json:"template_id"` + RenderDesignId int64 `json:"render_design_id"` //渲染设计id + RenderSource interface{} `json:"render_source"` //渲染完返回的数据 } type Request struct { diff --git a/server_api/websocket.api b/server_api/websocket.api index 0d445c71..15366697 100644 --- a/server_api/websocket.api +++ b/server_api/websocket.api @@ -22,26 +22,30 @@ type DataTransferData { T string `json:"t"` //消息类型 D interface{} `json:"d"` //传递的消息 } -type RenderImageReqMsg { //websocket接受需要云渲染的图片 - ProductId int64 `json:"product_id"` - SizeId int64 `json:"size_id"` - TemplateId int64 `json:"template_id"` +type RenderImageReqMsg { //websocket接受要云渲染处理的数据 + ProductIds []int64 `json:"product_ids"` //产品 id + MapsSourceId int64 `json:"maps_source_id"` //贴图数据id + TemplateId int64 `json:"template_id"` //模板id + UserId int64 `json:"user_id"` //用户id(这个前端不用传,后台获取) + RenderDesignId int64 `json:"render_design_id"` //渲染设计id } type RenderImageRspMsg { //websocket发送渲染完的数据 - ProductId int64 `json:"product_id"` - SizeId int64 `json:"size_id"` - TemplateId int64 `json:"template_id"` - Source string `json:"source"` + ProductId int64 `json:"product_id"` //产品 id + MapsSourceId int64 `json:"maps_source_id"` //贴图数据id + TemplateId int64 `json:"template_id"` //模板id + RenderDesignId int64 `json:"render_design_id"` //渲染设计id + RenderSource interface{} `json:"render_source"` //渲染结果数据 } //渲染完了通知接口 type RenderNotifyReq { - Sign string `json:"sign"` - Time int64 `json:"time"` - NotifyList []NotifyItem `json:"notify_list"` + Sign string `json:"sign"` + Time int64 `json:"time"` + Info NotifyInfo `json:"info"` } -type NotifyItem { - ProductId int64 `json:"product_id"` - SizeId int64 `json:"size_id"` - TemplateId int64 `json:"template_id"` - Source string `json:"source"` +type NotifyInfo { + ProductId int64 `json:"product_id"` + MapsSourceId int64 `json:"maps_source_id"` + TemplateId int64 `json:"template_id"` + RenderDesignId int64 `json:"render_design_id"` //渲染设计id + RenderSource interface{} `json:"render_source"` //渲染完返回的数据 } \ No newline at end of file From c375d17c07fd12124a1c415faea17927469c8ff5 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 14:10:41 +0800 Subject: [PATCH 38/56] fix --- .../internal/logic/rendernotifylogic.go | 4 ++-- server/websocket/internal/types/types.go | 20 +++++++++---------- server_api/websocket.api | 20 +++++++++---------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index 68a49e0d..f2b2ebdb 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -50,7 +50,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi h.Write([]byte(fmt.Sprintf(constants.RENDER_NOTIFY_SIGN_KEY, string(notifyByte), req.Time))) signHex := h.Sum(nil) sign := hex.EncodeToString(signHex) - fmt.Println(sign) + //fmt.Println(sign) if req.Sign != sign { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid sign") } @@ -73,7 +73,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi ProductId: req.Info.ProductId, MapsSourceId: req.Info.MapsSourceId, TemplateId: req.Info.TemplateId, - RenderSource: req.Info.RenderSource, + RenderResult: req.Info.RenderResult, }, } b, _ := json.Marshal(rspData) diff --git a/server/websocket/internal/types/types.go b/server/websocket/internal/types/types.go index 27260328..29427449 100644 --- a/server/websocket/internal/types/types.go +++ b/server/websocket/internal/types/types.go @@ -19,11 +19,11 @@ type RenderImageReqMsg struct { } type RenderImageRspMsg struct { - ProductId int64 `json:"product_id"` //产品 id - MapsSourceId int64 `json:"maps_source_id"` //贴图数据id - TemplateId int64 `json:"template_id"` //模板id - RenderDesignId int64 `json:"render_design_id"` //渲染设计id - RenderSource interface{} `json:"render_source"` //渲染结果数据 + ProductId int64 `json:"product_id"` //产品 id + MapsSourceId int64 `json:"maps_source_id"` //贴图数据id + TemplateId int64 `json:"template_id"` //模板id + RenderDesignId int64 `json:"render_design_id"` //渲染设计id + RenderResult string `json:"render_result"` //渲染结果数据 } type RenderNotifyReq struct { @@ -33,11 +33,11 @@ type RenderNotifyReq struct { } type NotifyInfo struct { - ProductId int64 `json:"product_id"` - MapsSourceId int64 `json:"maps_source_id"` - TemplateId int64 `json:"template_id"` - RenderDesignId int64 `json:"render_design_id"` //渲染设计id - RenderSource interface{} `json:"render_source"` //渲染完返回的数据 + ProductId int64 `json:"product_id"` + MapsSourceId int64 `json:"maps_source_id"` + TemplateId int64 `json:"template_id"` + RenderDesignId int64 `json:"render_design_id"` //渲染设计id + RenderResult string `json:"render_result"` //渲染结果数据 } type Request struct { diff --git a/server_api/websocket.api b/server_api/websocket.api index 15366697..90ff6e31 100644 --- a/server_api/websocket.api +++ b/server_api/websocket.api @@ -30,11 +30,11 @@ type RenderImageReqMsg { //websocket接受要云渲染处理的数据 RenderDesignId int64 `json:"render_design_id"` //渲染设计id } type RenderImageRspMsg { //websocket发送渲染完的数据 - ProductId int64 `json:"product_id"` //产品 id - MapsSourceId int64 `json:"maps_source_id"` //贴图数据id - TemplateId int64 `json:"template_id"` //模板id - RenderDesignId int64 `json:"render_design_id"` //渲染设计id - RenderSource interface{} `json:"render_source"` //渲染结果数据 + ProductId int64 `json:"product_id"` //产品 id + MapsSourceId int64 `json:"maps_source_id"` //贴图数据id + TemplateId int64 `json:"template_id"` //模板id + RenderDesignId int64 `json:"render_design_id"` //渲染设计id + RenderResult string `json:"render_result"` //渲染结果数据 } //渲染完了通知接口 type RenderNotifyReq { @@ -43,9 +43,9 @@ type RenderNotifyReq { Info NotifyInfo `json:"info"` } type NotifyInfo { - ProductId int64 `json:"product_id"` - MapsSourceId int64 `json:"maps_source_id"` - TemplateId int64 `json:"template_id"` - RenderDesignId int64 `json:"render_design_id"` //渲染设计id - RenderSource interface{} `json:"render_source"` //渲染完返回的数据 + ProductId int64 `json:"product_id"` + MapsSourceId int64 `json:"maps_source_id"` + TemplateId int64 `json:"template_id"` + RenderDesignId int64 `json:"render_design_id"` //渲染设计id + RenderResult string `json:"render_result"` //渲染结果数据 } \ No newline at end of file From 55a879ada836f5a73f37bc0f92080fa4808aae1e Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 14:31:53 +0800 Subject: [PATCH 39/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index f6935f10..56f47c85 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -92,7 +92,7 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp return }*/ //生成连接唯一标识 - flag := uuid.New().String() + "" + flag := uuid.New().String() + "time=" + time.Now().Format("15-04-05") ws := wsConnectItem{ conn: conn, flag: flag, From 7297a1ef020873f8bb5dfc3e10624ab42faaa5cb Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 15:46:07 +0800 Subject: [PATCH 40/56] fix --- .../internal/logic/datatransferlogic.go | 30 ++++++++----------- .../internal/logic/rendernotifylogic.go | 11 ++++--- .../internal/logic/ws_render_image_logic.go | 27 ++++++++--------- server/websocket/internal/types/types.go | 23 +++++--------- server_api/websocket.api | 23 +++++--------- 5 files changed, 46 insertions(+), 68 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 56f47c85..34d14adb 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -65,7 +65,6 @@ type wsConnectItem struct { inChan chan []byte //接受消息缓冲通道 outChan chan []byte //发送回客户端的消息 mutex sync.Mutex //互斥锁 - userId int64 //用户id renderProperty renderProperty //扩展云渲染属性 } @@ -80,17 +79,17 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp w.Header().Set("Connection", "Upgrade") rsp := types.DataTransferData{} //鉴权不成功10秒后断开 - /* isAuth, userInfo := l.checkAuth(svcCtx, r) - if !isAuth { - time.Sleep(time.Second) //兼容下火狐 - rsp.T = constants.WEBSOCKET_UNAUTH - b, _ := json.Marshal(rsp) - //先发一条正常信息 - _ = conn.WriteMessage(websocket.TextMessage, b) - //发送关闭信息 - _ = conn.WriteMessage(websocket.CloseMessage, nil) - return - }*/ + /*isAuth, _ := l.checkAuth(svcCtx, r) + if !isAuth { + time.Sleep(time.Second) //兼容下火狐 + rsp.T = constants.WEBSOCKET_UNAUTH + b, _ := json.Marshal(rsp) + //先发一条正常信息 + _ = conn.WriteMessage(websocket.TextMessage, b) + //发送关闭信息 + _ = conn.WriteMessage(websocket.CloseMessage, nil) + return + }*/ //生成连接唯一标识 flag := uuid.New().String() + "time=" + time.Now().Format("15-04-05") ws := wsConnectItem{ @@ -104,9 +103,6 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100), }, } - /*if userInfo != nil { - ws.userId = userInfo.UserId - }*/ //保存连接 mapConnPool.Store(flag, ws) defer ws.close() @@ -245,8 +241,8 @@ func (w *wsConnectItem) sendToOutChan(data []byte) { } // 获取需要渲染图片的map key -func (w *wsConnectItem) getRenderImageMapKey(productId, mapsSourceId, templateId, renderDesignId int64) string { - return fmt.Sprintf("%d-%d-%d-%d", productId, mapsSourceId, templateId, renderDesignId) +func (w *wsConnectItem) getRenderImageMapKey(productId, templateTagId int64) string { + return fmt.Sprintf("%d-%d", productId, templateTagId) } // 处理接受到的数据 diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index f2b2ebdb..20ab5c9a 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -50,7 +50,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi h.Write([]byte(fmt.Sprintf(constants.RENDER_NOTIFY_SIGN_KEY, string(notifyByte), req.Time))) signHex := h.Sum(nil) sign := hex.EncodeToString(signHex) - //fmt.Println(sign) + fmt.Println(sign) if req.Sign != sign { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid sign") } @@ -61,7 +61,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi if !ok { return true } - renderKey := ws.getRenderImageMapKey(req.Info.ProductId, req.Info.MapsSourceId, req.Info.TemplateId, req.Info.RenderDesignId) + renderKey := ws.getRenderImageMapKey(req.Info.ProductId, req.Info.TemplateTagId) //查询有无该渲染任务 _, ok = ws.renderProperty.renderImageTask[renderKey] if !ok { @@ -70,10 +70,9 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi rspData := types.DataTransferData{ T: constants.WEBSOCKET_RENDER_IMAGE, D: types.RenderImageRspMsg{ - ProductId: req.Info.ProductId, - MapsSourceId: req.Info.MapsSourceId, - TemplateId: req.Info.TemplateId, - RenderResult: req.Info.RenderResult, + ProductId: req.Info.ProductId, + TemplateTagId: req.Info.TemplateTagId, + Image: req.Info.Image, }, } b, _ := json.Marshal(rspData) diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index 5dc17dd4..012d7d6b 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -20,28 +20,25 @@ type renderImageControlChanItem struct { // 渲染请求数据处理发送云渲染服务处理 func (w *wsConnectItem) SendToCloudRender(data []byte) { - var renderImageData []types.RenderImageReqMsg + 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) //把需要渲染的图片任务加进去 - for _, v := range renderImageData { - for _, productId := range v.ProductIds { - select { - case <-w.closeChan: //连接关闭了 - return - default: - //加入渲染任务 - key := w.getRenderImageMapKey(productId, v.MapsSourceId, v.TemplateId, v.RenderDesignId) - w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ - Option: 1, //0删除 1添加 - Key: key, - } - // TODO 数据发送给云渲染服务器 - v.UserId = w.userId //赋值 + for _, productId := range renderImageData.ProductIds { + select { + case <-w.closeChan: //连接关闭了 + return + default: + //加入渲染任务 + key := w.getRenderImageMapKey(productId, renderImageData.TemplateTagId) + w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ + Option: 1, //0删除 1添加 + Key: key, } + // TODO 数据发送给云渲染服务器 } } } diff --git a/server/websocket/internal/types/types.go b/server/websocket/internal/types/types.go index 29427449..01801c85 100644 --- a/server/websocket/internal/types/types.go +++ b/server/websocket/internal/types/types.go @@ -11,19 +11,14 @@ type DataTransferData struct { } type RenderImageReqMsg struct { - ProductIds []int64 `json:"product_ids"` //产品 id - MapsSourceId int64 `json:"maps_source_id"` //贴图数据id - TemplateId int64 `json:"template_id"` //模板id - UserId int64 `json:"user_id"` //用户id(这个前端不用传,后台获取) - RenderDesignId int64 `json:"render_design_id"` //渲染设计id + ProductIds []int64 `json:"product_ids"` //产品 id + TemplateTagId int64 `json:"template_tag_id"` //模板标签id } type RenderImageRspMsg struct { - ProductId int64 `json:"product_id"` //产品 id - MapsSourceId int64 `json:"maps_source_id"` //贴图数据id - TemplateId int64 `json:"template_id"` //模板id - RenderDesignId int64 `json:"render_design_id"` //渲染设计id - RenderResult string `json:"render_result"` //渲染结果数据 + ProductId int64 `json:"product_id"` //产品 id + TemplateTagId int64 `json:"template_tag_id"` //模板标签id + Image string `json:"image"` //渲染后的图片 } type RenderNotifyReq struct { @@ -33,11 +28,9 @@ type RenderNotifyReq struct { } type NotifyInfo struct { - ProductId int64 `json:"product_id"` - MapsSourceId int64 `json:"maps_source_id"` - TemplateId int64 `json:"template_id"` - RenderDesignId int64 `json:"render_design_id"` //渲染设计id - RenderResult string `json:"render_result"` //渲染结果数据 + ProductId int64 `json:"product_id"` + TemplateTagId int64 `json:"template_tag_id"` //模板标签id + Image string `json:"image"` } type Request struct { diff --git a/server_api/websocket.api b/server_api/websocket.api index 90ff6e31..2b46d61f 100644 --- a/server_api/websocket.api +++ b/server_api/websocket.api @@ -23,18 +23,13 @@ type DataTransferData { D interface{} `json:"d"` //传递的消息 } type RenderImageReqMsg { //websocket接受要云渲染处理的数据 - ProductIds []int64 `json:"product_ids"` //产品 id - MapsSourceId int64 `json:"maps_source_id"` //贴图数据id - TemplateId int64 `json:"template_id"` //模板id - UserId int64 `json:"user_id"` //用户id(这个前端不用传,后台获取) - RenderDesignId int64 `json:"render_design_id"` //渲染设计id + ProductIds []int64 `json:"product_ids"` //产品 id + TemplateTagId int64 `json:"template_tag_id"` //模板标签id } type RenderImageRspMsg { //websocket发送渲染完的数据 - ProductId int64 `json:"product_id"` //产品 id - MapsSourceId int64 `json:"maps_source_id"` //贴图数据id - TemplateId int64 `json:"template_id"` //模板id - RenderDesignId int64 `json:"render_design_id"` //渲染设计id - RenderResult string `json:"render_result"` //渲染结果数据 + ProductId int64 `json:"product_id"` //产品 id + TemplateTagId int64 `json:"template_tag_id"` //模板标签id + Image string `json:"image"` //渲染后的图片 } //渲染完了通知接口 type RenderNotifyReq { @@ -43,9 +38,7 @@ type RenderNotifyReq { Info NotifyInfo `json:"info"` } type NotifyInfo { - ProductId int64 `json:"product_id"` - MapsSourceId int64 `json:"maps_source_id"` - TemplateId int64 `json:"template_id"` - RenderDesignId int64 `json:"render_design_id"` //渲染设计id - RenderResult string `json:"render_result"` //渲染结果数据 + ProductId int64 `json:"product_id"` + TemplateTagId int64 `json:"template_tag_id"` //模板标签id + Image string `json:"image"` } \ No newline at end of file From 5532cd1a11aeebbd1f5a91daabcfc1023551ee23 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 16:13:06 +0800 Subject: [PATCH 41/56] fix --- .../internal/logic/datatransferlogic.go | 4 ++-- .../internal/logic/rendernotifylogic.go | 4 ++-- .../internal/logic/ws_render_image_logic.go | 2 +- server/websocket/internal/types/types.go | 19 +++++++++++-------- server_api/websocket.api | 19 +++++++++++-------- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 34d14adb..582624d1 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -241,8 +241,8 @@ func (w *wsConnectItem) sendToOutChan(data []byte) { } // 获取需要渲染图片的map key -func (w *wsConnectItem) getRenderImageMapKey(productId, templateTagId int64) string { - return fmt.Sprintf("%d-%d", productId, templateTagId) +func (w *wsConnectItem) getRenderImageMapKey(productId, templateTagId int64, algorithmVersion string) string { + return fmt.Sprintf("%d-%d-%s", productId, templateTagId, algorithmVersion) } // 处理接受到的数据 diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index 20ab5c9a..06ab1172 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -50,7 +50,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi h.Write([]byte(fmt.Sprintf(constants.RENDER_NOTIFY_SIGN_KEY, string(notifyByte), req.Time))) signHex := h.Sum(nil) sign := hex.EncodeToString(signHex) - fmt.Println(sign) + //fmt.Println(sign) if req.Sign != sign { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid sign") } @@ -61,7 +61,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi if !ok { return true } - renderKey := ws.getRenderImageMapKey(req.Info.ProductId, req.Info.TemplateTagId) + renderKey := ws.getRenderImageMapKey(req.Info.ProductId, req.Info.TemplateTagId, req.Info.AlgorithmVersion) //查询有无该渲染任务 _, ok = ws.renderProperty.renderImageTask[renderKey] if !ok { diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index 012d7d6b..634b6267 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -33,7 +33,7 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) { return default: //加入渲染任务 - key := w.getRenderImageMapKey(productId, renderImageData.TemplateTagId) + key := w.getRenderImageMapKey(productId, renderImageData.TemplateTagId, renderImageData.AlgorithmVersion) w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ Option: 1, //0删除 1添加 Key: key, diff --git a/server/websocket/internal/types/types.go b/server/websocket/internal/types/types.go index 01801c85..c8aa0da8 100644 --- a/server/websocket/internal/types/types.go +++ b/server/websocket/internal/types/types.go @@ -11,14 +11,16 @@ type DataTransferData struct { } type RenderImageReqMsg struct { - ProductIds []int64 `json:"product_ids"` //产品 id - TemplateTagId int64 `json:"template_tag_id"` //模板标签id + ProductIds []int64 `json:"product_ids"` //产品 id + TemplateTagId int64 `json:"template_tag_id"` //模板标签id + AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 } type RenderImageRspMsg struct { - ProductId int64 `json:"product_id"` //产品 id - TemplateTagId int64 `json:"template_tag_id"` //模板标签id - Image string `json:"image"` //渲染后的图片 + ProductId int64 `json:"product_id"` //产品 id + TemplateTagId int64 `json:"template_tag_id"` //模板标签id + AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 + Image string `json:"image"` //渲染后的图片 } type RenderNotifyReq struct { @@ -28,9 +30,10 @@ type RenderNotifyReq struct { } type NotifyInfo struct { - ProductId int64 `json:"product_id"` - TemplateTagId int64 `json:"template_tag_id"` //模板标签id - Image string `json:"image"` + ProductId int64 `json:"product_id"` //产品id + TemplateTagId int64 `json:"template_tag_id"` //模板标签id + AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 + Image string `json:"image"` } type Request struct { diff --git a/server_api/websocket.api b/server_api/websocket.api index 2b46d61f..c2dc6430 100644 --- a/server_api/websocket.api +++ b/server_api/websocket.api @@ -23,13 +23,15 @@ type DataTransferData { D interface{} `json:"d"` //传递的消息 } type RenderImageReqMsg { //websocket接受要云渲染处理的数据 - ProductIds []int64 `json:"product_ids"` //产品 id - TemplateTagId int64 `json:"template_tag_id"` //模板标签id + ProductIds []int64 `json:"product_ids"` //产品 id + TemplateTagId int64 `json:"template_tag_id"` //模板标签id + AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 } type RenderImageRspMsg { //websocket发送渲染完的数据 - ProductId int64 `json:"product_id"` //产品 id - TemplateTagId int64 `json:"template_tag_id"` //模板标签id - Image string `json:"image"` //渲染后的图片 + ProductId int64 `json:"product_id"` //产品 id + TemplateTagId int64 `json:"template_tag_id"` //模板标签id + AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 + Image string `json:"image"` //渲染后的图片 } //渲染完了通知接口 type RenderNotifyReq { @@ -38,7 +40,8 @@ type RenderNotifyReq { Info NotifyInfo `json:"info"` } type NotifyInfo { - ProductId int64 `json:"product_id"` - TemplateTagId int64 `json:"template_tag_id"` //模板标签id - Image string `json:"image"` + ProductId int64 `json:"product_id"` //产品id + TemplateTagId int64 `json:"template_tag_id"` //模板标签id + AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 + Image string `json:"image"` } \ No newline at end of file From 4eb37c3820a8f5255f7a008f6960382607d2287e Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 16:14:27 +0800 Subject: [PATCH 42/56] fix --- constants/websocket.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/constants/websocket.go b/constants/websocket.go index 93648380..fcc6e372 100644 --- a/constants/websocket.go +++ b/constants/websocket.go @@ -14,5 +14,5 @@ const ( WEBSOCKET_ERR_DATA_FORMAT = "WEBSOCKET_ERR_DATA_FORMAT" ) -// 云渲染通知需要的签名字符串 +// 云渲染完成通知api需要的签名字符串 const RENDER_NOTIFY_SIGN_KEY = "fusen-render-notify-%s-%d" From 58c0580cad22fdfc2f9e3270f8e2d848ff788d4f Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 16:27:42 +0800 Subject: [PATCH 43/56] fix --- constants/websocket.go | 1 + .../internal/logic/datatransferlogic.go | 22 ++++++++++-------- utils/id_generator/wesocket.go | 23 +++++++++++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 utils/id_generator/wesocket.go diff --git a/constants/websocket.go b/constants/websocket.go index fcc6e372..7b4c9ca3 100644 --- a/constants/websocket.go +++ b/constants/websocket.go @@ -12,6 +12,7 @@ const ( WEBSOCKET_RENDER_IMAGE = "WEBSOCKET_RENDER_IMAGE" //数据格式错误 WEBSOCKET_ERR_DATA_FORMAT = "WEBSOCKET_ERR_DATA_FORMAT" + // ) // 云渲染完成通知api需要的签名字符串 diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 582624d1..1b25d9f8 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -7,7 +7,7 @@ import ( "fusenapi/constants" "fusenapi/server/websocket/internal/types" "fusenapi/utils/auth" - "github.com/google/uuid" + "fusenapi/utils/id_generator" "github.com/gorilla/websocket" "net/http" "sync" @@ -34,6 +34,8 @@ func NewDataTransferLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Data } var ( + //全局websocketid生成器 + websocketIdGenerator = id_generator.NewWebsocketId(1) //临时缓存对象池 buffPool = sync.Pool{ New: func() interface{} { @@ -61,7 +63,7 @@ type wsConnectItem struct { conn *websocket.Conn //websocket的连接 closeChan chan struct{} //ws连接关闭chan isClose bool //是否已经关闭 - flag string //ws连接唯一标识 + uniqueId uint64 //ws连接唯一标识 inChan chan []byte //接受消息缓冲通道 outChan chan []byte //发送回客户端的消息 mutex sync.Mutex //互斥锁 @@ -91,10 +93,10 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp return }*/ //生成连接唯一标识 - flag := uuid.New().String() + "time=" + time.Now().Format("15-04-05") + uniqueId := websocketIdGenerator.Get() ws := wsConnectItem{ conn: conn, - flag: flag, + uniqueId: uniqueId, closeChan: make(chan struct{}, 1), inChan: make(chan []byte, 100), outChan: make(chan []byte, 100), @@ -104,12 +106,12 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp }, } //保存连接 - mapConnPool.Store(flag, ws) + mapConnPool.Store(uniqueId, ws) defer ws.close() //把连接成功消息发回去 time.Sleep(time.Second) //兼容下火狐 rsp.T = constants.WEBSOCKET_CONNECT_SUCCESS - rsp.D = flag + rsp.D = uniqueId b, _ := json.Marshal(rsp) _ = conn.WriteMessage(websocket.TextMessage, b) //循环读客户端信息 @@ -155,7 +157,7 @@ func (w *wsConnectItem) heartbeat() { case <-tick: //发送心跳信息 if err := w.conn.WriteMessage(websocket.PongMessage, nil); err != nil { - logx.Error("发送心跳信息异常,关闭连接:", w.flag, err) + logx.Error("发送心跳信息异常,关闭连接:", w.uniqueId, err) w.close() return } @@ -167,16 +169,16 @@ func (w *wsConnectItem) heartbeat() { func (w *wsConnectItem) close() { w.mutex.Lock() defer w.mutex.Unlock() - logx.Info("websocket:", w.flag, " is closing...") + logx.Info("websocket:", w.uniqueId, " is closing...") //发送关闭信息 _ = w.conn.WriteMessage(websocket.CloseMessage, nil) w.conn.Close() - mapConnPool.Delete(w.flag) + mapConnPool.Delete(w.uniqueId) if !w.isClose { w.isClose = true close(w.closeChan) } - logx.Info("websocket:", w.flag, " is closed") + logx.Info("websocket:", w.uniqueId, " is closed") } // 读取输出返回给客户端 diff --git a/utils/id_generator/wesocket.go b/utils/id_generator/wesocket.go new file mode 100644 index 00000000..62f21adf --- /dev/null +++ b/utils/id_generator/wesocket.go @@ -0,0 +1,23 @@ +package id_generator + +import "sync" + +type WebsocketId struct { + nodeId uint64 + count uint64 + mu sync.Mutex +} + +func (wid *WebsocketId) Get() uint64 { + wid.mu.Lock() + defer wid.mu.Unlock() + wid.count++ + return (wid.count << 8) | wid.nodeId +} + +func NewWebsocketId(NodeId uint8) *WebsocketId { + return &WebsocketId{ + nodeId: uint64(NodeId), + count: 0, + } +} From d843fff73d7ba5d4e14a5d86281d5166f04cb303 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 16:29:09 +0800 Subject: [PATCH 44/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 1b25d9f8..999bd381 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -54,7 +54,7 @@ var ( WriteBufferPool: &buffPool, EnableCompression: true, } - //连接map池 + //websocket连接存储 mapConnPool = sync.Map{} ) From cf30fc2b88f79bc35c481f15e9369c53f50161a3 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 17:07:54 +0800 Subject: [PATCH 45/56] fix --- constants/websocket.go | 6 +- server/websocket/internal/handler/routes.go | 5 ++ .../handler/thirdpartyloginnotifyhandler.go | 35 ++++++++ .../logic/thirdpartyloginnotifylogic.go | 86 +++++++++++++++++++ server/websocket/internal/types/types.go | 15 ++++ server_api/websocket.api | 18 +++- 6 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 server/websocket/internal/handler/thirdpartyloginnotifyhandler.go create mode 100644 server/websocket/internal/logic/thirdpartyloginnotifylogic.go diff --git a/constants/websocket.go b/constants/websocket.go index 7b4c9ca3..34e28eb7 100644 --- a/constants/websocket.go +++ b/constants/websocket.go @@ -12,8 +12,12 @@ const ( WEBSOCKET_RENDER_IMAGE = "WEBSOCKET_RENDER_IMAGE" //数据格式错误 WEBSOCKET_ERR_DATA_FORMAT = "WEBSOCKET_ERR_DATA_FORMAT" - // + //第三方登录通知 + WEBSOCKET_THIRD_PARTY_LOGIN_NOTIFY = "WEBSOCKET_THIRD_PARTY_LOGIN_NOTIFY" ) // 云渲染完成通知api需要的签名字符串 const RENDER_NOTIFY_SIGN_KEY = "fusen-render-notify-%s-%d" + +// 第三方登录通知api需要的签名字符串 +const THIRD_PARTY_LOGIN_NOTIFY_SIGN_KEY = "fusen-render-notify-%s-%d" diff --git a/server/websocket/internal/handler/routes.go b/server/websocket/internal/handler/routes.go index 859ecc5b..6649e3c3 100644 --- a/server/websocket/internal/handler/routes.go +++ b/server/websocket/internal/handler/routes.go @@ -22,6 +22,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/websocket/render_notify", Handler: RenderNotifyHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/api/websocket/third_party_login_notify", + Handler: ThirdPartyLoginNotifyHandler(serverCtx), + }, }, ) } diff --git a/server/websocket/internal/handler/thirdpartyloginnotifyhandler.go b/server/websocket/internal/handler/thirdpartyloginnotifyhandler.go new file mode 100644 index 00000000..ae16b24c --- /dev/null +++ b/server/websocket/internal/handler/thirdpartyloginnotifyhandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/websocket/internal/logic" + "fusenapi/server/websocket/internal/svc" + "fusenapi/server/websocket/internal/types" +) + +func ThirdPartyLoginNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.ThirdPartyLoginNotifyReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewThirdPartyLoginNotifyLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.ThirdPartyLoginNotify(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/websocket/internal/logic/thirdpartyloginnotifylogic.go b/server/websocket/internal/logic/thirdpartyloginnotifylogic.go new file mode 100644 index 00000000..ce077259 --- /dev/null +++ b/server/websocket/internal/logic/thirdpartyloginnotifylogic.go @@ -0,0 +1,86 @@ +package logic + +import ( + "crypto/sha256" + "encoding/hex" + "encoding/json" + "fmt" + "fusenapi/constants" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "time" + + "context" + + "fusenapi/server/websocket/internal/svc" + "fusenapi/server/websocket/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type ThirdPartyLoginNotifyLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewThirdPartyLoginNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ThirdPartyLoginNotifyLogic { + return &ThirdPartyLoginNotifyLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *ThirdPartyLoginNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *ThirdPartyLoginNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } + +func (l *ThirdPartyLoginNotifyLogic) ThirdPartyLoginNotify(req *types.ThirdPartyLoginNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) { + if time.Now().Unix()-120 > req.Time /*|| req.Time > time.Now().Unix() */ { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param: time is invalid") + } + if req.Info.WebsocketId <= 0 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:websocket_id is required") + } + if req.Info.Token == "" { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:token is required") + } + //验证签名 sha256 + 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) + sign := hex.EncodeToString(signHex) + //fmt.Println(sign) + if req.Sign != sign { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid sign") + } + //查询对应websocket连接 + val, ok := mapConnPool.Load(req.Info.WebsocketId) + if !ok { + return resp.SetStatusWithMessage(basic.CodeOK, "success:websocket connection is not exists") + } + ws, ok := val.(wsConnectItem) + 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) + select { + case <-ws.closeChan: + return resp.SetStatusWithMessage(basic.CodeOK, "websocket connect object is closed") + case ws.outChan <- b: + return resp.SetStatusWithMessage(basic.CodeOK, "success") + } +} diff --git a/server/websocket/internal/types/types.go b/server/websocket/internal/types/types.go index c8aa0da8..dbeff4db 100644 --- a/server/websocket/internal/types/types.go +++ b/server/websocket/internal/types/types.go @@ -23,6 +23,10 @@ type RenderImageRspMsg struct { Image string `json:"image"` //渲染后的图片 } +type ThirdPartyLoginRspMsg struct { + Token string `json:"token"` +} + type RenderNotifyReq struct { Sign string `json:"sign"` Time int64 `json:"time"` @@ -36,6 +40,17 @@ type NotifyInfo struct { Image string `json:"image"` } +type ThirdPartyLoginNotifyReq struct { + Sign string `json:"sign"` + Time int64 `json:"time"` + Info ThirdPartyLoginNotify `json:"info"` +} + +type ThirdPartyLoginNotify struct { + WebsocketId uint64 `json:"websocket_id"` + Token string `json:"token"` +} + type Request struct { } diff --git a/server_api/websocket.api b/server_api/websocket.api index c2dc6430..379f3e45 100644 --- a/server_api/websocket.api +++ b/server_api/websocket.api @@ -12,9 +12,12 @@ service websocket { //websocket数据交互 @handler DataTransferHandler get /api/websocket/data_transfer(request) returns (response); - //渲染完了通知接口 + //云渲染完了通知接口 @handler RenderNotifyHandler post /api/websocket/render_notify(RenderNotifyReq) returns (response); + //第三方登录通知接口 + @handler ThirdPartyLoginNotifyHandler + post /api/websocket/third_party_login_notify(ThirdPartyLoginNotifyReq) returns (response); } //websocket数据交互 @@ -33,6 +36,9 @@ type RenderImageRspMsg { //websocket发送渲染完的数据 AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 Image string `json:"image"` //渲染后的图片 } +type ThirdPartyLoginRspMsg { //websocket三方登录的通知数据 + Token string `json:"token"` +} //渲染完了通知接口 type RenderNotifyReq { Sign string `json:"sign"` @@ -44,4 +50,14 @@ type NotifyInfo { TemplateTagId int64 `json:"template_tag_id"` //模板标签id AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 Image string `json:"image"` +} +//第三方登录通知接口 +type ThirdPartyLoginNotifyReq { + Sign string `json:"sign"` + Time int64 `json:"time"` + Info ThirdPartyLoginNotify `json:"info"` +} +type ThirdPartyLoginNotify { + WebsocketId uint64 `json:"websocket_id"` + Token string `json:"token"` } \ No newline at end of file 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 46/56] 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) //把需要渲染的图片任务加进去 From 64a284bd77fddd90391654dcf1f07799b8d84839 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 17:37:56 +0800 Subject: [PATCH 47/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 2 +- server/websocket/internal/logic/ws_render_image_logic.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index f72dca20..e2056ba4 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -262,7 +262,7 @@ 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") + w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket message:"+string(data)) return } d, _ := json.Marshal(parseInfo.D) diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index 4803a1f4..ea929c26 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -26,7 +26,7 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) { select { case <-w.closeChan: return - case w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message"): + case w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:"+string(data)): logx.Error("invalid format of websocket render image message", err) return } From 687c18641f8026186949bbabfff11be5a13d952f Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 17:41:36 +0800 Subject: [PATCH 48/56] fix --- .../internal/logic/ws_render_image_logic.go | 56 +++++++++---------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index ea929c26..1f1c6557 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -19,36 +19,6 @@ type renderImageControlChanItem struct { Key string //map的key } -// 渲染请求数据处理发送云渲染服务处理 -func (w *wsConnectItem) SendToCloudRender(data []byte) { - var renderImageData types.RenderImageReqMsg - if err := json.Unmarshal(data, &renderImageData); err != nil { - select { - case <-w.closeChan: - return - case w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:"+string(data)): - logx.Error("invalid format of websocket render image message", err) - return - } - } - logx.Info("收到请求云渲染图片数据:", renderImageData) - //把需要渲染的图片任务加进去 - for _, productId := range renderImageData.ProductIds { - select { - case <-w.closeChan: //连接关闭了 - return - default: - //加入渲染任务 - key := w.getRenderImageMapKey(productId, renderImageData.TemplateTagId, renderImageData.AlgorithmVersion) - w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ - Option: 1, //0删除 1添加 - Key: key, - } - // TODO 数据发送给云渲染服务器 - } - } -} - // 操作连接中渲染任务的增加/删除 func (w *wsConnectItem) operationRenderTask() { for { @@ -67,3 +37,29 @@ func (w *wsConnectItem) operationRenderTask() { } } } + +// 渲染请求数据处理发送云渲染服务处理 +func (w *wsConnectItem) SendToCloudRender(data []byte) { + var renderImageData types.RenderImageReqMsg + if err := json.Unmarshal(data, &renderImageData); err != nil { + w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:"+string(data)) + logx.Error("invalid format of websocket render image message", err) + return + } + logx.Info("收到请求云渲染图片数据:", renderImageData) + //把需要渲染的图片任务加进去 + for _, productId := range renderImageData.ProductIds { + select { + case <-w.closeChan: //连接关闭了 + return + default: + //加入渲染任务 + key := w.getRenderImageMapKey(productId, renderImageData.TemplateTagId, renderImageData.AlgorithmVersion) + w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ + Option: 1, //0删除 1添加 + Key: key, + } + // TODO 数据发送给云渲染服务器 + } + } +} From 18bbc6788fb5da5d0f8200693ff94db28f9ae3bf Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 19:30:40 +0800 Subject: [PATCH 49/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index e2056ba4..b4ffae61 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -44,14 +44,17 @@ var ( } //升级websocket upgrade = websocket.Upgrader{ - ReadBufferSize: 1024 * 10, //最大可读取大小 10M + //最大可读取大小 10M + ReadBufferSize: 1024 * 10, //握手超时时间15s HandshakeTimeout: time.Second * 15, //允许跨域 CheckOrigin: func(r *http.Request) bool { return true }, - WriteBufferPool: &buffPool, + //写的缓存池 + WriteBufferPool: &buffPool, + //是否支持压缩 EnableCompression: true, } //websocket连接存储 From 2c537bf82356710ed72c4230a342fe4596ee8f07 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 19:33:29 +0800 Subject: [PATCH 50/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index b4ffae61..74f14691 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -103,8 +103,8 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp conn: conn, uniqueId: uniqueId, closeChan: make(chan struct{}, 1), - inChan: make(chan []byte, 100), - outChan: make(chan []byte, 100), + inChan: make(chan []byte, 1000), + outChan: make(chan []byte, 1000), renderProperty: renderProperty{ renderImageTask: make(map[string]struct{}), renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100), From 64e7361d509e726e29f3d4bbac0c46191c2976bc Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 28 Jul 2023 10:02:40 +0800 Subject: [PATCH 51/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 1 - 1 file changed, 1 deletion(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 74f14691..e14a687a 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -81,7 +81,6 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp return } defer conn.Close() - w.Header().Set("Connection", "Upgrade") //鉴权不成功10秒后断开 /*isAuth, _ := l.checkAuth(svcCtx, r) if !isAuth { From 164c737dd35c5520651f2370b9a46d4241c50756 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 28 Jul 2023 10:20:35 +0800 Subject: [PATCH 52/56] fix --- server/websocket/internal/logic/datatransferlogic.go | 4 ++-- server/websocket/internal/logic/ws_render_image_logic.go | 2 +- server/websocket/internal/types/types.go | 3 +++ server_api/websocket.api | 3 +++ 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index e14a687a..44ba0abb 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -245,8 +245,8 @@ func (w *wsConnectItem) sendToOutChan(data []byte) { } // 获取需要渲染图片的map key -func (w *wsConnectItem) getRenderImageMapKey(productId, templateTagId int64, algorithmVersion string) string { - return fmt.Sprintf("%d-%d-%s", productId, templateTagId, algorithmVersion) +func (w *wsConnectItem) getRenderImageMapKey(productId, templateTagId, logoId int64, algorithmVersion string) string { + return fmt.Sprintf("%d-%d-%d-%s", productId, templateTagId, logoId, algorithmVersion) } // 格式化返回数据 diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index 1f1c6557..d58def77 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -54,7 +54,7 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) { return default: //加入渲染任务 - key := w.getRenderImageMapKey(productId, renderImageData.TemplateTagId, renderImageData.AlgorithmVersion) + key := w.getRenderImageMapKey(productId, renderImageData.TemplateTagId, renderImageData.LogoId, renderImageData.AlgorithmVersion) w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ Option: 1, //0删除 1添加 Key: key, diff --git a/server/websocket/internal/types/types.go b/server/websocket/internal/types/types.go index dbeff4db..901b8123 100644 --- a/server/websocket/internal/types/types.go +++ b/server/websocket/internal/types/types.go @@ -13,6 +13,7 @@ type DataTransferData struct { type RenderImageReqMsg struct { ProductIds []int64 `json:"product_ids"` //产品 id TemplateTagId int64 `json:"template_tag_id"` //模板标签id + LogoId int64 `json:"logo_id"` //logoid AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 } @@ -20,6 +21,7 @@ type RenderImageRspMsg struct { ProductId int64 `json:"product_id"` //产品 id TemplateTagId int64 `json:"template_tag_id"` //模板标签id AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 + LogoId int64 `json:"logo_id"` //logoid Image string `json:"image"` //渲染后的图片 } @@ -37,6 +39,7 @@ type NotifyInfo struct { ProductId int64 `json:"product_id"` //产品id TemplateTagId int64 `json:"template_tag_id"` //模板标签id AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 + LogoId int64 `json:"logo_id"` //logoid Image string `json:"image"` } diff --git a/server_api/websocket.api b/server_api/websocket.api index 379f3e45..e24e2874 100644 --- a/server_api/websocket.api +++ b/server_api/websocket.api @@ -28,12 +28,14 @@ type DataTransferData { type RenderImageReqMsg { //websocket接受要云渲染处理的数据 ProductIds []int64 `json:"product_ids"` //产品 id TemplateTagId int64 `json:"template_tag_id"` //模板标签id + LogoId int64 `json:"logo_id"` //logoid AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 } type RenderImageRspMsg { //websocket发送渲染完的数据 ProductId int64 `json:"product_id"` //产品 id TemplateTagId int64 `json:"template_tag_id"` //模板标签id AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 + LogoId int64 `json:"logo_id"` //logoid Image string `json:"image"` //渲染后的图片 } type ThirdPartyLoginRspMsg { //websocket三方登录的通知数据 @@ -49,6 +51,7 @@ type NotifyInfo { ProductId int64 `json:"product_id"` //产品id TemplateTagId int64 `json:"template_tag_id"` //模板标签id AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 + LogoId int64 `json:"logo_id"` //logoid Image string `json:"image"` } //第三方登录通知接口 From aa39146cfbc70715bd21d146d1bd2ad6b86bdc14 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 28 Jul 2023 10:22:01 +0800 Subject: [PATCH 53/56] fix --- server/websocket/internal/logic/rendernotifylogic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index b0088855..8323e8f6 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -57,7 +57,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi if !ok { return true } - renderKey := ws.getRenderImageMapKey(req.Info.ProductId, req.Info.TemplateTagId, req.Info.AlgorithmVersion) + renderKey := ws.getRenderImageMapKey(req.Info.ProductId, req.Info.TemplateTagId, req.Info.LogoId, req.Info.AlgorithmVersion) //查询有无该渲染任务 _, ok = ws.renderProperty.renderImageTask[renderKey] if !ok { From a158f562b6a8befb875da4b838b5302427968321 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 28 Jul 2023 10:37:23 +0800 Subject: [PATCH 54/56] fix --- .../internal/logic/getproducttemplatetagslogic.go | 1 + server/product-template-tag/internal/types/types.go | 1 + server_api/product-template-tag.api | 1 + 3 files changed, 3 insertions(+) diff --git a/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go b/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go index 68c62363..815ef07c 100644 --- a/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go +++ b/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go @@ -47,6 +47,7 @@ func (l *GetProductTemplateTagsLogic) GetProductTemplateTags(req *types.GetProdu list := make([]types.GetProductTemplateTagsRsp, 0, len(productTemplateTags)) for _, v := range productTemplateTags { list = append(list, types.GetProductTemplateTagsRsp{ + Id: v.Id, Tag: *v.Title, Cover: *v.CoverImg, }) diff --git a/server/product-template-tag/internal/types/types.go b/server/product-template-tag/internal/types/types.go index 39866da6..a89ea882 100644 --- a/server/product-template-tag/internal/types/types.go +++ b/server/product-template-tag/internal/types/types.go @@ -10,6 +10,7 @@ type GetProductTemplateTagsReq struct { } type GetProductTemplateTagsRsp struct { + Id int64 `json:"id"` Tag string `json:"tag"` Cover string `json:"cover"` } diff --git a/server_api/product-template-tag.api b/server_api/product-template-tag.api index ae62099a..3b057025 100644 --- a/server_api/product-template-tag.api +++ b/server_api/product-template-tag.api @@ -20,6 +20,7 @@ type GetProductTemplateTagsReq { Limit int `form:"limit"` } type GetProductTemplateTagsRsp { + Id int64 `json:"id"` Tag string `json:"tag"` Cover string `json:"cover"` } \ No newline at end of file From 15b2a80f58eee6e7ace1ca7f8a9f8114e8c2ebcf Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 28 Jul 2023 10:42:04 +0800 Subject: [PATCH 55/56] fix --- server/product-template-tag/internal/handler/routes.go | 2 +- server_api/product-template-tag.api | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/product-template-tag/internal/handler/routes.go b/server/product-template-tag/internal/handler/routes.go index b2c8f181..24a5c707 100644 --- a/server/product-template-tag/internal/handler/routes.go +++ b/server/product-template-tag/internal/handler/routes.go @@ -14,7 +14,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { []rest.Route{ { Method: http.MethodGet, - Path: "/api/product-template/get_product_template_tags", + Path: "/api/product-template-tag/get_product_template_tags", Handler: GetProductTemplateTagsHandler(serverCtx), }, }, diff --git a/server_api/product-template-tag.api b/server_api/product-template-tag.api index 3b057025..c8b1a2cc 100644 --- a/server_api/product-template-tag.api +++ b/server_api/product-template-tag.api @@ -12,7 +12,7 @@ import "basic.api" service product-template-tag { //获取产品模板标签列表 @handler GetProductTemplateTagsHandler - get /api/product-template/get_product_template_tags(GetProductTemplateTagsReq) returns (response); + get /api/product-template-tag/get_product_template_tags(GetProductTemplateTagsReq) returns (response); } //获取产品模板标签列表 From 8f5bcc76ec2ddd6da18e3c1114475357651d763e Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 28 Jul 2023 11:13:47 +0800 Subject: [PATCH 56/56] fix --- server/websocket/internal/logic/rendernotifylogic.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index 8323e8f6..9e5e0c9e 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -57,6 +57,10 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi if !ok { return true } + //关闭标识 + if ws.isClose { + return true + } renderKey := ws.getRenderImageMapKey(req.Info.ProductId, req.Info.TemplateTagId, req.Info.LogoId, req.Info.AlgorithmVersion) //查询有无该渲染任务 _, ok = ws.renderProperty.renderImageTask[renderKey]