app

package
v0.0.0-...-b62a5d4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2025 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateToken

func GenerateToken(uid int64, nickname string, ip string, expiry int64) (string, error)

GenerateToken generates a new JWT token for a user.

func GetAccessHost

func GetAccessHost(c *gin.Context) string

func GetIP

func GetIP(ctx *gin.Context) (out string)

GetIP extracts the user IP from the request context.

func GetPage

func GetPage(c *gin.Context) int

func GetPageOffset

func GetPageOffset(page, pageSize int) int

func GetPageSize

func GetPageSize(c *gin.Context) int

func GetRequestIP

func GetRequestIP(c *gin.Context) string

GetRequestIP 获取ip

func GetUID

func GetUID(ctx *gin.Context) (out int64)

GetUid extracts the user ID from the request context.

func RequestParamStrParse

func RequestParamStrParse(c *gin.Context, param any)

RequestParamStrParse 解析

func SetTokenToContext

func SetTokenToContext(ctx *gin.Context, tokenString string) error

SetTokenToContext set token to gin.Context

Types

type ConnStorage

type ConnStorage = map[*gws.Conn]*WebsocketClient

type Datetime

type Datetime time.Time

func (Datetime) MarshalJSON

func (t Datetime) MarshalJSON() ([]byte, error)

func (*Datetime) Scan

func (t *Datetime) Scan(v interface{}) error

func (*Datetime) String

func (t *Datetime) String() string

func (*Datetime) UnmarshalJSON

func (t *Datetime) UnmarshalJSON(data []byte) error

func (Datetime) Value

func (t Datetime) Value() (driver.Value, error)

type ListRes

type ListRes struct {
	// 数据清单
	List interface{} `json:"list"`
	// 翻页信息
	Pager Pager `json:"pager"`
}

type LogType

type LogType string
const (
	WebSocketServerPingInterval         = 25
	WebSocketServerPingWait             = 40
	LogInfo                     LogType = "info"
	LogError                    LogType = "error"
	LogWarn                     LogType = "warn"
)

type Pager

type Pager struct {
	// 页码
	Page int `json:"page"`
	// 每页数量
	PageSize int `json:"pageSize"`
	// 总行数
	TotalRows int `json:"totalRows"`
}

type ResDetailsResult

type ResDetailsResult struct {
	// 业务状态码
	Code int `json:"code"`
	// 状态
	Status bool `json:"status"`
	// 失败&&成功消息
	Msg interface{} `json:"message"`
	// 错误格式数据
	Data interface{} `json:"data"`
	// 错误支付
	Details interface{} `json:"details"`
}

type ResListResult

type ResListResult struct {
	// 业务状态码
	Code int `json:"code"`
	// 状态
	Status bool `json:"status"`
	// 失败&&成功消息
	Msg interface{} `json:"message"`
	// 数据集合
	Data ListRes `json:"data"`
}

type ResResult

type ResResult struct {
	// 业务状态码
	Code int `json:"code"`
	// 状态
	Status bool `json:"status"`
	// 失败&&成功消息
	Msg interface{} `json:"message"`
	// 数据集合
	Data interface{} `json:"data"`
}

type Response

type Response struct {
	Ctx *gin.Context
}

func NewResponse

func NewResponse(ctx *gin.Context) *Response

func (*Response) SendResultResponse

func (r *Response) SendResultResponse(statusCode int, content interface{})

func (*Response) ToResponse

func (r *Response) ToResponse(code *code.Code)

ToResponse 输出到浏览器

func (*Response) ToResponseList

func (r *Response) ToResponseList(code *code.Code, list interface{}, totalRows int)

type UserEntity

type UserEntity struct {
	UID      int64  `json:"uid"`
	Nickname string `json:"nickname"`
	IP       string `json:"ip"`
	jwt.RegisteredClaims
}

func ParseToken

func ParseToken(tokenString string) (*UserEntity, error)

ParseToken parses a JWT token and returns the user data.

type UserSelectEntity

type UserSelectEntity struct {
	UID      int64  `json:"uid"`
	Email    string `json:"email"`
	Nickname string `json:"nickname"`
	Avatar   string `json:"avatar"`
}

type ValidError

type ValidError struct {
	Key     string
	Message string
}

func (*ValidError) Error

func (v *ValidError) Error() string

func (*ValidError) Field

func (v *ValidError) Field() string

func (*ValidError) Map

func (v *ValidError) Map() map[string]string

type ValidErrors

type ValidErrors []*ValidError

func BindAndValid

func BindAndValid(c *gin.Context, obj interface{}) (bool, ValidErrors)

BindAndValid 绑定请求参数并进行验证,支持多语言

func (ValidErrors) Error

func (v ValidErrors) Error() string

func (ValidErrors) Errors

func (v ValidErrors) Errors() []string

func (ValidErrors) ErrorsToString

func (v ValidErrors) ErrorsToString() string

func (ValidErrors) Maps

func (v ValidErrors) Maps() []map[string]string

func (ValidErrors) MapsToString

func (v ValidErrors) MapsToString() string

type WebSocketMessage

type WebSocketMessage struct {
	Type string `json:"type"` // 操作类型,例如 "upload", "update", "delete"
	Data []byte `json:"data"` // 文件数据(仅在上传和更新时使用)
}

type WebsocketClient

type WebsocketClient struct {
	Ctx         *gin.Context
	User        *UserEntity
	UserClients *ConnStorage
	SF          *singleflight.Group // 用于处理并发请求的缓存
	// contains filtered or unexported fields
}

WebsocketClient 结构体来存储每个 WebSocket 连接及其相关状态

func (*WebsocketClient) BindAndValid

func (c *WebsocketClient) BindAndValid(data []byte, obj any) (bool, ValidErrors)

基于全局验证器的 WebSocket 版本参数绑定和验证工具函数

func (*WebsocketClient) BroadcastResponse

func (c *WebsocketClient) BroadcastResponse(code *code.Code, options ...any)

BroadcastResponse 将结果转换为 JSON 格式并广播给所有客户端 第二个options参数为是否排除自己 第三个options参数为动作类型

func (*WebsocketClient) PingLoop

func (c *WebsocketClient) PingLoop(PingInterval time.Duration)

定期发送 Ping 消息

func (*WebsocketClient) ToResponse

func (c *WebsocketClient) ToResponse(code *code.Code, action ...string)

ToResponse 将结果转换为 JSON 格式并发送给客户端

type WebsocketServer

type WebsocketServer struct {
	// contains filtered or unexported fields
}

func NewWebsocketServer

func NewWebsocketServer(c WebsocketServerConfig) *WebsocketServer

func (*WebsocketServer) AddClient

func (w *WebsocketServer) AddClient(c *WebsocketClient)

func (*WebsocketServer) AddUserClient

func (w *WebsocketServer) AddUserClient(c *WebsocketClient)

func (*WebsocketServer) Authorization

func (w *WebsocketServer) Authorization(c *WebsocketClient, msg *WebSocketMessage)

func (*WebsocketServer) GetClient

func (w *WebsocketServer) GetClient(conn *gws.Conn) *WebsocketClient

func (*WebsocketServer) OnClose

func (w *WebsocketServer) OnClose(conn *gws.Conn, err error)

func (*WebsocketServer) OnMessage

func (w *WebsocketServer) OnMessage(conn *gws.Conn, message *gws.Message)

func (*WebsocketServer) OnOpen

func (w *WebsocketServer) OnOpen(conn *gws.Conn)

func (*WebsocketServer) OnPing

func (w *WebsocketServer) OnPing(socket *gws.Conn, payload []byte)

func (*WebsocketServer) OnPong

func (w *WebsocketServer) OnPong(socket *gws.Conn, payload []byte)

func (*WebsocketServer) RemoveClient

func (w *WebsocketServer) RemoveClient(conn *gws.Conn)

func (*WebsocketServer) RemoveUserClient

func (w *WebsocketServer) RemoveUserClient(c *WebsocketClient)

func (*WebsocketServer) Run

func (w *WebsocketServer) Run() gin.HandlerFunc

func (*WebsocketServer) Upgrade

func (w *WebsocketServer) Upgrade()

func (*WebsocketServer) Use

func (w *WebsocketServer) Use(action string, handler func(*WebsocketClient, *WebSocketMessage))

func (*WebsocketServer) UserDataSelectUse

func (w *WebsocketServer) UserDataSelectUse(handler func(*WebsocketClient, int64) (*UserSelectEntity, error))

type WebsocketServerConfig

type WebsocketServerConfig struct {
	GWSOption    gws.ServerOption
	PingInterval time.Duration
	PingWait     time.Duration
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL