fix
This commit is contained in:
35
server/websocket/internal/handler/loginnotifyhandler.go
Normal file
35
server/websocket/internal/handler/loginnotifyhandler.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/websocket/internal/logic"
|
||||
"fusenapi/server/websocket/internal/svc"
|
||||
"fusenapi/server/websocket/internal/types"
|
||||
)
|
||||
|
||||
func LoginNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var req types.LoginNotifyReq
|
||||
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewLoginNotifyLogic(r.Context(), svcCtx)
|
||||
|
||||
rl := reflect.ValueOf(l)
|
||||
basic.BeforeLogic(w, r, rl)
|
||||
|
||||
resp := l.LoginNotify(&req, userinfo)
|
||||
|
||||
if !basic.AfterLogic(w, r, rl, resp) {
|
||||
basic.NormalAfterLogic(w, r, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/websocket/internal/logic"
|
||||
"fusenapi/server/websocket/internal/svc"
|
||||
"fusenapi/server/websocket/internal/types"
|
||||
)
|
||||
|
||||
func RegisterAccountNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var req types.RegisterAccountNotifyReq
|
||||
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewRegisterAccountNotifyLogic(r.Context(), svcCtx)
|
||||
|
||||
rl := reflect.ValueOf(l)
|
||||
basic.BeforeLogic(w, r, rl)
|
||||
|
||||
resp := l.RegisterAccountNotify(&req, userinfo)
|
||||
|
||||
if !basic.AfterLogic(w, r, rl, resp) {
|
||||
basic.NormalAfterLogic(w, r, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
Path: "/api/websocket/render_notify",
|
||||
Handler: RenderNotifyHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/websocket/register_account_notify",
|
||||
Handler: RegisterAccountNotifyHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/websocket/login_notify",
|
||||
Handler: LoginNotifyHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package logic
|
||||
|
||||
//websocket连接
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
@@ -323,7 +324,7 @@ func (w *wsConnectItem) sendToOutChan(data []byte) {
|
||||
}
|
||||
|
||||
// 格式化为websocket标准返回格式
|
||||
func (w *wsConnectItem) respondDataFormat(msgType string, data interface{}) []byte {
|
||||
func (w *wsConnectItem) respondDataFormat(msgType constants.Websocket, data interface{}) []byte {
|
||||
d := websocket_data.DataTransferData{
|
||||
T: msgType,
|
||||
D: data,
|
||||
|
||||
56
server/websocket/internal/logic/loginnotifylogic.go
Normal file
56
server/websocket/internal/logic/loginnotifylogic.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package logic
|
||||
|
||||
//登录回调
|
||||
import (
|
||||
"fusenapi/constants"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/websocket/internal/svc"
|
||||
"fusenapi/server/websocket/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type LoginNotifyLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewLoginNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginNotifyLogic {
|
||||
return &LoginNotifyLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *LoginNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
func (l *LoginNotifyLogic) LoginNotify(req *types.LoginNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
if req.Data.WebsocketConnId == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,连接标识不能为空")
|
||||
}
|
||||
value, ok := mapConnPool.Load(req.Data.WebsocketConnId)
|
||||
if !ok {
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success,but connection is not found")
|
||||
}
|
||||
ws, ok := value.(wsConnectItem)
|
||||
if !ok {
|
||||
logx.Error("渲染回调断言websocket连接失败")
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "断言连接错误")
|
||||
}
|
||||
//发送消息到出口缓冲池
|
||||
ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_LOGIN_NOTIFY, req.Data.Info))
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
||||
}
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *LoginNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
@@ -0,0 +1,56 @@
|
||||
package logic
|
||||
|
||||
//注册帐号回调
|
||||
import (
|
||||
"fusenapi/constants"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/websocket/internal/svc"
|
||||
"fusenapi/server/websocket/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type RegisterAccountNotifyLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewRegisterAccountNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterAccountNotifyLogic {
|
||||
return &RegisterAccountNotifyLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *RegisterAccountNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
func (l *RegisterAccountNotifyLogic) RegisterAccountNotify(req *types.RegisterAccountNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
if req.Data.WebsocketConnId == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,连接标识不能为空")
|
||||
}
|
||||
value, ok := mapConnPool.Load(req.Data.WebsocketConnId)
|
||||
if !ok {
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success,but connection is not found")
|
||||
}
|
||||
ws, ok := value.(wsConnectItem)
|
||||
if !ok {
|
||||
logx.Error("渲染回调断言websocket连接失败")
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "断言连接错误")
|
||||
}
|
||||
//发送消息到出口缓冲池
|
||||
ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_REGISTER_NOTIFY, req.Data.Info))
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
||||
}
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *RegisterAccountNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
@@ -1,5 +1,6 @@
|
||||
package logic
|
||||
|
||||
//云渲染回调
|
||||
import (
|
||||
"context"
|
||||
"fusenapi/server/websocket/internal/svc"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package logic
|
||||
|
||||
//处理websocket云渲染任务数据
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package logic
|
||||
|
||||
//复用websocket连接标识
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
@@ -12,6 +12,28 @@ type RenderNotifyReq struct {
|
||||
Image string `json:"image"`
|
||||
}
|
||||
|
||||
type RegisterAccountNotifyReq struct {
|
||||
Data RegisterAccountData `json:"data"`
|
||||
Time int64 `json:"time,optional"` //utc时间戳(用于验证签名)
|
||||
Sign string `json:"sign,optional"` //签名
|
||||
}
|
||||
|
||||
type RegisterAccountData struct {
|
||||
WebsocketConnId string `json:"websocket_conn_id"` //连接标识
|
||||
Info map[string]interface{} `json:"info"`
|
||||
}
|
||||
|
||||
type LoginNotifyReq struct {
|
||||
Data LoginNotifyData `json:"data"`
|
||||
Time int64 `json:"time,optional"` //utc时间戳(用于验证签名)
|
||||
Sign string `json:"sign,optional"` //签名
|
||||
}
|
||||
|
||||
type LoginNotifyData struct {
|
||||
WebsocketConnId string `json:"websocket_conn_id"` //连接标识
|
||||
Info map[string]interface{} `json:"info"`
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
}
|
||||
|
||||
@@ -79,4 +101,4 @@ func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string,
|
||||
newResp.Data = data[0]
|
||||
}
|
||||
return newResp
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user