server

package
v0.0.0-...-23f8949 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package server 提供连接事件处理器

Package server 提供 Gateway 服务器配置选项

Package server 提供基于 gnet 的高性能 WebSocket 服务器

Index

Constants

View Source
const ErrorCmd = 900

ErrorCmd 错误命令类型

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

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

Handler 连接事件处理器

func NewHandler

func NewHandler(server *Server) *Handler

NewHandler 创建处理器

func (*Handler) AuthMiddleware

func (h *Handler) AuthMiddleware(next protocol.HandlerFunc) protocol.HandlerFunc

AuthMiddleware 认证中间件

func (*Handler) HandleDeliveryAck

func (h *Handler) HandleDeliveryAck(ctx context.Context, connID string, packet *protocol.Packet) (*protocol.Packet, error)

HandleDeliveryAck 处理送达确认

func (*Handler) HandleDisconnect

func (h *Handler) HandleDisconnect(ctx context.Context, connID string, packet *protocol.Packet) (*protocol.Packet, error)

HandleDisconnect 处理断开连接请求

func (*Handler) HandleHandshake

func (h *Handler) HandleHandshake(ctx context.Context, connID string, packet *protocol.Packet) (*protocol.Packet, error)

HandleHandshake 处理握手请求

func (*Handler) HandleHeartbeat

func (h *Handler) HandleHeartbeat(ctx context.Context, connID string, packet *protocol.Packet) (*protocol.Packet, error)

HandleHeartbeat 处理心跳请求

func (*Handler) HandleReadReceipt

func (h *Handler) HandleReadReceipt(ctx context.Context, connID string, packet *protocol.Packet) (*protocol.Packet, error)

HandleReadReceipt 处理已读回执

func (*Handler) HandleRecallMessage

func (h *Handler) HandleRecallMessage(ctx context.Context, connID string, packet *protocol.Packet) (*protocol.Packet, error)

HandleRecallMessage 处理撤回消息请求

func (*Handler) HandleSendMessage

func (h *Handler) HandleSendMessage(ctx context.Context, connID string, packet *protocol.Packet) (*protocol.Packet, error)

HandleSendMessage 处理发送消息请求

func (*Handler) HandleSync

func (h *Handler) HandleSync(ctx context.Context, connID string, packet *protocol.Packet) (*protocol.Packet, error)

HandleSync 处理同步请求

func (*Handler) RegisterHandlers

func (h *Handler) RegisterHandlers()

RegisterHandlers 注册所有命令处理器

type Option

type Option func(*Options)

Option 配置选项函数类型

func WithAddr

func WithAddr(addr string) Option

WithAddr 设置监听地址

func WithBucketNum

func WithBucketNum(num int) Option

WithBucketNum 设置连接分桶数量

func WithBufferSize

func WithBufferSize(read, write int) Option

WithBufferSize 设置读写缓冲区大小

func WithGatewayInfo

func WithGatewayInfo(id, addr string) Option

WithGatewayInfo 设置网关信息

func WithHeartbeat

func WithHeartbeat(interval, timeout time.Duration) Option

WithHeartbeat 设置心跳配置

func WithHeartbeatInterval

func WithHeartbeatInterval(interval time.Duration) Option

WithHeartbeatInterval 设置心跳间隔

func WithIdleTimeout

func WithIdleTimeout(timeout time.Duration) Option

WithIdleTimeout 设置空闲超时

func WithMaxConnections

func WithMaxConnections(max int) Option

WithMaxConnections 设置最大连接数

func WithReadBufferSize

func WithReadBufferSize(size int) Option

WithReadBufferSize 设置读缓冲区大小

func WithTLS

func WithTLS(certFile, keyFile string) Option

WithTLS 设置 TLS 配置

func WithWSPath

func WithWSPath(path string) Option

WithWSPath 设置 WebSocket 路径

func WithWorkerPoolSize

func WithWorkerPoolSize(size int) Option

WithWorkerPoolSize 设置工作池大小

func WithWriteBufferSize

func WithWriteBufferSize(size int) Option

WithWriteBufferSize 设置写缓冲区大小

type Options

type Options struct {
	// 网络配置
	Addr            string // 监听地址,格式 "host:port"
	MaxConnections  int    // 最大连接数
	ReadBufferSize  int    // 读缓冲区大小
	WriteBufferSize int    // 写缓冲区大小

	// WebSocket 配置
	WSPath   string // WebSocket 路径,默认 "/ws"
	EnableWS bool   // 是否启用 WebSocket

	// 超时配置
	ReadTimeout  time.Duration // 读超时
	WriteTimeout time.Duration // 写超时
	IdleTimeout  time.Duration // 空闲超时(心跳超时)

	// 心跳配置
	HeartbeatInterval time.Duration // 心跳间隔
	HeartbeatTimeout  time.Duration // 心跳超时(未收到心跳多久后断开)

	// 连接分桶配置
	BucketNum int // 连接分桶数量(减少锁竞争)

	// 工作池配置
	WorkerPoolSize int // 工作协程池大小

	// 服务发现
	GatewayID   string // 网关实例ID
	GatewayAddr string // 网关外部地址(用于服务发现)

	// TLS 配置
	EnableTLS bool   // 是否启用 TLS
	CertFile  string // 证书文件路径
	KeyFile   string // 私钥文件路径
}

Options Gateway 服务器配置选项

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions 返回默认配置

func (*Options) Validate

func (o *Options) Validate() error

Validate 验证配置有效性

type Server

type Server struct {
	gnet.BuiltinEventEngine
	// contains filtered or unexported fields
}

Server Gateway 服务器

func NewServer

func NewServer(opts ...Option) *Server

NewServer 创建新的 Gateway 服务器

func (*Server) ConnManager

func (s *Server) ConnManager() *connection.Manager

ConnManager 返回连接管理器

func (*Server) MessageClient

func (s *Server) MessageClient() message.MessageServiceClient

MessageClient 返回 Message Service 客户端

func (*Server) OnBoot

func (s *Server) OnBoot(eng gnet.Engine) gnet.Action

OnBoot 服务器启动回调

func (*Server) OnClose

func (s *Server) OnClose(c gnet.Conn, err error) (action gnet.Action)

OnClose 连接关闭回调

func (*Server) OnOpen

func (s *Server) OnOpen(c gnet.Conn) (out []byte, action gnet.Action)

OnOpen 新连接建立回调

func (*Server) OnShutdown

func (s *Server) OnShutdown(eng gnet.Engine)

OnShutdown 服务器关闭回调

func (*Server) OnTick

func (s *Server) OnTick() (delay time.Duration, action gnet.Action)

OnTick 定时器回调

func (*Server) OnTraffic

func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action)

OnTraffic 数据到达回调

func (*Server) RedisClient

func (s *Server) RedisClient() interface{}

RedisClient 返回 Redis 客户端

func (*Server) Router

func (s *Server) Router() *protocol.Router

Router 返回命令路由器

func (*Server) SetMessageClient

func (s *Server) SetMessageClient(client message.MessageServiceClient)

SetMessageClient 设置 Message Service 客户端

func (*Server) SetRedisClient

func (s *Server) SetRedisClient(client interface{})

SetRedisClient 设置 Redis 客户端

func (*Server) Start

func (s *Server) Start() error

Start 启动服务器

func (*Server) Stats

func (s *Server) Stats() map[string]interface{}

Stats 返回服务器统计信息

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) error

Stop 停止服务器

Jump to

Keyboard shortcuts

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