2024-10-14 13:49:16 +08:00

45 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package websocket
import (
"catface/app/global/consts"
"catface/app/global/variable"
controllerWs "catface/app/http/controller/websocket"
"catface/app/http/validator/core/data_transfer"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type Connect struct {
Token string `form:"token" json:"token" binding:"required,min=10"`
}
// 验证器语法,参见 Register.go文件有详细说明
// 注意websocket 连接建立之前如果有错误只能在服务端同构日志输出方式记录因为使用response.Fail等函数客户端是收不到任何信息的
func (c Connect) CheckParams(context *gin.Context) {
// 1. 首先检查是否开启websocket服务配置在配置项中开启
if variable.ConfigYml.GetInt("Websocket.Start") != 1 {
variable.ZapLog.Error(consts.WsServerNotStartMsg)
return
}
//2.基本的验证规则没有通过
if err := context.ShouldBind(&c); err != nil {
variable.ZapLog.Error("客户端上线参数不合格", zap.Error(err))
return
}
extraAddBindDataContext := data_transfer.DataAddContext(c, consts.ValidatorPrefix, context)
if extraAddBindDataContext == nil {
variable.ZapLog.Error("websocket-Connect 表单验证器json化失败")
context.Abort()
return
} else {
if serviceWs, ok := (&controllerWs.Ws{}).OnOpen(extraAddBindDataContext); ok == false {
variable.ZapLog.Error(consts.WsOpenFailMsg)
} else {
(&controllerWs.Ws{}).OnMessage(serviceWs, extraAddBindDataContext) // 注意这里传递的service_ws必须是调用open返回的必须保证的ws对象的一致性
}
}
}