This commit is contained in:
laodaming 2023-08-24 15:45:23 +08:00
parent d003844efd
commit dcdc949a90
10 changed files with 77 additions and 243 deletions

View File

@ -20,8 +20,6 @@ const (
WEBSOCKET_RENDER_IMAGE_ERR Websocket = "WEBSOCKET_RENDER_IMAGE_ERR"
//传入数据格式错误
WEBSOCKET_ERR_DATA_FORMAT Websocket = "WEBSOCKET_ERR_DATA_FORMAT"
//登录回调通知
WEBSOCKET_LOGIN_NOTIFY Websocket = "WEBSOCKET_LOGIN_NOTIFY"
//注册帐号回调通知
WEBSOCKET_REGISTER_NOTIFY Websocket = "WEBSOCKET_REGISTER_NOTIFY"
//通用回调通知
WEBSOCKET_COMMON_NOTIFY Websocket = "WEBSOCKET_COMMON_NOTIFY"
)

View File

@ -11,22 +11,22 @@ import (
"fusenapi/server/websocket/internal/types"
)
func LoginNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
func CommonNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.LoginNotifyReq
var req types.CommonNotifyReq
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
return
}
// 创建一个业务逻辑层实例
l := logic.NewLoginNotifyLogic(r.Context(), svcCtx)
l := logic.NewCommonNotifyLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.LoginNotify(&req, userinfo)
resp := l.CommonNotify(&req, userinfo)
if !basic.AfterLogic(w, r, rl, resp) {
basic.NormalAfterLogic(w, r, resp)

View File

@ -1,35 +0,0 @@
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)
}
}
}

View File

@ -24,13 +24,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
},
{
Method: http.MethodPost,
Path: "/api/websocket/register_account_notify",
Handler: RegisterAccountNotifyHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/api/websocket/login_notify",
Handler: LoginNotifyHandler(serverCtx),
Path: "/api/websocket/common_notify",
Handler: CommonNotifyHandler(serverCtx),
},
},
)

View File

@ -0,0 +1,59 @@
package logic
import (
"context"
"fusenapi/constants"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/server/websocket/internal/svc"
"fusenapi/server/websocket/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type CommonNotifyLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCommonNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommonNotifyLogic {
return &CommonNotifyLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *CommonNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *CommonNotifyLogic) CommonNotify(req *types.CommonNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if req.Data == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "data is empty")
}
//websocket连接id不能为空
if req.WebsocketId == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "websocket connect id is empty")
}
//查询websocket连接
value, ok := mapConnPool.Load(req.WebsocketId)
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_COMMON_NOTIFY, req.Data))
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *CommonNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }

View File

@ -1,83 +0,0 @@
package logic
//登录回调
import (
"encoding/json"
"fusenapi/constants"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/encryption_decryption"
"fusenapi/utils/websocket_data"
"time"
"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 == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "data is empty")
}
//解密数据
data, err := encryption_decryption.CBCDecrypt(req.Data)
if err != nil {
logx.Error("解密失败:", err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid data ")
}
//真正的数据结构
var parseInfo websocket_data.NotifyData
if err = json.Unmarshal([]byte(data), &parseInfo); err != nil {
logx.Error("failed to parse json data:", err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid format of parse data")
}
//websocket连接id不能为空
if parseInfo.WebsocketConnectId == "" {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "websocket connect id is empty")
}
now := time.Now().UTC().Unix()
//请求时间前后20秒都会失效
if parseInfo.RequestTime < now-20 || parseInfo.RequestTime > now+20 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid data ,time is not in allowed range")
}
//查询websocket连接
value, ok := mapConnPool.Load(parseInfo.WebsocketConnectId)
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, parseInfo.Data))
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)
// }

View File

@ -1,83 +0,0 @@
package logic
//注册帐号回调
import (
"encoding/json"
"fusenapi/constants"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/encryption_decryption"
"fusenapi/utils/websocket_data"
"time"
"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 == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "data is empty")
}
//解密数据
data, err := encryption_decryption.CBCDecrypt(req.Data)
if err != nil {
logx.Error("解密失败:", err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid data ")
}
//真正的数据结构
var parseInfo websocket_data.NotifyData
if err = json.Unmarshal([]byte(data), &parseInfo); err != nil {
logx.Error("failed to parse json data:", err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid format of parse data")
}
//websocket连接id不能为空
if parseInfo.WebsocketConnectId == "" {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "websocket connect id is empty")
}
now := time.Now().UTC().Unix()
//请求时间前后20秒都会失效
if parseInfo.RequestTime < now-20 || parseInfo.RequestTime > now+20 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid data ,time is not in allowed range")
}
//查询websocket连接
value, ok := mapConnPool.Load(parseInfo.WebsocketConnectId)
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, parseInfo.Data))
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)
// }

View File

@ -12,12 +12,9 @@ type RenderNotifyReq struct {
Image string `json:"image"`
}
type RegisterAccountNotifyReq struct {
Data string `json:"data"` //aes_cbc加密密文
}
type LoginNotifyReq struct {
Data string `json:"data"` //aes_cbc加密密文
type CommonNotifyReq struct {
WebsocketId string `json:"websocket_id"` //websocket连接标识
Data string `json:"data"` //aes_cbc加密密文
}
type Request struct {

View File

@ -15,12 +15,9 @@ service websocket {
//云渲染完了通知接口
@handler RenderNotifyHandler
post /api/websocket/render_notify(RenderNotifyReq) returns (response);
//注册回调
@handler RegisterAccountNotifyHandler
post /api/websocket/register_account_notify(RegisterAccountNotifyReq) returns (response);
//登录回调
@handler LoginNotifyHandler
post /api/websocket/login_notify(LoginNotifyReq) returns (response);
//通用回调接口
@handler CommonNotifyHandler
post /api/websocket/common_notify(CommonNotifyReq) returns (response);
}
//渲染完了通知接口
@ -30,11 +27,8 @@ type RenderNotifyReq {
GuestId int64 `json:"guest_id"`
Image string `json:"image"`
}
//注册回调
type RegisterAccountNotifyReq {
Data string `json:"data"` //aes_cbc加密密文
}
//登录回调
type LoginNotifyReq {
Data string `json:"data"` //aes_cbc加密密文
//通用回调接口
type CommonNotifyReq {
WebsocketId string `json:"websocket_id"` //websocket连接标识
Data string `json:"data"` //aes_cbc加密密文
}

View File

@ -1,8 +0,0 @@
package websocket_data
// 请求回调接口数据(登录|注册)
type NotifyData struct {
WebsocketConnectId string `json:"websocket_connect_id"` //websocket连接唯一标识
RequestTime int64 `json:"request_time"` //请求回调时的utc时间
Data interface{} `json:"data"` //其他数据
}