This commit is contained in:
laodaming 2023-07-27 17:33:50 +08:00
parent cf30fc2b88
commit 9f9f7d3fd6
4 changed files with 36 additions and 35 deletions

View File

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

View File

@ -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添加

View File

@ -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")

View File

@ -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)
//把需要渲染的图片任务加进去