Documentation ¶
Overview ¶
Package server 提供了包含多种网络类型的服务器实现
Index ¶
- Constants
- Variables
- func BindData(srv *Server, name string, data any)
- func BindService(srv *Server, services ...Service)
- func BindServiceToMultipleServer(server *MultipleServer, services ...MultipleService)
- func DefaultWebsocketUpgrader() *websocket.Upgrader
- func HasMessageType(mt MessageType) bool
- func LoadData[T any](srv *Server, name string) T
- type Bot
- type BotOption
- type Conn
- func (slf *Conn) Close(err ...error)
- func (slf *Conn) GetData(key any) any
- func (slf *Conn) GetID() string
- func (slf *Conn) GetIP() string
- func (slf *Conn) GetMessageData(key any) any
- func (slf *Conn) GetOnlineTime() time.Duration
- func (slf *Conn) GetOpenTime() time.Time
- func (slf *Conn) GetServer() *Server
- func (slf *Conn) GetWST() int
- func (slf *Conn) GetWebsocketRequest() *http.Request
- func (slf *Conn) IsBot() bool
- func (slf *Conn) IsClosed() bool
- func (slf *Conn) IsWebsocket() bool
- func (slf *Conn) PushAsyncMessage(caller func() error, callback func(err error), mark ...log.Field)
- func (slf *Conn) PushUniqueAsyncMessage(name string, caller func() error, callback func(err error), mark ...log.Field)
- func (slf *Conn) ReleaseData() *Conn
- func (slf *Conn) RemoteAddr() net.Addr
- func (slf *Conn) SetData(key, value any) *Conn
- func (slf *Conn) SetMessageData(key, value any) *Conn
- func (slf *Conn) SetWST(wst int) *Conn
- func (slf *Conn) Ticker() *timer.Ticker
- func (slf *Conn) ViewData() map[any]any
- func (slf *Conn) Write(packet []byte, callback ...func(err error))
- type ConnectionClosedEventHandler
- type ConnectionOpenedAfterEventHandler
- type ConnectionOpenedEventHandler
- type ConnectionPacketPreprocessEventHandler
- type ConnectionReceivePacketEventHandler
- type ConnectionWritePacketBeforeEventHandler
- type ConsoleCommandEventHandler
- type ConsoleParams
- func (slf ConsoleParams) Add(key, value string)
- func (slf ConsoleParams) Clear()
- func (slf ConsoleParams) Del(key string)
- func (slf ConsoleParams) Get(key string) string
- func (slf ConsoleParams) GetValueNum(key string) int
- func (slf ConsoleParams) GetValues(key string) []string
- func (slf ConsoleParams) Has(key string) bool
- type ContextPacker
- type HandlerFunc
- type Http
- type HttpContext
- type HttpRouter
- func (slf *HttpRouter[Context]) Any(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) CONNECT(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) DELETE(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) GET(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) Group(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) HEAD(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) Handle(httpMethod, relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) Match(methods []string, relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) OPTIONS(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) PATCH(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) POST(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) PUT(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) Static(relativePath, root string) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) StaticFS(relativePath string, fs http.FileSystem) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) StaticFile(relativePath, filepath string) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) StaticFileFS(relativePath, filepath string, fs http.FileSystem) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) TRACE(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
- func (slf *HttpRouter[Context]) Use(middleware ...HandlerFunc[Context]) *HttpRouter[Context]
- type HttpWrapper
- func (slf *HttpWrapper[CTX]) Any(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
- func (slf *HttpWrapper[CTX]) Connect(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
- func (slf *HttpWrapper[CTX]) DELETE(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
- func (slf *HttpWrapper[CTX]) GET(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
- func (slf *HttpWrapper[CTX]) Group(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
- func (slf *HttpWrapper[CTX]) HEAD(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
- func (slf *HttpWrapper[CTX]) Handle(httpMethod, relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
- func (slf *HttpWrapper[CTX]) Match(methods []string, relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
- func (slf *HttpWrapper[CTX]) OPTIONS(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
- func (slf *HttpWrapper[CTX]) PATCH(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
- func (slf *HttpWrapper[CTX]) POST(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
- func (slf *HttpWrapper[CTX]) PUT(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
- func (slf *HttpWrapper[CTX]) Static(relativePath, root string) *HttpWrapper[CTX]
- func (slf *HttpWrapper[CTX]) StaticFS(relativePath string, fs http.FileSystem) *HttpWrapper[CTX]
- func (slf *HttpWrapper[CTX]) StaticFile(relativePath, filepath string) *HttpWrapper[CTX]
- func (slf *HttpWrapper[CTX]) Trace(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
- func (slf *HttpWrapper[CTX]) Use(middleware ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
- type HttpWrapperGroup
- func (slf *HttpWrapperGroup[CTX]) DELETE(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
- func (slf *HttpWrapperGroup[CTX]) GET(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
- func (slf *HttpWrapperGroup[CTX]) Group(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
- func (slf *HttpWrapperGroup[CTX]) Handle(httpMethod, relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
- func (slf *HttpWrapperGroup[CTX]) OPTIONS(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
- func (slf *HttpWrapperGroup[CTX]) PATCH(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
- func (slf *HttpWrapperGroup[CTX]) POST(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
- func (slf *HttpWrapperGroup[CTX]) PUT(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
- func (slf *HttpWrapperGroup[CTX]) Use(middleware ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
- type HttpWrapperHandleFunc
- type Message
- type MessageErrorEventHandler
- type MessageExecBeforeEventHandler
- type MessageLowExecEventHandler
- type MessageReadyEventHandler
- type MessageType
- type MultipleServer
- type MultipleService
- type Network
- type OnDeadlockDetectEventHandler
- type Option
- func WithAsyncLowMessageDuration(duration time.Duration) Option
- func WithAsyncPoolSize(size int) Option
- func WithConnWriteBufferSize(size int) Option
- func WithDeadlockDetect(t time.Duration) Option
- func WithDisableAsyncMessage() Option
- func WithDispatcherBufferSize(size int) Option
- func WithGRPCServerOptions(options ...grpc.ServerOption) Option
- func WithLimitLife(t time.Duration) Option
- func WithLowMessageDuration(duration time.Duration) Option
- func WithMessageStatistics(duration time.Duration, limit int) Option
- func WithPProf(pattern ...string) Option
- func WithPacketWarnSize(size int) Option
- func WithTLS(certFile, keyFile string) Option
- func WithTicker(poolSize, size, connSize int, autonomy bool) Option
- func WithWebsocketCompression(level int) Option
- func WithWebsocketConnInitializer(...) Option
- func WithWebsocketMessageType(messageTypes ...int) Option
- func WithWebsocketReadDeadline(t time.Duration) Option
- func WithWebsocketUpgrade(upgrader *websocket.Upgrader) Option
- func WithWebsocketWriteCompression() Option
- type Server
- func (srv *Server) BindData(name string, data any)
- func (h Server) Broadcast(packet []byte, filter ...func(conn *Conn) bool)
- func (h Server) CloseConn(id string)
- func (srv *Server) Context() context.Context
- func (srv *Server) GRPCServer() *grpc.Server
- func (srv *Server) GetAllDurationMessageCount() []int64
- func (srv *Server) GetConnCurrShunt(conn *Conn) string
- func (srv *Server) GetDurationMessageCount() int64
- func (srv *Server) GetDurationMessageCountByOffset(offset int) int64
- func (srv *Server) GetMessageCount() int64
- func (srv *Server) GetNetwork() Network
- func (h Server) GetOnline(id string) *Conn
- func (h Server) GetOnlineAll() map[string]*Conn
- func (h Server) GetOnlineBotCount() int
- func (h Server) GetOnlineCount() int
- func (srv *Server) GetShuntNum() int
- func (srv *Server) HasMessageStatistics() bool
- func (srv *Server) HasShunt(name string) bool
- func (srv *Server) HttpRouter() gin.IRouterdeprecated
- func (srv *Server) HttpServer() *Http[*HttpContext]
- func (h Server) IsOnline(id string) bool
- func (srv *Server) IsSocket() bool
- func (srv *Server) LoadData(name string, data any) any
- func (slf Server) OnConnectionClosedEvent(conn *Conn, err any)
- func (slf Server) OnConnectionOpenedAfterEvent(conn *Conn)
- func (slf Server) OnConnectionOpenedEvent(conn *Conn)
- func (slf Server) OnConnectionPacketPreprocessEvent(conn *Conn, packet []byte, usePacket func(newPacket []byte)) bool
- func (slf Server) OnConnectionReceivePacketEvent(conn *Conn, packet []byte)
- func (slf Server) OnConnectionWritePacketBeforeEvent(conn *Conn, packet []byte) (newPacket []byte)
- func (slf Server) OnConsoleCommandEvent(command string, paramsStr string)
- func (slf Server) OnDeadlockDetectEvent(message *Message)
- func (slf Server) OnMessageErrorEvent(message *Message, err error)
- func (slf Server) OnMessageExecBeforeEvent(message *Message) bool
- func (slf Server) OnMessageLowExecEvent(message *Message, cost time.Duration)
- func (slf Server) OnMessageReadyEvent()
- func (slf Server) OnShuntChannelClosedEvent(name string)
- func (slf Server) OnShuntChannelCreatedEvent(name string)
- func (slf Server) OnStartBeforeEvent()
- func (slf Server) OnStartFinishEvent()
- func (slf Server) OnStopEvent()
- func (srv *Server) PushAsyncMessage(caller func() error, callback func(err error), mark ...log.Field)
- func (srv *Server) PushPacketMessage(conn *Conn, wst int, packet []byte, mark ...log.Field)
- func (srv *Server) PushShuntAsyncMessage(conn *Conn, caller func() error, callback func(err error), mark ...log.Field)
- func (srv *Server) PushShuntMessage(conn *Conn, caller func(), mark ...log.Field)
- func (srv *Server) PushShuntTickerMessage(conn *Conn, name string, caller func(), mark ...log.Field)
- func (srv *Server) PushSystemMessage(handler func(), mark ...log.Field)
- func (srv *Server) PushTickerMessage(name string, caller func(), mark ...log.Field)
- func (srv *Server) PushUniqueAsyncMessage(unique string, caller func() error, callback func(err error), ...)
- func (srv *Server) PushUniqueShuntAsyncMessage(conn *Conn, unique string, caller func() error, callback func(err error), ...)
- func (slf Server) RegConnectionClosedEvent(handler ConnectionClosedEventHandler, priority ...int)
- func (slf Server) RegConnectionOpenedAfterEvent(handler ConnectionOpenedAfterEventHandler, priority ...int)
- func (slf Server) RegConnectionOpenedEvent(handler ConnectionOpenedEventHandler, priority ...int)
- func (slf Server) RegConnectionPacketPreprocessEvent(handler ConnectionPacketPreprocessEventHandler, priority ...int)
- func (slf Server) RegConnectionReceivePacketEvent(handler ConnectionReceivePacketEventHandler, priority ...int)
- func (slf Server) RegConnectionWritePacketBeforeEvent(handler ConnectionWritePacketBeforeEventHandler, priority ...int)
- func (slf Server) RegConsoleCommandEvent(command string, handler ConsoleCommandEventHandler, priority ...int)
- func (slf Server) RegDeadlockDetectEvent(handler OnDeadlockDetectEventHandler, priority ...int)
- func (slf Server) RegMessageErrorEvent(handler MessageErrorEventHandler, priority ...int)
- func (slf Server) RegMessageExecBeforeEvent(handler MessageExecBeforeEventHandler, priority ...int)
- func (slf Server) RegMessageLowExecEvent(handler MessageLowExecEventHandler, priority ...int)
- func (slf Server) RegMessageReadyEvent(handler MessageReadyEventHandler, priority ...int)
- func (slf Server) RegShuntChannelCloseEvent(handler ShuntChannelClosedEventHandler, priority ...int)
- func (slf Server) RegShuntChannelCreatedEvent(handler ShuntChannelCreatedEventHandler, priority ...int)
- func (slf Server) RegStartBeforeEvent(handler StartBeforeEventHandler, priority ...int)
- func (slf Server) RegStartFinishEvent(handler StartFinishEventHandler, priority ...int)
- func (slf Server) RegStopEvent(handler StopEventHandler, priority ...int)
- func (srv *Server) Run(addr string) (err error)
- func (srv *Server) RunNone() error
- func (srv *Server) Shutdown()
- func (srv *Server) Ticker() *timer.Ticker
- func (srv *Server) TimeoutContext(timeout time.Duration) (context.Context, context.CancelFunc)
- func (srv *Server) UseShunt(conn *Conn, name string)
- type Service
- type ShuntChannelClosedEventHandler
- type ShuntChannelCreatedEventHandler
- type StartBeforeEventHandler
- type StartFinishEventHandler
- type StopEventHandler
Examples ¶
Constants ¶
const ( DefaultAsyncPoolSize = 256 DefaultWebsocketReadDeadline = 30 * time.Second DefaultPacketWarnSize = 1024 * 1024 * 1 // 1MB DefaultDispatcherBufferSize = 1024 * 16 DefaultConnWriteBufferSize = 1024 * 1 DefaultConnHubBufferSize = 1024 * 1 DefaultLowMessageDuration = 100 * time.Millisecond DefaultAsyncLowMessageDuration = time.Second )
const ( // WebsocketMessageTypeText 表示文本数据消息。文本消息负载被解释为 UTF-8 编码的文本数据 WebsocketMessageTypeText = websocket.TextMessage // WebsocketMessageTypeBinary 表示二进制数据消息 WebsocketMessageTypeBinary = websocket.BinaryMessage // WebsocketMessageTypeClose 表示关闭控制消息。可选消息负载包含数字代码和文本。使用 FormatCloseMessage 函数来格式化关闭消息负载 WebsocketMessageTypeClose = websocket.CloseMessage // WebsocketMessageTypePing 表示 ping 控制消息。可选的消息负载是 UTF-8 编码的文本 WebsocketMessageTypePing = websocket.PingMessage // WebsocketMessageTypePong 表示一个 pong 控制消息。可选的消息负载是 UTF-8 编码的文本 WebsocketMessageTypePong = websocket.PongMessage )
Variables ¶
var ( ErrConstructed = errors.New("the Server must be constructed using the server.New function") ErrCanNotSupportNetwork = errors.New("can not support network") ErrNetworkOnlySupportHttp = errors.New("the current network mode is not compatible with HttpRouter, only NetworkHttp is supported") ErrNetworkOnlySupportGRPC = errors.New("the current network mode is not compatible with RegGrpcServer, only NetworkGRPC is supported") ErrNetworkIncompatibleHttp = errors.New("the current network mode is not compatible with NetworkHttp") ErrWebsocketIllegalMessageType = errors.New("illegal message type") ErrNoSupportTicker = errors.New("the server does not support Ticker, please use the WithTicker option to create the server") )
Functions ¶
func BindService ¶ added in v0.4.2
BindService 绑定服务到特定 Server,被绑定的服务将会在 Server 初始化时执行 Service.OnInit 方法
Example ¶
这个案例中我们将 `TestService` 绑定到了 `srv` 服务器中,当服务器启动时,将会对 `TestService` 进行初始化
其中 `TestService` 的定义如下: ```go
type TestService struct{} func (ts *TestService) OnInit(srv *server.Server) { srv.RegStartFinishEvent(onStartFinish) srv.RegStopEvent(func(srv *server.Server) { fmt.Println("server stop") }) } func (ts *TestService) onStartFinish(srv *server.Server) { fmt.Println("server start finish") }
```
可以看出,在服务初始化时,该服务向服务器注册了启动完成事件及停止事件。这是我们推荐的编码方式,这样编码有以下好处:
- 具备可控制的初始化顺序,避免 init 产生的各种顺序导致的问题,如配置还未加载完成,即开始进行数据库连接等操作
- 可以方便的将不同的服务拆分到不同的包中进行管理
- 当不需要某个服务时,可以直接删除该服务的绑定,而不需要修改其他代码
- ...
srv := server.New(server.NetworkNone, server.WithLimitLife(time.Second)) server.BindService(srv, new(TestService)) if err := srv.RunNone(); err != nil { panic(err) }
Output: server start finish server stop
func BindServiceToMultipleServer ¶ added in v0.5.3
func BindServiceToMultipleServer(server *MultipleServer, services ...MultipleService)
BindServiceToMultipleServer 绑定服务到多个 MultipleServer
func DefaultWebsocketUpgrader ¶ added in v0.4.2
func HasMessageType ¶ added in v0.1.3
func HasMessageType(mt MessageType) bool
HasMessageType 检查是否存在指定的消息类型
Types ¶
type Bot ¶ added in v0.3.1
type Bot struct {
// contains filtered or unexported fields
}
func (*Bot) SendPacket ¶ added in v0.3.1
SendPacket 发送数据包到服务器
func (*Bot) SendWSPacket ¶ added in v0.3.1
SendWSPacket 发送 WebSocket 数据包到服务器
type BotOption ¶ added in v0.3.1
type BotOption func(bot *Bot)
func WithBotNetworkDelay ¶ added in v0.3.1
WithBotNetworkDelay 设置机器人网络延迟及波动范围
- delay 延迟
- fluctuation 波动范围
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn 服务器连接单次消息的包装
func (*Conn) GetMessageData ¶ added in v0.1.0
GetMessageData 获取消息数据
func (*Conn) GetOnlineTime ¶ added in v0.2.8
GetOnlineTime 获取连接在线时长
func (*Conn) GetOpenTime ¶ added in v0.2.8
GetOpenTime 获取连接打开时间
func (*Conn) GetWebsocketRequest ¶ added in v0.2.7
GetWebsocketRequest 获取websocket请求
func (*Conn) PushAsyncMessage ¶ added in v0.3.0
PushAsyncMessage 推送异步消息,该消息将通过 Server.PushShuntAsyncMessage 函数推送
- mark 为可选的日志标记,当发生异常时,将会在日志中进行体现
func (*Conn) PushUniqueAsyncMessage ¶ added in v0.3.1
func (slf *Conn) PushUniqueAsyncMessage(name string, caller func() error, callback func(err error), mark ...log.Field)
PushUniqueAsyncMessage 推送唯一异步消息,该消息将通过 Server.PushUniqueShuntAsyncMessage 函数推送
- mark 为可选的日志标记,当发生异常时,将会在日志中进行体现
- 不同的是当上一个相同的 unique 消息未执行完成时,将会忽略该消息
func (*Conn) SetMessageData ¶ added in v0.1.0
SetMessageData 设置消息数据,该数据将在消息处理完成后释放
type ConnectionClosedEventHandler ¶ added in v0.3.4
type ConnectionOpenedAfterEventHandler ¶ added in v0.3.4
type ConnectionOpenedEventHandler ¶ added in v0.3.4
type ConnectionPacketPreprocessEventHandler ¶ added in v0.3.4
type ConnectionReceivePacketEventHandler ¶ added in v0.3.4
type ConnectionWritePacketBeforeEventHandler ¶ added in v0.3.4
type ConsoleCommandEventHandler ¶ added in v0.3.4
type ConsoleCommandEventHandler func(srv *Server, command string, params ConsoleParams)
type ConsoleParams ¶ added in v0.3.0
ConsoleParams 控制台参数
func (ConsoleParams) Get ¶ added in v0.3.0
func (slf ConsoleParams) Get(key string) string
Get 获取参数值
func (ConsoleParams) GetValueNum ¶ added in v0.3.0
func (slf ConsoleParams) GetValueNum(key string) int
GetValueNum 获取参数值数量
func (ConsoleParams) GetValues ¶ added in v0.3.0
func (slf ConsoleParams) GetValues(key string) []string
GetValues 获取参数值
type ContextPacker ¶ added in v0.0.29
type HandlerFunc ¶ added in v0.0.29
type HandlerFunc[Context any] func(ctx Context)
type Http ¶ added in v0.0.29
type Http[Context any] struct { *gin.Engine *HttpRouter[Context] // contains filtered or unexported fields }
Http 基于 gin.Engine 包装的 http 服务器
func NewHttpHandleWrapper ¶ added in v0.0.29
func NewHttpHandleWrapper[Context any](srv *Server, packer ContextPacker[Context]) *Http[Context]
NewHttpHandleWrapper 创建一个新的 http 处理程序包装器
- 默认使用 server.HttpContext 作为上下文,如果需要依赖其作为新的上下文,可以通过 NewHttpContext 创建
type HttpContext ¶ added in v0.0.29
HttpContext 基于 gin.Context 的 http 请求上下文
func NewHttpContext ¶ added in v0.0.29
func NewHttpContext(ctx *gin.Context) *HttpContext
NewHttpContext 基于 gin.Context 创建一个新的 HttpContext
func (*HttpContext) Gin ¶ added in v0.0.29
func (slf *HttpContext) Gin() *gin.Context
Gin 获取 gin.Context
func (*HttpContext) ReadTo ¶ added in v0.0.29
func (slf *HttpContext) ReadTo(dest any) error
ReadTo 读取请求数据到指定结构体,如果失败则返回错误
type HttpRouter ¶ added in v0.0.29
type HttpRouter[Context any] struct { // contains filtered or unexported fields }
func (*HttpRouter[Context]) Any ¶ added in v0.0.29
func (slf *HttpRouter[Context]) Any(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
Any 注册一个匹配所有 HTTP 方法的路由
- GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.
func (*HttpRouter[Context]) CONNECT ¶ added in v0.0.29
func (slf *HttpRouter[Context]) CONNECT(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
CONNECT 是 Handle("CONNECT", path, handlers) 的快捷方式
func (*HttpRouter[Context]) DELETE ¶ added in v0.0.29
func (slf *HttpRouter[Context]) DELETE(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
DELETE 是 Handle("DELETE", path, handlers) 的快捷方式
func (*HttpRouter[Context]) GET ¶ added in v0.0.29
func (slf *HttpRouter[Context]) GET(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
GET 是 Handle("GET", path, handlers) 的快捷方式
func (*HttpRouter[Context]) Group ¶ added in v0.0.29
func (slf *HttpRouter[Context]) Group(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
Group 创建一个新的路由组。您应该添加所有具有共同中间件的路由。
- 例如: v1 := slf.Group("/v1")
func (*HttpRouter[Context]) HEAD ¶ added in v0.0.29
func (slf *HttpRouter[Context]) HEAD(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
HEAD 是 Handle("HEAD", path, handlers) 的快捷方式
func (*HttpRouter[Context]) Handle ¶ added in v0.0.29
func (slf *HttpRouter[Context]) Handle(httpMethod, relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
Handle 使用给定的路径和方法注册新的请求句柄和中间件
- 最后一个处理程序应该是真正的处理程序,其他处理程序应该是可以而且应该在不同路由之间共享的中间件。
func (*HttpRouter[Context]) Match ¶ added in v0.0.29
func (slf *HttpRouter[Context]) Match(methods []string, relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
Match 注册一个匹配指定 HTTP 方法的路由
- GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.
func (*HttpRouter[Context]) OPTIONS ¶ added in v0.0.29
func (slf *HttpRouter[Context]) OPTIONS(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
OPTIONS 是 Handle("OPTIONS", path, handlers) 的快捷方式
func (*HttpRouter[Context]) PATCH ¶ added in v0.0.29
func (slf *HttpRouter[Context]) PATCH(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
PATCH 是 Handle("PATCH", path, handlers) 的快捷方式
func (*HttpRouter[Context]) POST ¶ added in v0.0.29
func (slf *HttpRouter[Context]) POST(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
POST 是 Handle("POST", path, handlers) 的快捷方式
func (*HttpRouter[Context]) PUT ¶ added in v0.0.29
func (slf *HttpRouter[Context]) PUT(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
PUT 是 Handle("PUT", path, handlers) 的快捷方式
func (*HttpRouter[Context]) Static ¶ added in v0.0.29
func (slf *HttpRouter[Context]) Static(relativePath, root string) *HttpRouter[Context]
Static 提供来自给定文件系统根目录的文件。
- 例如: Static("/static", "/var/www")
func (*HttpRouter[Context]) StaticFS ¶ added in v0.0.29
func (slf *HttpRouter[Context]) StaticFS(relativePath string, fs http.FileSystem) *HttpRouter[Context]
StaticFS 与 `Static` 类似,但可以使用自定义的 `http.FileSystem` 代替。
- 例如: StaticFS("/static", Dir{"/var/www", false})
- 由于依赖于 gin.Engine 默认情况下使用:gin.Dir
func (*HttpRouter[Context]) StaticFile ¶ added in v0.0.29
func (slf *HttpRouter[Context]) StaticFile(relativePath, filepath string) *HttpRouter[Context]
StaticFile 注册单个路由以便为本地文件系统的单个文件提供服务。
- 例如: StaticFile("favicon.ico", "./resources/favicon.ico")
func (*HttpRouter[Context]) StaticFileFS ¶ added in v0.0.29
func (slf *HttpRouter[Context]) StaticFileFS(relativePath, filepath string, fs http.FileSystem) *HttpRouter[Context]
StaticFileFS 与 `StaticFile` 类似,但可以使用自定义的 `http.FileSystem` 代替。
- 例如: StaticFileFS("favicon.ico", "./resources/favicon.ico", Dir{".", false})
- 由于依赖于 gin.Engine 默认情况下使用:gin.Dir
func (*HttpRouter[Context]) TRACE ¶ added in v0.0.29
func (slf *HttpRouter[Context]) TRACE(relativePath string, handlers ...HandlerFunc[Context]) *HttpRouter[Context]
TRACE 是 Handle("TRACE", path, handlers) 的快捷方式
func (*HttpRouter[Context]) Use ¶ added in v0.5.0
func (slf *HttpRouter[Context]) Use(middleware ...HandlerFunc[Context]) *HttpRouter[Context]
Use 将中间件附加到路由组。
type HttpWrapper ¶ added in v0.0.26
type HttpWrapper[CTX any] struct { // contains filtered or unexported fields }
HttpWrapper http 包装器
func NewGinWrapper ¶ added in v0.0.29
NewGinWrapper 创建 gin 包装器,用于对 NewHttpWrapper 函数的替代
func (*HttpWrapper[CTX]) Any ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) Any(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
Any 注册 Any 请求
func (*HttpWrapper[CTX]) Connect ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) Connect(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
Connect 注册 Connect 请求
func (*HttpWrapper[CTX]) DELETE ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) DELETE(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
DELETE 注册 DELETE 请求
func (*HttpWrapper[CTX]) GET ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) GET(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
GET 注册 GET 请求
func (*HttpWrapper[CTX]) Group ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) Group(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
Group 创建一个新的路由组。您应该添加所有具有共同中间件的路由。
func (*HttpWrapper[CTX]) HEAD ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) HEAD(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
HEAD 注册 HEAD 请求
func (*HttpWrapper[CTX]) Handle ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) Handle(httpMethod, relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
Handle 处理请求
func (*HttpWrapper[CTX]) Match ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) Match(methods []string, relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
Match 注册与您声明的指定方法相匹配的路由。
func (*HttpWrapper[CTX]) OPTIONS ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) OPTIONS(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
OPTIONS 注册 OPTIONS 请求
func (*HttpWrapper[CTX]) PATCH ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) PATCH(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
PATCH 注册 PATCH 请求
func (*HttpWrapper[CTX]) POST ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) POST(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
POST 注册 POST 请求
func (*HttpWrapper[CTX]) PUT ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) PUT(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
PUT 注册 PUT 请求
func (*HttpWrapper[CTX]) Static ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) Static(relativePath, root string) *HttpWrapper[CTX]
Static 注册 Static 请求
func (*HttpWrapper[CTX]) StaticFS ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) StaticFS(relativePath string, fs http.FileSystem) *HttpWrapper[CTX]
StaticFS 注册 StaticFS 请求
func (*HttpWrapper[CTX]) StaticFile ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) StaticFile(relativePath, filepath string) *HttpWrapper[CTX]
StaticFile 注册 StaticFile 请求
func (*HttpWrapper[CTX]) Trace ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) Trace(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
Trace 注册 Trace 请求
func (*HttpWrapper[CTX]) Use ¶ added in v0.0.26
func (slf *HttpWrapper[CTX]) Use(middleware ...HttpWrapperHandleFunc[CTX]) *HttpWrapper[CTX]
Use 使用中间件
type HttpWrapperGroup ¶ added in v0.0.26
type HttpWrapperGroup[CTX any] struct { // contains filtered or unexported fields }
HttpWrapperGroup http 包装器
func (*HttpWrapperGroup[CTX]) DELETE ¶ added in v0.0.26
func (slf *HttpWrapperGroup[CTX]) DELETE(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
DELETE 注册 DELETE 请求
func (*HttpWrapperGroup[CTX]) GET ¶ added in v0.0.26
func (slf *HttpWrapperGroup[CTX]) GET(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
GET 注册 GET 请求
func (*HttpWrapperGroup[CTX]) Group ¶ added in v0.0.26
func (slf *HttpWrapperGroup[CTX]) Group(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
Group 创建分组
func (*HttpWrapperGroup[CTX]) Handle ¶ added in v0.0.26
func (slf *HttpWrapperGroup[CTX]) Handle(httpMethod, relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
Handle 处理请求
func (*HttpWrapperGroup[CTX]) OPTIONS ¶ added in v0.0.26
func (slf *HttpWrapperGroup[CTX]) OPTIONS(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
OPTIONS 注册 OPTIONS 请求
func (*HttpWrapperGroup[CTX]) PATCH ¶ added in v0.0.26
func (slf *HttpWrapperGroup[CTX]) PATCH(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
PATCH 注册 PATCH 请求
func (*HttpWrapperGroup[CTX]) POST ¶ added in v0.0.26
func (slf *HttpWrapperGroup[CTX]) POST(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
POST 注册 POST 请求
func (*HttpWrapperGroup[CTX]) PUT ¶ added in v0.0.26
func (slf *HttpWrapperGroup[CTX]) PUT(relativePath string, handlers ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
PUT 注册 PUT 请求
func (*HttpWrapperGroup[CTX]) Use ¶ added in v0.0.26
func (slf *HttpWrapperGroup[CTX]) Use(middleware ...HttpWrapperHandleFunc[CTX]) *HttpWrapperGroup[CTX]
Use 使用中间件
type HttpWrapperHandleFunc ¶ added in v0.0.26
type HttpWrapperHandleFunc[CTX any] func(ctx CTX)
type Message ¶
type Message struct {
// contains filtered or unexported fields
}
Message 服务器消息
func (*Message) GetProducer ¶ added in v0.5.0
func (*Message) MessageType ¶ added in v0.1.3
func (slf *Message) MessageType() MessageType
MessageType 返回消息类型
type MessageErrorEventHandler ¶ added in v0.3.4
type MessageExecBeforeEventHandler ¶ added in v0.3.4
type MessageLowExecEventHandler ¶ added in v0.3.4
type MessageReadyEventHandler ¶ added in v0.3.4
type MessageReadyEventHandler func(srv *Server)
type MessageType ¶
type MessageType byte
MessageType 消息类型
const ( // MessageTypePacket 数据包消息类型:该类型的数据将被发送到 ConnectionReceivePacketEvent 进行处理 MessageTypePacket MessageType = iota + 1 // MessageTypeTicker 定时器消息类型 MessageTypeTicker // MessageTypeShuntTicker 分流定时器消息类型 MessageTypeShuntTicker // MessageTypeAsync 异步消息类型 MessageTypeAsync // MessageTypeAsyncCallback 异步回调消息类型 MessageTypeAsyncCallback // MessageTypeShuntAsync 分流异步消息类型 MessageTypeShuntAsync // MessageTypeShuntAsyncCallback 分流异步回调消息类型 MessageTypeShuntAsyncCallback // MessageTypeUniqueAsync 唯一异步消息类型 MessageTypeUniqueAsync // MessageTypeUniqueAsyncCallback 唯一异步回调消息类型 MessageTypeUniqueAsyncCallback // MessageTypeUniqueShuntAsync 唯一分流异步消息类型 MessageTypeUniqueShuntAsync // MessageTypeUniqueShuntAsyncCallback 唯一分流异步回调消息类型 MessageTypeUniqueShuntAsyncCallback // MessageTypeSystem 系统消息类型 MessageTypeSystem // MessageTypeShunt 普通分流消息类型 MessageTypeShunt )
type MultipleServer ¶
type MultipleServer struct {
// contains filtered or unexported fields
}
func NewMultipleServer ¶
func NewMultipleServer(serverHandle ...func() (addr string, srv *Server)) *MultipleServer
func (*MultipleServer) OnExitEvent ¶ added in v0.0.4
func (slf *MultipleServer) OnExitEvent()
func (*MultipleServer) OnStartFinishEvent ¶ added in v0.5.3
func (slf *MultipleServer) OnStartFinishEvent()
func (*MultipleServer) RegExitEvent ¶ added in v0.0.4
func (slf *MultipleServer) RegExitEvent(handle func())
RegExitEvent 注册退出事件
func (*MultipleServer) RegStartFinishEvent ¶ added in v0.5.3
func (slf *MultipleServer) RegStartFinishEvent(handle func())
RegStartFinishEvent 注册启动完成事件
func (*MultipleServer) Run ¶
func (slf *MultipleServer) Run()
type MultipleService ¶ added in v0.5.3
type MultipleService interface { // OnInit 初始化服务,该方法将会在 Server 初始化时执行 // - 通常来说,该阶段发生任何错误都应该 panic 以阻止 Server 启动 OnInit(srv *MultipleServer) // OnPreloading 预加载阶段,该方法将会在所有服务的 OnInit 函数执行完毕后执行 // - 通常来说,该阶段发生任何错误都应该 panic 以阻止 Server 启动 OnPreloading(srv *MultipleServer) }
MultipleService 兼容传统 service 设计模式的接口,通过该接口可以实现更简洁、更具有可读性的服务绑定
type Network ¶
type Network string
Network 服务器运行的网络模式
- 根据不同的网络模式,服务器将会产生不同的行为,该类型将在服务器创建时候指定
服务器支持的网络模式如下:
- NetworkNone 该模式下不监听任何网络端口,仅开启消息队列,适用于纯粹的跨服服务器等情况
- NetworkTcp 该模式下将会监听 TCP 协议的所有地址,包括 IPv4 和 IPv6
- NetworkTcp4 该模式下将会监听 TCP 协议的 IPv4 地址
- NetworkTcp6 该模式下将会监听 TCP 协议的 IPv6 地址
- NetworkUdp 该模式下将会监听 UDP 协议的所有地址,包括 IPv4 和 IPv6
- NetworkUdp4 该模式下将会监听 UDP 协议的 IPv4 地址
- NetworkUdp6 该模式下将会监听 UDP 协议的 IPv6 地址
- NetworkUnix 该模式下将会监听 Unix 协议的地址
- NetworkHttp 该模式下将会监听 HTTP 协议的地址
- NetworkWebsocket 该模式下将会监听 Websocket 协议的地址
- NetworkKcp 该模式下将会监听 KCP 协议的地址
- NetworkGRPC 该模式下将会监听 GRPC 协议的地址
const ( // NetworkNone 该模式下不监听任何网络端口,仅开启消息队列,适用于纯粹的跨服服务器等情况 NetworkNone Network = "none" NetworkTcp Network = "tcp" NetworkTcp4 Network = "tcp4" NetworkTcp6 Network = "tcp6" NetworkUdp Network = "udp" NetworkUdp4 Network = "udp4" NetworkUdp6 Network = "udp6" NetworkUnix Network = "unix" NetworkHttp Network = "http" // NetworkWebsocket 该模式下需要获取url参数值时,可以通过连接的GetData函数获取 // - 当有多个同名参数时,获取到的值为切片类型 NetworkWebsocket Network = "websocket" NetworkKcp Network = "kcp" NetworkGRPC Network = "grpc" )
type OnDeadlockDetectEventHandler ¶ added in v0.3.5
type Option ¶
type Option func(srv *Server)
func WithAsyncLowMessageDuration ¶ added in v0.5.0
WithAsyncLowMessageDuration 通过指定异步消息的慢消息时长的方式创建服务器,当消息处理时间超过指定时长时,将会输出 WARN 类型的日志
- 默认值为 DefaultAsyncLowMessageDuration
- 当 duration <= 0 时,表示关闭慢消息检测
Example ¶
服务器在启动时将发布一条阻塞 1s 的异步消息,模拟了慢消息的过程,这时候如果通过 RegMessageLowExecEvent 函数注册过慢消息事件,将会收到该事件的消息
- 该示例中,将在收到慢消息时关闭服务器
package main import ( "fmt" "github.com/kercylan98/minotaur/server" "github.com/kercylan98/minotaur/utils/times" "time" ) func main() { srv := server.New(server.NetworkNone, server.WithAsyncLowMessageDuration(time.Second), ) srv.RegStartFinishEvent(func(srv *server.Server) { srv.PushAsyncMessage(func() error { time.Sleep(time.Second) return nil }, nil) }) srv.RegMessageLowExecEvent(func(srv *server.Server, message *server.Message, cost time.Duration) { srv.Shutdown() fmt.Println(times.GetSecond(cost)) }) if err := srv.RunNone(); err != nil { panic(err) } }
Output: 1
func WithAsyncPoolSize ¶ added in v0.0.10
WithAsyncPoolSize 通过指定异步消息池大小的方式创建服务器
- 当通过 WithDisableAsyncMessage 禁用异步消息时,此选项无效
- 默认值为 DefaultAsyncPoolSize
func WithConnWriteBufferSize ¶ added in v0.4.1
WithConnWriteBufferSize 通过连接写入缓冲区大小的方式创建服务器
- 默认值为 DefaultConnWriteBufferSize
- 设置合适的缓冲区大小可以提高服务器性能,但是会占用更多的内存
func WithDeadlockDetect ¶ added in v0.0.10
WithDeadlockDetect 通过死锁、死循环、永久阻塞检测的方式创建服务器
- 当检测到死锁、死循环、永久阻塞时,服务器将会生成 WARN 类型的日志,关键字为 "SuspectedDeadlock"
- 默认不开启死锁检测
func WithDisableAsyncMessage ¶ added in v0.0.10
func WithDisableAsyncMessage() Option
WithDisableAsyncMessage 通过禁用异步消息的方式创建服务器
func WithDispatcherBufferSize ¶ added in v0.4.1
WithDispatcherBufferSize 通过消息分发器缓冲区大小的方式创建服务器
- 默认值为 DefaultDispatcherBufferSize
- 设置合适的缓冲区大小可以提高服务器性能,但是会占用更多的内存
func WithGRPCServerOptions ¶
func WithGRPCServerOptions(options ...grpc.ServerOption) Option
WithGRPCServerOptions 通过GRPC的可选项创建GRPC服务器
func WithLowMessageDuration ¶ added in v0.5.0
WithLowMessageDuration 通过指定慢消息时长的方式创建服务器,当消息处理时间超过指定时长时,将会输出 WARN 类型的日志
- 默认值为 DefaultLowMessageDuration
- 当 duration <= 0 时,表示关闭慢消息检测
Example ¶
服务器在启动时将阻塞 1s,模拟了慢消息的过程,这时候如果通过 RegMessageLowExecEvent 函数注册过慢消息事件,将会收到该事件的消息
- 该示例中,将在收到慢消息时关闭服务器
package main import ( "fmt" "github.com/kercylan98/minotaur/server" "github.com/kercylan98/minotaur/utils/times" "time" ) func main() { srv := server.New(server.NetworkNone, server.WithLowMessageDuration(time.Second), ) srv.RegStartFinishEvent(func(srv *server.Server) { time.Sleep(time.Second) }) srv.RegMessageLowExecEvent(func(srv *server.Server, message *server.Message, cost time.Duration) { srv.Shutdown() fmt.Println(times.GetSecond(cost)) }) if err := srv.RunNone(); err != nil { panic(err) } }
Output: 1
func WithMessageStatistics ¶ added in v0.4.1
WithMessageStatistics 通过消息统计的方式创建服务器
- 默认不开启,当 duration 和 limit 均大于 0 的时候,服务器将记录每 duration 期间的消息数量,并保留最多 limit 条
func WithPacketWarnSize ¶ added in v0.3.3
WithPacketWarnSize 通过数据包大小警告的方式创建服务器,当数据包大小超过指定大小时,将会输出 WARN 类型的日志
- 默认值为 DefaultPacketWarnSize
- 当 size <= 0 时,表示不设置警告
func WithTicker ¶
WithTicker 通过定时器创建服务器,为服务器添加定时器功能
- poolSize:指定服务器定时器池大小,当池子内的定时器数量超出该值后,多余的定时器在释放时将被回收,该值小于等于 0 时将使用 timer.DefaultTickerPoolSize
- size:服务器定时器时间轮大小
- connSize:服务器连接定时器时间轮大小,当该值小于等于 0 的时候,在新连接建立时将不再为其创建定时器
- autonomy:定时器是否独立运行(独立运行的情况下不会作为服务器消息运行,会导致并发问题)
func WithWebsocketCompression ¶ added in v0.0.12
WithWebsocketCompression 通过数据压缩的方式创建Websocket服务器
- 默认不开启数据压缩
func WithWebsocketConnInitializer ¶ added in v0.4.2
func WithWebsocketConnInitializer(initializer func(writer http.ResponseWriter, request *http.Request, conn *websocket.Conn) error) Option
WithWebsocketConnInitializer 通过 websocket 连接初始化的方式创建服务器,当 initializer 返回错误时,服务器将不会处理该连接的后续逻辑
- 该选项仅在创建 NetworkWebsocket 服务器时有效
func WithWebsocketMessageType ¶
WithWebsocketMessageType 设置仅支持特定类型的Websocket消息
func WithWebsocketReadDeadline ¶ added in v0.0.8
WithWebsocketReadDeadline 设置 Websocket 读取超时时间
- 默认: DefaultWebsocketReadDeadline
- 当 t <= 0 时,表示不设置超时时间
func WithWebsocketUpgrade ¶ added in v0.4.2
WithWebsocketUpgrade 通过指定 websocket.Upgrader 的方式创建服务器
- 默认值为 DefaultWebsocketUpgrader
- 该选项仅在创建 NetworkWebsocket 服务器时有效
func WithWebsocketWriteCompression ¶ added in v0.0.12
func WithWebsocketWriteCompression() Option
WithWebsocketWriteCompression 通过数据写入压缩的方式创建Websocket服务器
- 默认不开启数据压缩
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server 网络服务器
func New ¶
New 根据特定网络类型创建一个服务器
Example ¶
该案例将创建一个简单的 WebSocket 服务器,如果需要更多的服务器类型可参考 [` Network `](#struct_Network) 部分
- server.WithLimitLife(time.Millisecond) 通常不是在正常开发应该使用的,在这里只是为了让服务器在启动完成后的 1 毫秒后自动关闭
该案例的输出结果为 true
package main import ( "fmt" "github.com/kercylan98/minotaur/server" "time" ) func main() { srv := server.New(server.NetworkWebsocket, server.WithLimitLife(time.Millisecond)) fmt.Println(srv != nil) }
Output: true
func (*Server) GRPCServer ¶
GRPCServer 当网络类型为 NetworkGRPC 时将被允许获取 grpc 服务器,否则将会发生 panic
func (*Server) GetAllDurationMessageCount ¶ added in v0.4.1
GetAllDurationMessageCount 获取所有 WithMessageStatistics 设置的 duration 期间的消息量
func (*Server) GetConnCurrShunt ¶ added in v0.4.1
GetConnCurrShunt 获取连接当前所使用的消息分流渠道
func (*Server) GetDurationMessageCount ¶ added in v0.4.1
GetDurationMessageCount 获取当前 WithMessageStatistics 设置的 duration 期间的消息量
func (*Server) GetDurationMessageCountByOffset ¶ added in v0.4.1
GetDurationMessageCountByOffset 获取特定偏移次数的 WithMessageStatistics 设置的 duration 期间的消息量
- 该值小于 0 时,将与 GetDurationMessageCount 无异,否则将返回 +n 个期间的消息量,例如 duration 为 1 分钟,limit 为 10,那么 offset 为 1 的情况下,获取的则是上一分钟消息量
func (*Server) GetMessageCount ¶ added in v0.0.29
GetMessageCount 获取当前服务器中消息的数量
func (*Server) GetNetwork ¶ added in v0.5.5
GetNetwork 返回服务器网络类型
func (Server) GetOnlineAll ¶ added in v0.0.12
GetOnlineAll 获取所有在线连接
func (Server) GetOnlineBotCount ¶ added in v0.3.1
func (h Server) GetOnlineBotCount() int
GetOnlineBotCount 获取在线机器人数量
func (Server) GetOnlineCount ¶ added in v0.0.12
func (h Server) GetOnlineCount() int
GetOnlineCount 获取在线人数
func (*Server) GetShuntNum ¶ added in v0.4.1
GetShuntNum 获取消息分流渠道数量
func (*Server) HasMessageStatistics ¶ added in v0.4.1
HasMessageStatistics 是否了开启消息统计
func (*Server) HttpRouter
deprecated
func (*Server) HttpServer ¶ added in v0.0.29
func (srv *Server) HttpServer() *Http[*HttpContext]
HttpServer 替代 HttpRouter 的函数,返回一个 *Http[*HttpContext] 对象
- 通过该函数注册的路由将在服务器关闭时正常等待请求结束
- 如果需要自行包装 Context 对象,可以使用 NewHttpHandleWrapper 方法
func (*Server) IsSocket ¶ added in v0.3.1
IsSocket 通过执行 Network.IsSocket 函数检查该服务器是否是 Socket 模式
Example ¶
该案例将创建两个不同类型的服务器,其中 WebSocket 是一个 Socket 服务器,而 Http 是一个非 Socket 服务器
可知案例输出结果为:
- true
- false
package main import ( "fmt" "github.com/kercylan98/minotaur/server" ) func main() { srv1 := server.New(server.NetworkWebsocket) fmt.Println(srv1.IsSocket()) srv2 := server.New(server.NetworkHttp) fmt.Println(srv2.IsSocket()) }
Output: true false
func (Server) OnConnectionClosedEvent ¶
func (Server) OnConnectionOpenedAfterEvent ¶ added in v0.0.21
func (slf Server) OnConnectionOpenedAfterEvent(conn *Conn)
func (Server) OnConnectionOpenedEvent ¶
func (slf Server) OnConnectionOpenedEvent(conn *Conn)
func (Server) OnConnectionPacketPreprocessEvent ¶ added in v0.0.28
func (Server) OnConnectionReceivePacketEvent ¶
func (Server) OnConnectionWritePacketBeforeEvent ¶ added in v0.0.21
func (Server) OnConsoleCommandEvent ¶
func (Server) OnDeadlockDetectEvent ¶ added in v0.3.5
func (slf Server) OnDeadlockDetectEvent(message *Message)
func (Server) OnMessageErrorEvent ¶
func (Server) OnMessageExecBeforeEvent ¶ added in v0.1.3
func (Server) OnMessageLowExecEvent ¶
func (Server) OnMessageReadyEvent ¶ added in v0.1.4
func (slf Server) OnMessageReadyEvent()
func (Server) OnShuntChannelClosedEvent ¶ added in v0.0.24
func (slf Server) OnShuntChannelClosedEvent(name string)
func (Server) OnShuntChannelCreatedEvent ¶ added in v0.0.24
func (slf Server) OnShuntChannelCreatedEvent(name string)
func (Server) OnStartBeforeEvent ¶
func (slf Server) OnStartBeforeEvent()
func (Server) OnStartFinishEvent ¶
func (slf Server) OnStartFinishEvent()
func (Server) OnStopEvent ¶ added in v0.0.9
func (slf Server) OnStopEvent()
func (*Server) PushAsyncMessage ¶ added in v0.2.9
func (srv *Server) PushAsyncMessage(caller func() error, callback func(err error), mark ...log.Field)
PushAsyncMessage 向服务器中推送 MessageTypeAsync 消息
- 异步消息将在服务器的异步消息队列中进行处理,处理完成 caller 的阻塞操作后,将会通过系统消息执行 callback 函数
- callback 函数将在异步消息处理完成后进行调用,无论过程是否产生 err,都将被执行,允许为 nil
- 需要注意的是,为了避免并发问题,caller 函数请仅处理阻塞操作,其他操作应该在 callback 函数中进行
- mark 为可选的日志标记,当发生异常时,将会在日志中进行体现
func (*Server) PushPacketMessage ¶ added in v0.3.0
PushPacketMessage 向服务器中推送 MessageTypePacket 消息
- 当存在 UseShunt 的选项时,将会根据选项中的 shuntMatcher 进行分发,否则将在系统分发器中处理消息
func (*Server) PushShuntAsyncMessage ¶ added in v0.3.0
func (srv *Server) PushShuntAsyncMessage(conn *Conn, caller func() error, callback func(err error), mark ...log.Field)
PushShuntAsyncMessage 向特定分发器中推送 MessageTypeAsync 消息,消息执行与 MessageTypeAsync 一致
- 需要注意的是,当未指定 UseShunt 时,将会通过 PushAsyncMessage 进行转发
- mark 为可选的日志标记,当发生异常时,将会在日志中进行体现
func (*Server) PushShuntMessage ¶ added in v0.3.4
PushShuntMessage 向特定分发器中推送 MessageTypeShunt 消息,消息执行与 MessageTypeSystem 一致,不同的是将会在特定分发器中执行
func (*Server) PushShuntTickerMessage ¶ added in v0.3.0
func (srv *Server) PushShuntTickerMessage(conn *Conn, name string, caller func(), mark ...log.Field)
PushShuntTickerMessage 向特定分发器中推送 MessageTypeTicker 消息,消息执行与 MessageTypeTicker 一致
- 需要注意的是,当未指定 UseShunt 时,将会通过 PushTickerMessage 进行转发
- mark 为可选的日志标记,当发生异常时,将会在日志中进行体现
func (*Server) PushSystemMessage ¶ added in v0.3.0
PushSystemMessage 向服务器中推送 MessageTypeSystem 消息
- 系统消息仅包含一个可执行函数,将在系统分发器中执行
- mark 为可选的日志标记,当发生异常时,将会在日志中进行体现
func (*Server) PushTickerMessage ¶ added in v0.3.0
PushTickerMessage 向服务器中推送 MessageTypeTicker 消息
- 通过该函数推送定时消息,当消息触发时将在系统分发器中处理消息
- 可通过 timer.Ticker 或第三方定时器将执行函数(caller)推送到该消息中进行处理,可有效的避免线程安全问题
- 参数 name 仅用作标识该定时器名称
定时消息执行不会有特殊的处理,仅标记为定时任务,也就是允许将各类函数通过该消息发送处理,但是并不建议
- mark 为可选的日志标记,当发生异常时,将会在日志中进行体现
func (*Server) PushUniqueAsyncMessage ¶ added in v0.3.1
func (srv *Server) PushUniqueAsyncMessage(unique string, caller func() error, callback func(err error), mark ...log.Field)
PushUniqueAsyncMessage 向服务器中推送 MessageTypeAsync 消息,消息执行与 MessageTypeAsync 一致
- 不同的是当上一个相同的 unique 消息未执行完成时,将会忽略该消息
func (*Server) PushUniqueShuntAsyncMessage ¶ added in v0.3.1
func (srv *Server) PushUniqueShuntAsyncMessage(conn *Conn, unique string, caller func() error, callback func(err error), mark ...log.Field)
PushUniqueShuntAsyncMessage 向特定分发器中推送 MessageTypeAsync 消息,消息执行与 MessageTypeAsync 一致
- 需要注意的是,当未指定 UseShunt 时,将会通过系统分流渠道进行转发
- 不同的是当上一个相同的 unique 消息未执行完成时,将会忽略该消息
func (Server) RegConnectionClosedEvent ¶
func (slf Server) RegConnectionClosedEvent(handler ConnectionClosedEventHandler, priority ...int)
RegConnectionClosedEvent 在连接关闭后将立刻执行被注册的事件处理函数
func (Server) RegConnectionOpenedAfterEvent ¶ added in v0.0.21
func (slf Server) RegConnectionOpenedAfterEvent(handler ConnectionOpenedAfterEventHandler, priority ...int)
RegConnectionOpenedAfterEvent 在连接打开事件处理完成后将立刻执行被注册的事件处理函数
- 该阶段事件将会转到对应消息分流渠道中进行处理
func (Server) RegConnectionOpenedEvent ¶
func (slf Server) RegConnectionOpenedEvent(handler ConnectionOpenedEventHandler, priority ...int)
RegConnectionOpenedEvent 在连接打开后将立刻执行被注册的事件处理函数
- 该阶段的事件将会在系统消息中进行处理,不适合处理耗时操作
func (Server) RegConnectionPacketPreprocessEvent ¶ added in v0.0.28
func (slf Server) RegConnectionPacketPreprocessEvent(handler ConnectionPacketPreprocessEventHandler, priority ...int)
RegConnectionPacketPreprocessEvent 在接收到数据包后将立刻执行被注册的事件处理函数
- 预处理函数可以用于对数据包进行预处理,如解密、解压缩等
- 在调用 abort() 后,将不会再调用后续的预处理函数,也不会调用 OnConnectionReceivePacketEvent 函数
- 在调用 usePacket() 后,将使用新的数据包,而不会使用原始数据包,同时阻止后续的预处理函数的调用
场景:
- 数据包格式校验
- 数据包分包等情况处理
func (Server) RegConnectionReceivePacketEvent ¶
func (slf Server) RegConnectionReceivePacketEvent(handler ConnectionReceivePacketEventHandler, priority ...int)
RegConnectionReceivePacketEvent 在接收到数据包时将立刻执行被注册的事件处理函数
func (Server) RegConnectionWritePacketBeforeEvent ¶ added in v0.0.21
func (slf Server) RegConnectionWritePacketBeforeEvent(handler ConnectionWritePacketBeforeEventHandler, priority ...int)
RegConnectionWritePacketBeforeEvent 在发送数据包前将立刻执行被注册的事件处理函数
func (Server) RegConsoleCommandEvent ¶
func (slf Server) RegConsoleCommandEvent(command string, handler ConsoleCommandEventHandler, priority ...int)
RegConsoleCommandEvent 控制台收到指令时将立即执行被注册的事件处理函数
- 默认将注册 "exit", "quit", "close", "shutdown", "EXIT", "QUIT", "CLOSE", "SHUTDOWN" 指令作为关闭服务器的指令
- 可通过注册默认指令进行默认行为的覆盖
func (Server) RegDeadlockDetectEvent ¶ added in v0.3.5
func (slf Server) RegDeadlockDetectEvent(handler OnDeadlockDetectEventHandler, priority ...int)
RegDeadlockDetectEvent 在死锁检测触发时立即执行被注册的事件处理函数
func (Server) RegMessageErrorEvent ¶
func (slf Server) RegMessageErrorEvent(handler MessageErrorEventHandler, priority ...int)
RegMessageErrorEvent 在处理消息发生错误时将立即执行被注册的事件处理函数
func (Server) RegMessageExecBeforeEvent ¶ added in v0.1.3
func (slf Server) RegMessageExecBeforeEvent(handler MessageExecBeforeEventHandler, priority ...int)
RegMessageExecBeforeEvent 在处理消息前将立刻执行被注册的事件处理函数
- 当返回 true 时,将继续执行后续的消息处理函数,否则将不会执行后续的消息处理函数,并且该消息将被丢弃
适用于限流等场景
func (Server) RegMessageLowExecEvent ¶
func (slf Server) RegMessageLowExecEvent(handler MessageLowExecEventHandler, priority ...int)
RegMessageLowExecEvent 在处理消息缓慢时将立即执行被注册的事件处理函数
func (Server) RegMessageReadyEvent ¶ added in v0.1.4
func (slf Server) RegMessageReadyEvent(handler MessageReadyEventHandler, priority ...int)
RegMessageReadyEvent 在服务器消息处理器准备就绪时立即执行被注册的事件处理函数
func (Server) RegShuntChannelCloseEvent ¶ added in v0.0.24
func (slf Server) RegShuntChannelCloseEvent(handler ShuntChannelClosedEventHandler, priority ...int)
RegShuntChannelCloseEvent 在分流通道关闭时将立刻执行被注册的事件处理函数
func (Server) RegShuntChannelCreatedEvent ¶ added in v0.0.24
func (slf Server) RegShuntChannelCreatedEvent(handler ShuntChannelCreatedEventHandler, priority ...int)
RegShuntChannelCreatedEvent 在分流通道创建时将立刻执行被注册的事件处理函数
func (Server) RegStartBeforeEvent ¶
func (slf Server) RegStartBeforeEvent(handler StartBeforeEventHandler, priority ...int)
RegStartBeforeEvent 在服务器初始化完成启动前立刻执行被注册的事件处理函数
func (Server) RegStartFinishEvent ¶
func (slf Server) RegStartFinishEvent(handler StartFinishEventHandler, priority ...int)
RegStartFinishEvent 在服务器启动完成时将立刻执行被注册的事件处理函数
- 需要注意该时刻服务器已经启动完成,但是还有可能未开始处理消息,客户端有可能无法连接,如果需要在消息处理器准备就绪后执行,请使用 RegMessageReadyEvent 函数
func (Server) RegStopEvent ¶ added in v0.0.9
func (slf Server) RegStopEvent(handler StopEventHandler, priority ...int)
RegStopEvent 服务器停止时将立即执行被注册的事件处理函数
func (*Server) Run ¶
Run 使用特定地址运行服务器
- server.NetworkTcp (addr:":8888")
- server.NetworkTcp4 (addr:":8888")
- server.NetworkTcp6 (addr:":8888")
- server.NetworkUdp (addr:":8888")
- server.NetworkUdp4 (addr:":8888")
- server.NetworkUdp6 (addr:":8888")
- server.NetworkUnix (addr:"socketPath")
- server.NetworkHttp (addr:":8888")
- server.NetworkWebsocket (addr:":8888/ws")
- server.NetworkKcp (addr:":8888")
- server.NetworkNone (addr:"")
Example ¶
该案例将创建一个简单的 WebSocket 服务器并启动监听 `:9999/` 作为 WebSocket 监听地址,如果需要更多的服务器类型可参考 [` Network `](#struct_Network) 部分
- 当服务器启动失败后,将会返回错误信息并触发 panic
- server.WithLimitLife(time.Millisecond) 通常不是在正常开发应该使用的,在这里只是为了让服务器在启动完成后的 1 毫秒后自动关闭
package main import ( "github.com/kercylan98/minotaur/server" "time" ) func main() { srv := server.New(server.NetworkWebsocket, server.WithLimitLife(time.Millisecond)) if err := srv.Run(":9999"); err != nil { panic(err) } }
Output:
func (*Server) RunNone ¶ added in v0.0.29
RunNone 是 Run("") 的简写,仅适用于运行 NetworkNone 服务器
Example ¶
RunNone 函数并没有特殊的意义,该函数内部调用了 `srv.Run("")` 函数,仅是一个语法糖,用来表示服务器不需要监听任何地址
package main import ( "github.com/kercylan98/minotaur/server" ) func main() { srv := server.New(server.NetworkNone) if err := srv.RunNone(); err != nil { panic(err) } }
Output:
func (*Server) TimeoutContext ¶ added in v0.1.3
TimeoutContext 获取服务器超时上下文,context.WithTimeout 的简写
type Service ¶ added in v0.4.2
type Service interface { // OnInit 初始化服务,该方法将会在 Server 初始化时执行 // - 通常来说,该阶段发生任何错误都应该 panic 以阻止 Server 启动 OnInit(srv *Server) }
Service 兼容传统 service 设计模式的接口,通过该接口可以实现更简洁、更具有可读性的服务绑定
- 在这之前,我们在实现功能上会将 Server 进行全局存储,之后通过 init 函数进行初始化,这样的顺序是不可控的。
type ShuntChannelClosedEventHandler ¶ added in v0.3.4
type ShuntChannelCreatedEventHandler ¶ added in v0.3.4
type StartBeforeEventHandler ¶ added in v0.3.4
type StartBeforeEventHandler func(srv *Server)
type StartFinishEventHandler ¶ added in v0.3.4
type StartFinishEventHandler func(srv *Server)
type StopEventHandler ¶ added in v0.3.4
type StopEventHandler func(srv *Server)
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package gateway 是用于处理服务器消息的网关模块,适用于对客户端消息进行处理、转发的情况。
|
Package gateway 是用于处理服务器消息的网关模块,适用于对客户端消息进行处理、转发的情况。 |
internal
|
|