This commit is contained in:
laodaming 2023-07-27 12:32:03 +08:00
parent 2a06a28ad6
commit 8469253131
5 changed files with 101 additions and 90 deletions

View File

@ -65,6 +65,7 @@ type wsConnectItem struct {
inChan chan []byte //接受消息缓冲通道 inChan chan []byte //接受消息缓冲通道
outChan chan []byte //发送回客户端的消息 outChan chan []byte //发送回客户端的消息
mutex sync.Mutex //互斥锁 mutex sync.Mutex //互斥锁
userId int64 //用户id
renderProperty renderProperty //扩展云渲染属性 renderProperty renderProperty //扩展云渲染属性
} }
@ -79,16 +80,17 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp
w.Header().Set("Connection", "Upgrade") w.Header().Set("Connection", "Upgrade")
rsp := types.DataTransferData{} rsp := types.DataTransferData{}
//鉴权不成功10秒后断开 //鉴权不成功10秒后断开
if !l.checkAuth(svcCtx, r) { /* isAuth, userInfo := l.checkAuth(svcCtx, r)
time.Sleep(time.Second) //兼容下火狐 if !isAuth {
rsp.T = constants.WEBSOCKET_UNAUTH time.Sleep(time.Second) //兼容下火狐
b, _ := json.Marshal(rsp) rsp.T = constants.WEBSOCKET_UNAUTH
//先发一条正常信息 b, _ := json.Marshal(rsp)
_ = conn.WriteMessage(websocket.TextMessage, b) //先发一条正常信息
//发送关闭信息 _ = conn.WriteMessage(websocket.TextMessage, b)
_ = conn.WriteMessage(websocket.CloseMessage, nil) //发送关闭信息
return _ = conn.WriteMessage(websocket.CloseMessage, nil)
} return
}*/
//生成连接唯一标识 //生成连接唯一标识
flag := uuid.New().String() + "<date=" + time.Now().Format("2006-01-02-15-04-05") + ">" flag := uuid.New().String() + "<date=" + time.Now().Format("2006-01-02-15-04-05") + ">"
ws := wsConnectItem{ ws := wsConnectItem{
@ -102,6 +104,9 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp
renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100), renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100),
}, },
} }
/*if userInfo != nil {
ws.userId = userInfo.UserId
}*/
//保存连接 //保存连接
mapConnPool.Store(flag, ws) mapConnPool.Store(flag, ws)
defer ws.close() 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,并对空用户进行判断 // 解析JWT token,并对空用户进行判断
claims, err := svcCtx.ParseJwtToken(r) claims, err := svcCtx.ParseJwtToken(r)
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
if err != nil { if err != nil {
return false return false, nil
} }
if claims != nil { if claims != nil {
// 从token中获取对应的用户信息 // 从token中获取对应的用户信息
_, err = auth.GetUserInfoFormMapClaims(claims) userInfo, err = auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
if err != nil { if err != nil {
return false return false, nil
} }
} else { } else {
return false return false, nil
} }
return true return true, userInfo
} }
// 心跳 // 心跳
@ -240,8 +245,8 @@ func (w *wsConnectItem) sendToOutChan(data []byte) {
} }
// 获取需要渲染图片的map key // 获取需要渲染图片的map key
func (w *wsConnectItem) getRenderImageMapKey(productId, sizeId, templateId int64) string { func (w *wsConnectItem) getRenderImageMapKey(productId, mapsSourceId, templateId, renderDesignId int64) string {
return fmt.Sprintf("%d-%d-%d", productId, sizeId, templateId) return fmt.Sprintf("%d-%d-%d-%d", productId, mapsSourceId, templateId, renderDesignId)
} }
// 处理接受到的数据 // 处理接受到的数据

View File

@ -41,18 +41,16 @@ func NewRenderNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Rend
// } // }
func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basic.Response) { 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() */ { 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 //验证签名 sha256
notifyByte, _ := json.Marshal(req.NotifyList) notifyByte, _ := json.Marshal(req.Info)
h := sha256.New() h := sha256.New()
h.Write([]byte(fmt.Sprintf(constants.RENDER_NOTIFY_SIGN_KEY, string(notifyByte), req.Time))) h.Write([]byte(fmt.Sprintf(constants.RENDER_NOTIFY_SIGN_KEY, string(notifyByte), req.Time)))
signHex := h.Sum(nil) signHex := h.Sum(nil)
sign := hex.EncodeToString(signHex) sign := hex.EncodeToString(signHex)
fmt.Println(sign)
if req.Sign != sign { if req.Sign != sign {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid 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) ws, ok := value.(wsConnectItem)
if !ok { if !ok {
return false return true
} }
//遍历数据 renderKey := ws.getRenderImageMapKey(req.Info.ProductId, req.Info.MapsSourceId, req.Info.TemplateId, req.Info.RenderDesignId)
for _, notifyItem := range req.NotifyList { //查询有无该渲染任务
renderKey := ws.getRenderImageMapKey(notifyItem.ProductId, notifyItem.SizeId, notifyItem.TemplateId) _, ok = ws.renderProperty.renderImageTask[renderKey]
//查询有无该渲染任务 if !ok {
_, ok = ws.renderProperty.renderImageTask[renderKey] return true
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)
} }
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 true
}) })
return resp.SetStatus(basic.CodeOK) return resp.SetStatus(basic.CodeOK)

View File

@ -28,17 +28,20 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) {
logx.Info("收到请求云渲染图片数据:", renderImageData) logx.Info("收到请求云渲染图片数据:", renderImageData)
//把需要渲染的图片任务加进去 //把需要渲染的图片任务加进去
for _, v := range renderImageData { for _, v := range renderImageData {
select { for _, productId := range v.ProductIds {
case <-w.closeChan: //连接关闭了 select {
return case <-w.closeChan: //连接关闭了
default: return
//加入渲染任务 default:
key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId) //加入渲染任务
w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ key := w.getRenderImageMapKey(productId, v.MapsSourceId, v.TemplateId, v.RenderDesignId)
Option: 1, //0删除 1添加 w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{
Key: key, Option: 1, //0删除 1添加
Key: key,
}
// TODO 数据发送给云渲染服务器
v.UserId = w.userId //赋值
} }
// TODO 数据发送给云渲染服务器
} }
} }
} }

View File

@ -11,29 +11,33 @@ type DataTransferData struct {
} }
type RenderImageReqMsg struct { type RenderImageReqMsg struct {
ProductId int64 `json:"product_id"` ProductIds []int64 `json:"product_ids"` //产品 id
SizeId int64 `json:"size_id"` MapsSourceId int64 `json:"maps_source_id"` //贴图数据id
TemplateId int64 `json:"template_id"` TemplateId int64 `json:"template_id"` //模板id
UserId int64 `json:"user_id"` //用户id(这个前端不用传,后台获取)
RenderDesignId int64 `json:"render_design_id"` //渲染设计id
} }
type RenderImageRspMsg struct { type RenderImageRspMsg struct {
ProductId int64 `json:"product_id"` ProductId int64 `json:"product_id"` //产品 id
SizeId int64 `json:"size_id"` MapsSourceId int64 `json:"maps_source_id"` //贴图数据id
TemplateId int64 `json:"template_id"` TemplateId int64 `json:"template_id"` //模板id
Source string `json:"source"` RenderDesignId int64 `json:"render_design_id"` //渲染设计id
RenderSource interface{} `json:"render_source"` //渲染结果数据
} }
type RenderNotifyReq struct { type RenderNotifyReq struct {
Sign string `json:"sign"` Sign string `json:"sign"`
Time int64 `json:"time"` Time int64 `json:"time"`
NotifyList []NotifyItem `json:"notify_list"` Info NotifyInfo `json:"info"`
} }
type NotifyItem struct { type NotifyInfo struct {
ProductId int64 `json:"product_id"` ProductId int64 `json:"product_id"`
SizeId int64 `json:"size_id"` MapsSourceId int64 `json:"maps_source_id"`
TemplateId int64 `json:"template_id"` TemplateId int64 `json:"template_id"`
Source string `json:"source"` RenderDesignId int64 `json:"render_design_id"` //渲染设计id
RenderSource interface{} `json:"render_source"` //渲染完返回的数据
} }
type Request struct { type Request struct {

View File

@ -22,26 +22,30 @@ type DataTransferData {
T string `json:"t"` //消息类型 T string `json:"t"` //消息类型
D interface{} `json:"d"` //传递的消息 D interface{} `json:"d"` //传递的消息
} }
type RenderImageReqMsg { //websocket接受需要云渲染的图片 type RenderImageReqMsg { //websocket接受要云渲染处理的数据
ProductId int64 `json:"product_id"` ProductIds []int64 `json:"product_ids"` //产品 id
SizeId int64 `json:"size_id"` MapsSourceId int64 `json:"maps_source_id"` //贴图数据id
TemplateId int64 `json:"template_id"` TemplateId int64 `json:"template_id"` //模板id
UserId int64 `json:"user_id"` //用户id(这个前端不用传,后台获取)
RenderDesignId int64 `json:"render_design_id"` //渲染设计id
} }
type RenderImageRspMsg { //websocket发送渲染完的数据 type RenderImageRspMsg { //websocket发送渲染完的数据
ProductId int64 `json:"product_id"` ProductId int64 `json:"product_id"` //产品 id
SizeId int64 `json:"size_id"` MapsSourceId int64 `json:"maps_source_id"` //贴图数据id
TemplateId int64 `json:"template_id"` TemplateId int64 `json:"template_id"` //模板id
Source string `json:"source"` RenderDesignId int64 `json:"render_design_id"` //渲染设计id
RenderSource interface{} `json:"render_source"` //渲染结果数据
} }
//渲染完了通知接口 //渲染完了通知接口
type RenderNotifyReq { type RenderNotifyReq {
Sign string `json:"sign"` Sign string `json:"sign"`
Time int64 `json:"time"` Time int64 `json:"time"`
NotifyList []NotifyItem `json:"notify_list"` Info NotifyInfo `json:"info"`
} }
type NotifyItem { type NotifyInfo {
ProductId int64 `json:"product_id"` ProductId int64 `json:"product_id"`
SizeId int64 `json:"size_id"` MapsSourceId int64 `json:"maps_source_id"`
TemplateId int64 `json:"template_id"` TemplateId int64 `json:"template_id"`
Source string `json:"source"` RenderDesignId int64 `json:"render_design_id"` //渲染设计id
RenderSource interface{} `json:"render_source"` //渲染完返回的数据
} }