Documentation ¶
Index ¶
- Constants
- Variables
- type ClientCreater
- type ClientNamer
- type ParamConfig
- type TCPClient
- func (tc *TCPClient[ClientInfo]) Close(err error)
- func (tc *TCPClient[ClientInfo]) CloseWait(err error)
- func (tc *TCPClient[ClientInfo]) ConnName() string
- func (tc *TCPClient[ClientInfo]) Info() *ClientInfo
- func (tc *TCPClient[ClientInfo]) LocalAddr() string
- func (tc *TCPClient[ClientInfo]) RemoteAddr() string
- func (tc *TCPClient[ClientInfo]) Send(ctx context.Context, data []byte) error
- func (tc *TCPClient[ClientInfo]) SendMsg(ctx context.Context, msg utils.SendMsger) error
- func (tc *TCPClient[ClientInfo]) SendRPCMsg(ctx context.Context, rpcId interface{}, msg utils.SendMsger, ...) (interface{}, error)
- func (tc *TCPClient[ClientInfo]) SendText(ctx context.Context, data []byte) error
- type TCPEvent
- type TCPEventHandler
- func (*TCPEventHandler[ClientInfo]) CheckRPCResp(msg interface{}) interface{}
- func (h *TCPEventHandler[ClientInfo]) Context(parent context.Context, msg interface{}) context.Context
- func (*TCPEventHandler[ClientInfo]) DecodeMsg(ctx context.Context, data []byte, tc *TCPClient[ClientInfo]) (interface{}, int, error)
- func (*TCPEventHandler[ClientInfo]) OnConnected(ctx context.Context, tc *TCPClient[ClientInfo])
- func (*TCPEventHandler[ClientInfo]) OnDisConnect(ctx context.Context, tc *TCPClient[ClientInfo])
- func (*TCPEventHandler[ClientInfo]) OnMsg(ctx context.Context, msg interface{}, tc *TCPClient[ClientInfo])
- func (*TCPEventHandler[ClientInfo]) OnTick(ctx context.Context, tc *TCPClient[ClientInfo])
- type TCPHook
- type TCPServer
- func NewTCPServer[ClientId any, ClientInfo any](port int, event TCPEvent[ClientInfo]) (*TCPServer[ClientId, ClientInfo], error)
- func NewTCPServerWithWS[ClientId any, ClientInfo any](port int, event TCPEvent[ClientInfo], certFile, keyFile string) (*TCPServer[ClientId, ClientInfo], error)
- func NewTCPServerWithWS2[ClientId any, ClientInfo any](port int, event TCPEvent[ClientInfo], certPEMBlock, keyPEMBlock []byte) (*TCPServer[ClientId, ClientInfo], error)
- func (s *TCPServer[ClientId, ClientInfo]) AddClient(id ClientId, tc *TCPClient[ClientInfo])
- func (s *TCPServer[ClientId, ClientInfo]) ClientCount() int
- func (s *TCPServer[ClientId, ClientInfo]) CloseClient(id ClientId)
- func (s *TCPServer[ClientId, ClientInfo]) ConnCount() (int, int)
- func (s *TCPServer[ClientId, ClientInfo]) GetClient(id ClientId) *TCPClient[ClientInfo]
- func (s *TCPServer[ClientId, ClientInfo]) OnAccept(c net.Conn)
- func (s *TCPServer[ClientId, ClientInfo]) OnClose(c *tcp.TCPConn)
- func (s *TCPServer[ClientId, ClientInfo]) OnDisConnect(err error, c *tcp.TCPConn) error
- func (s *TCPServer[ClientId, ClientInfo]) OnRecv(data []byte, c *tcp.TCPConn) (int, error)
- func (s *TCPServer[ClientId, ClientInfo]) OnSend(data []byte, c *tcp.TCPConn) ([]byte, error)
- func (s *TCPServer[ClientId, ClientInfo]) OnShutdown()
- func (s *TCPServer[ClientId, ClientInfo]) RangeClient(f func(tc *TCPClient[ClientInfo]) bool)
- func (s *TCPServer[ClientId, ClientInfo]) RegHook(h TCPHook[ClientInfo])
- func (s *TCPServer[ClientId, ClientInfo]) RemoveClient(id ClientId) *TCPClient[ClientInfo]
- func (s *TCPServer[ClientId, ClientInfo]) Send(ctx context.Context, id ClientId, data []byte) error
- func (s *TCPServer[ClientId, ClientInfo]) SendMsg(ctx context.Context, id ClientId, msg utils.SendMsger) error
- func (s *TCPServer[ClientId, ClientInfo]) Start(reusePort bool) error
- func (s *TCPServer[ClientId, ClientInfo]) Stop() error
Constants ¶
View Source
const CtxKey_Text = utils.CtxKey("text") // 数据为text格式,否则为二进制格式 值:不受限制 一般写1
View Source
const CtxKey_WS = utils.CtxKey("ws") // 表示ws连接 值:不受限制 一般写1
Variables ¶
View Source
var ParamConf loader.JsonLoader[ParamConfig]
View Source
var WSHeader = http.Header{ "GOVersion": []string{runtime.Version()}, "GOOS": []string{runtime.GOOS}, "GOARCH": []string{runtime.GOARCH}, }
握手时给客户端回复的头,外部可修改
Functions ¶
This section is empty.
Types ¶
type ClientCreater ¶
type ClientCreater interface {
ClientCreate()
}
type ClientNamer ¶
type ClientNamer interface {
ClientName() string
}
type ParamConfig ¶
type ParamConfig struct { IgnoreIp []string `json:"ignoreip,omitempty"` // 建立连接和失去连接时,log输出忽略的ip, 支持?*通配符 不区分大小写 // SendMsg接口中,输出日志等级会按照下面的配置来执行,否则按照Debug输出 // 日志级别和zerolog.Level一致 LogLevelMsg int `json:"loglevelmsg,omitempty"` // msg消息默认的消息级别,不配置就是debug级别 LogLevelByMsg map[string]int `json:"loglevelbymsg,omitempty"` // 根据消息ID区分的消息日志级别,消息ID:日志级别,不配置就使用LogLevelMsg级别 ActiveTimeout int `json:"activetimeout,omitempty"` // 连接活跃超时时间 单位秒 <=0表示不检查活跃 MsgSeq bool `json:"msgseq,omitempty"` // 消息顺序执行 WSHeader map[string][]string `json:"wsheader,omitempty"` // websocket握手时 回复的头 }
参数配置
func (*ParamConfig) Create ¶
func (c *ParamConfig) Create()
func (*ParamConfig) IsIgnoreIp ¶
func (c *ParamConfig) IsIgnoreIp(ip string) bool
func (*ParamConfig) MsgLogLevel ¶
func (c *ParamConfig) MsgLogLevel(msgid string) int
func (*ParamConfig) Normalize ¶
func (c *ParamConfig) Normalize()
type TCPClient ¶
type TCPClient[ClientInfo any] struct { // contains filtered or unexported fields }
TCPClient是TCPServer创建的连接对象 ClientInfo是和业务相关的客户端信息结构 如果ClientInfo存在ClientCreate函数,创建链接时会调用 如果ClientInfo存在ClientName函数,输出日志是会调用
func (*TCPClient[ClientInfo]) RemoteAddr ¶
func (*TCPClient[ClientInfo]) SendMsg ¶
SendMsg 发送消息对象,会调用消息对象的MsgMarshal来编码消息 消息对象可实现zerolog.LogObjectMarshaler接口,更好的输出日志,通过ParamConf.LogLevelMsg配置可控制日志级别
func (*TCPClient[ClientInfo]) SendRPCMsg ¶
func (tc *TCPClient[ClientInfo]) SendRPCMsg(ctx context.Context, rpcId interface{}, msg utils.SendMsger, timeout time.Duration) (interface{}, error)
SendRPCMsg 发送RPC消息并等待消息回复,需要依赖event.CheckRPCResp来判断是否rpc调用 成功返回解析后的消息 消息对象可实现zerolog.LogObjectMarshaler接口,更好的输出日志,通过ParamConf.LogLevelMsg配置可控制日志级别
type TCPEvent ¶
type TCPEvent[ClientInfo any] interface { // 收到连接 OnConnected(ctx context.Context, tc *TCPClient[ClientInfo]) // 用户掉线 OnDisConnect(ctx context.Context, tc *TCPClient[ClientInfo]) // DecodeMsg 解码消息实现 // 返回值为 msg,len,err // msg 解码出的消息体 // len 解码消息的数据长度,内部根据len来删除已解码的数据 // err 解码错误,若发生error,服务器将重连 DecodeMsg(ctx context.Context, data []byte, tc *TCPClient[ClientInfo]) (interface{}, int, error) // Context 生成Context, 目前OnMsg、OnTick参数使用 // msg为nil时 表示是OnTick调用 Context(parent context.Context, msg interface{}) context.Context // CheckRPCResp 判断是否RPC返回消息,如果使用SendRPCMsg需要实现此函数 // 返回值为 rpcid // rpcid 对应请求SendRPC的id, 返回nil表示非rpc调用 CheckRPCResp(msg interface{}) interface{} // OnRecv 收到消息,解码成功后调用 异步顺序调用,rpc不会调用此函数 OnMsg(ctx context.Context, msg interface{}, tc *TCPClient[ClientInfo]) // OnTick 每秒调用一次 异步调用 OnTick(ctx context.Context, tc *TCPClient[ClientInfo]) }
type TCPEventHandler ¶
type TCPEventHandler[ClientInfo any] struct { }
TCPEventHandler TCPEvent的内置实现 如果不想实现TCPEvent的所有接口,可以继承它实现部分方法
func (*TCPEventHandler[ClientInfo]) CheckRPCResp ¶
func (*TCPEventHandler[ClientInfo]) CheckRPCResp(msg interface{}) interface{}
func (*TCPEventHandler[ClientInfo]) Context ¶
func (h *TCPEventHandler[ClientInfo]) Context(parent context.Context, msg interface{}) context.Context
func (*TCPEventHandler[ClientInfo]) OnConnected ¶
func (*TCPEventHandler[ClientInfo]) OnConnected(ctx context.Context, tc *TCPClient[ClientInfo])
func (*TCPEventHandler[ClientInfo]) OnDisConnect ¶
func (*TCPEventHandler[ClientInfo]) OnDisConnect(ctx context.Context, tc *TCPClient[ClientInfo])
type TCPHook ¶
type TCPHook[ClientInfo any] interface { // 收到连接 OnConnected(tc *TCPClient[ClientInfo]) // ws连接握手 OnWSHandShake(gc *TCPClient[ClientInfo]) // 用户掉线,removeClient表示是否引起RemoveClient,但不会调用OnRemoveClient OnDisConnect(tc *TCPClient[ClientInfo], removeClient bool, closeReason error) // 添加Client OnAddClient(tc *TCPClient[ClientInfo]) // 添加Client OnRemoveClient(tc *TCPClient[ClientInfo]) // 发送数据 OnSend(tc *TCPClient[ClientInfo], len int) // 接受数据 OnRecv(tc *TCPClient[ClientInfo], len int) }
Hook
type TCPServer ¶
type TCPServer[ClientId any, ClientInfo any] struct { *tcp.TCPConnEvenHandle // 不可需改 Address string // 监听地址 Scheme string // scheme支持tcp和ws,为空表示tcp // contains filtered or unexported fields }
TCPServer ClientId客户端ID类型 ClientInfo是和业务相关的客户端信息结构类型
func NewTCPServer ¶
func NewTCPServer[ClientId any, ClientInfo any](port int, event TCPEvent[ClientInfo]) (*TCPServer[ClientId, ClientInfo], error)
创建服务器
func NewTCPServerWithWS ¶
func NewTCPServerWithWS2 ¶
func (*TCPServer[ClientId, ClientInfo]) ClientCount ¶
func (*TCPServer[ClientId, ClientInfo]) CloseClient ¶
func (s *TCPServer[ClientId, ClientInfo]) CloseClient(id ClientId)
主动关闭 不会回调event的OnDisConnect 使用TCPClient.Close会回调OnDisConnect
func (*TCPServer[ClientId, ClientInfo]) OnDisConnect ¶
func (*TCPServer[ClientId, ClientInfo]) OnShutdown ¶
func (s *TCPServer[ClientId, ClientInfo]) OnShutdown()
func (*TCPServer[ClientId, ClientInfo]) RangeClient ¶
遍历Client f函数返回false 停止遍历
func (*TCPServer[ClientId, ClientInfo]) RemoveClient ¶
Click to show internal directories.
Click to hide internal directories.