fix
This commit is contained in:
parent
2a06a28ad6
commit
8469253131
@ -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,7 +80,8 @@ 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)
|
||||||
|
if !isAuth {
|
||||||
time.Sleep(time.Second) //兼容下火狐
|
time.Sleep(time.Second) //兼容下火狐
|
||||||
rsp.T = constants.WEBSOCKET_UNAUTH
|
rsp.T = constants.WEBSOCKET_UNAUTH
|
||||||
b, _ := json.Marshal(rsp)
|
b, _ := json.Marshal(rsp)
|
||||||
@ -88,7 +90,7 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp
|
|||||||
//发送关闭信息
|
//发送关闭信息
|
||||||
_ = conn.WriteMessage(websocket.CloseMessage, nil)
|
_ = conn.WriteMessage(websocket.CloseMessage, nil)
|
||||||
return
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理接受到的数据
|
// 处理接受到的数据
|
||||||
|
@ -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,23 +59,21 @@ 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]
|
_, ok = ws.renderProperty.renderImageTask[renderKey]
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
return true
|
||||||
}
|
}
|
||||||
rspData := types.DataTransferData{
|
rspData := types.DataTransferData{
|
||||||
T: constants.WEBSOCKET_RENDER_IMAGE,
|
T: constants.WEBSOCKET_RENDER_IMAGE,
|
||||||
D: types.RenderImageRspMsg{
|
D: types.RenderImageRspMsg{
|
||||||
ProductId: notifyItem.ProductId,
|
ProductId: req.Info.ProductId,
|
||||||
SizeId: notifyItem.SizeId,
|
MapsSourceId: req.Info.MapsSourceId,
|
||||||
TemplateId: notifyItem.TemplateId,
|
TemplateId: req.Info.TemplateId,
|
||||||
Source: notifyItem.Source,
|
RenderSource: req.Info.RenderSource,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
b, _ := json.Marshal(rspData)
|
b, _ := json.Marshal(rspData)
|
||||||
@ -88,7 +84,6 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi
|
|||||||
}
|
}
|
||||||
//发送数据到out chan
|
//发送数据到out chan
|
||||||
ws.sendToOutChan(b)
|
ws.sendToOutChan(b)
|
||||||
}
|
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
return resp.SetStatus(basic.CodeOK)
|
return resp.SetStatus(basic.CodeOK)
|
||||||
|
@ -28,17 +28,20 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) {
|
|||||||
logx.Info("收到请求云渲染图片数据:", renderImageData)
|
logx.Info("收到请求云渲染图片数据:", renderImageData)
|
||||||
//把需要渲染的图片任务加进去
|
//把需要渲染的图片任务加进去
|
||||||
for _, v := range renderImageData {
|
for _, v := range renderImageData {
|
||||||
|
for _, productId := range v.ProductIds {
|
||||||
select {
|
select {
|
||||||
case <-w.closeChan: //连接关闭了
|
case <-w.closeChan: //连接关闭了
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
//加入渲染任务
|
//加入渲染任务
|
||||||
key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId)
|
key := w.getRenderImageMapKey(productId, v.MapsSourceId, v.TemplateId, v.RenderDesignId)
|
||||||
w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{
|
w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{
|
||||||
Option: 1, //0删除 1添加
|
Option: 1, //0删除 1添加
|
||||||
Key: key,
|
Key: key,
|
||||||
}
|
}
|
||||||
// TODO 数据发送给云渲染服务器
|
// TODO 数据发送给云渲染服务器
|
||||||
|
v.UserId = w.userId //赋值
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
@ -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"` //渲染完返回的数据
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user