Documentation ¶
Index ¶
- Constants
- Variables
- type Agent
- func (g *Agent) Address() string
- func (g *Agent) All() map[string]Client
- func (g *Agent) Broadcast(msg []byte, filter func(client Client) bool)
- func (g *Agent) ClientCodec() *codec.Client
- func (g *Agent) Close()
- func (g *Agent) Count() int
- func (g *Agent) Filter(filter func(client Client) bool) map[string]Client
- func (g *Agent) ForEach(fn func(client Client) bool)
- func (g *Agent) GetClient(val string, by ...string) Client
- func (g *Agent) Opts() *Options
- func (g *Agent) Run() error
- func (g *Agent) Server() Server
- func (g *Agent) ServerCodec() *codec.Server
- func (g *Agent) SetOnDisconnect(fn func(Client))
- func (g *Agent) SetOnReceive(fn func(Client, *codec.ClientHead, []byte) (*codec.ServerHead, []byte, error))
- func (g *Agent) StartClient(client Client)
- type Client
- type Meta
- type Option
- func WithAddress(addr string) Option
- func WithHeartbeatDeadline(t time.Duration) Option
- func WithHeartbeatInterval(t time.Duration) Option
- func WithMaxConnNum(num uint) Option
- func WithReadTimeout(t time.Duration) Option
- func WithWaitAuthTime(t time.Duration) Option
- func WithWriteTimeout(t time.Duration) Option
- type Options
- type Server
Constants ¶
View Source
const ( MetaClientId = "Client-Id" // 客户端连接ID MetaClientIp = "Client-Ip" // IPv4地址 MetaClientVer = "Client-Ver" // 客户端版本 MetaAccountId = "Account-Id" // 账户ID MetaChannelUid = "Channel-Uid" // 渠道账户 MetaServerId = "Server-Id" // 服务器ID MetaRoleId = "Role-Id" // 角色ID )
Variables ¶
View Source
var ( Opts = &struct { Type string // 网关类型, websocket / tcp / quic Host string // 主机IP地址 Port string // 网关监听端口 ConnMaxNum uint // 网关最大连接 HeartbeatInterval int64 // 网关 WebSocket 心跳间隔 HeartbeatDeadline int64 // 网关 WebSocket 心跳等待 AuthTime int64 // 网关 鉴权认证 有效时间 }{} Flags = []cli.Flag{ &cli.StringFlag{ Name: "agent_type", Value: "websocket", Usage: "设置网关类型. 目前支持 websocket, tcp, quic", EnvVars: []string{"GAME_AGENT_TYPE"}, Destination: &Opts.Type, Required: true, }, &cli.StringFlag{ Name: "agent_host", Value: "", Usage: "设置当前网关的外网IP地址", EnvVars: []string{"GAME_AGENT_HOST"}, Destination: &Opts.Host, Required: true, }, &cli.StringFlag{ Name: "agent_port", Value: ":0", Usage: "设置当前网关的端口. default :0", Destination: &Opts.Port, }, &cli.Int64Flag{ Name: "agent_heartbeat_interval", Value: 30, Usage: "设置当前网关的心跳的时间间隔 (单位秒)", EnvVars: []string{"GAME_AGENT_HEARTBEAT_INTERVAL"}, Destination: &Opts.HeartbeatInterval, }, &cli.Int64Flag{ Name: "agent_heartbeat_deadline", Value: 10, Usage: "设置当前网关的心跳的等待时间 (单位秒)", EnvVars: []string{"GAME_AGENT_HEARTBEAT_DEADLINE"}, Destination: &Opts.HeartbeatDeadline, }, &cli.UintFlag{ Name: "agent_conn_max", Value: 0, Usage: "设置当前网关的最大连接数", EnvVars: []string{"GAME_AGENT_CONN_MAX"}, Destination: &Opts.ConnMaxNum, }, &cli.Int64Flag{ Name: "agent_auth_time", Value: 5, Usage: "设置当前网关的鉴权认证有效时间 (单位秒)", EnvVars: []string{"GAME_AGENT_AUTH_TIME"}, Destination: &Opts.AuthTime, }, } )
View Source
var ( DefaultReadTimeout = 15 * time.Second // 默认请求超时时间 DefaultWriteTimeout = 15 * time.Second // 默认请求超时时间 )
View Source
var (
RegisterAgent = map[string]func(agent *Agent) Server{}
)
Functions ¶
This section is empty.
Types ¶
type Agent ¶
type Agent struct { sync.RWMutex OnReceive func(Client, *codec.ClientHead, []byte) (*codec.ServerHead, []byte, error) // 收到数据调用 OnDisconnect func(Client) // 连接断开时调用 // contains filtered or unexported fields }
func (*Agent) ClientCodec ¶
func (*Agent) ServerCodec ¶
func (*Agent) SetOnDisconnect ¶
func (*Agent) SetOnReceive ¶
func (g *Agent) SetOnReceive(fn func(Client, *codec.ClientHead, []byte) (*codec.ServerHead, []byte, error))
func (*Agent) StartClient ¶
type Client ¶
type Client interface { Id() string // Client Id Server() Server // 关联服务端 Meta() *Meta // 客户端上下文 Log() *logger.Helper // 日志对象 Closed() bool // 判断是否关闭 Read() (*codec.ClientHead, []byte, error) // 读取消息 Write([]byte) // 发送消息 Close() // 关闭连接 Destroy() // 销毁连接 (丢弃任何未发送或未确认的数据) SetAuthState(state bool) // 设置认证状态 (建立Socket连接后, 需要发送Token进行认证) }
客户端
type Option ¶
type Option func(o *Options)
func WithAddress ¶
func WithHeartbeatDeadline ¶
func WithHeartbeatInterval ¶
func WithMaxConnNum ¶
func WithReadTimeout ¶
func WithWaitAuthTime ¶
func WithWriteTimeout ¶
type Options ¶
type Options struct { Address string // 服务器监听地址 MaxConnNum uint // 最大连接数限制 WaitAuthTime time.Duration // 等待认证时间 HeartbeatInterval time.Duration // 心跳间隔 HeartbeatDeadline time.Duration // 心跳等待 ReadTimeout time.Duration // 读超时 WriteTimeout time.Duration // 写超时 }
服务器参数结果提
func NewOptions ¶
Click to show internal directories.
Click to hide internal directories.