fix
This commit is contained in:
52
server/websocket/internal/logic/mq_consumer.go
Normal file
52
server/websocket/internal/logic/mq_consumer.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/utils/websocket_data"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
// 消费渲染结果数据
|
||||
type MqConsumerRenderResult struct {
|
||||
}
|
||||
|
||||
func (m *MqConsumerRenderResult) Run(data []byte) error {
|
||||
logx.Info("接收到MqConsumerRenderResult数据:", string(data))
|
||||
var parseInfo websocket_data.RenderImageNotify
|
||||
if err := json.Unmarshal(data, &parseInfo); err != nil {
|
||||
logx.Error("MqConsumerRenderResult data format err:", err)
|
||||
return nil //不返回错误则就删掉该消息
|
||||
}
|
||||
//遍历websocket链接把数据传进去
|
||||
mapConnPool.Range(func(key, value any) bool {
|
||||
//断言连接
|
||||
ws, ok := value.(wsConnectItem)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
//关闭标识
|
||||
if ws.isClose {
|
||||
return true
|
||||
}
|
||||
//查询有无该渲染任务
|
||||
renderId, ok := ws.renderProperty.renderImageTask[parseInfo.TaskId]
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
b := ws.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, websocket_data.RenderImageRspMsg{
|
||||
RenderId: renderId,
|
||||
Image: parseInfo.Image,
|
||||
})
|
||||
//删除对应的需要渲染的图片map
|
||||
ws.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{
|
||||
Option: 0, //0删除 1添加
|
||||
TaskId: parseInfo.TaskId,
|
||||
RenderId: renderId,
|
||||
}
|
||||
//发送数据到out chan
|
||||
ws.sendToOutChan(b)
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/constants"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/websocket_data"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/websocket/internal/svc"
|
||||
"fusenapi/server/websocket/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type RenderNotifyLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewRenderNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RenderNotifyLogic {
|
||||
return &RenderNotifyLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *RenderNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *RenderNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
|
||||
func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basic.Response) {
|
||||
if time.Now().Unix()-120 > req.Time /*|| req.Time > time.Now().Unix() */ {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param time")
|
||||
}
|
||||
//验证签名 sha256
|
||||
/*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")
|
||||
}*/
|
||||
//遍历websocket链接把数据传进去
|
||||
mapConnPool.Range(func(key, value any) bool {
|
||||
//断言连接
|
||||
ws, ok := value.(wsConnectItem)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
//关闭标识
|
||||
if ws.isClose {
|
||||
return true
|
||||
}
|
||||
//查询有无该渲染任务
|
||||
renderId, ok := ws.renderProperty.renderImageTask[req.Info.TaskId]
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
b := ws.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, websocket_data.RenderImageRspMsg{
|
||||
RenderId: renderId,
|
||||
Image: req.Info.Image,
|
||||
})
|
||||
//删除对应的需要渲染的图片map
|
||||
ws.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{
|
||||
Option: 0, //0删除 1添加
|
||||
TaskId: req.Info.TaskId,
|
||||
RenderId: renderId,
|
||||
}
|
||||
//发送数据到out chan
|
||||
ws.sendToOutChan(b)
|
||||
return true
|
||||
})
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/constants"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/websocket_data"
|
||||
"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")
|
||||
}
|
||||
b := ws.respondDataFormat(constants.WEBSOCKET_THIRD_PARTY_LOGIN_NOTIFY, websocket_data.ThirdPartyLoginRspMsg{
|
||||
Token: req.Info.Token,
|
||||
})
|
||||
select {
|
||||
case <-ws.closeChan:
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "websocket connect object is closed")
|
||||
case ws.outChan <- b:
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user