2023-07-25 09:10:50 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fusenapi/utils/basic"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
"github.com/zeromicro/go-zero/rest/httpx"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"fusenapi/server/websocket/internal/svc"
|
|
|
|
"fusenapi/server/websocket/internal/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RenderNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var req types.RenderNotifyReq
|
|
|
|
_, err := basic.RequestParse(w, r, svcCtx, &req)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mapConnPool.Range(func(key, value any) bool {
|
|
|
|
ws, ok := value.(wsConnectItem)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
setOutRenderImage(req, ws)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
httpx.OkJsonCtx(r.Context(), w, basic.Response{
|
|
|
|
Code: 200,
|
|
|
|
Message: "success",
|
|
|
|
Data: nil,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 把渲染好的数据放入outchan
|
|
|
|
func setOutRenderImage(req types.RenderNotifyReq, ws wsConnectItem) {
|
|
|
|
ws.mutex.Lock()
|
|
|
|
defer ws.mutex.Unlock()
|
|
|
|
for _, notifyItem := range req.NotifyList {
|
2023-07-25 09:12:37 +00:00
|
|
|
renderKey := ws.getRenderImageMapKey(notifyItem.ProductId, notifyItem.SizeId, notifyItem.TemplateId)
|
2023-07-25 09:10:50 +00:00
|
|
|
//加载并删除
|
|
|
|
_, ok := ws.renderImage[renderKey]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
responseData := types.RenderImageRspMsg{
|
|
|
|
ProductId: notifyItem.ProductId,
|
|
|
|
SizeId: notifyItem.SizeId,
|
|
|
|
TemplateId: notifyItem.TemplateId,
|
|
|
|
Source: "我是渲染资源",
|
|
|
|
}
|
|
|
|
b, _ := json.Marshal(responseData)
|
|
|
|
select {
|
|
|
|
case <-ws.closeChan:
|
|
|
|
return
|
|
|
|
case ws.outChan <- b:
|
|
|
|
logx.Info("notify send render result to out chan")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|