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 //接受消息缓冲通道
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() + "<date=" + time.Now().Format("2006-01-02-15-04-05") + ">"
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)
}
// 处理接受到的数据

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) {
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)

View File

@ -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 数据发送给云渲染服务器
}
}
}

View File

@ -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 {

View File

@ -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"` //渲染完返回的数据
}