network

package
v2.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 2 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attr added in v2.3.0

type Attr interface {
	// Set 设置属性值
	// @param key any 属性键
	// @param value any 属性值
	Set(key, value any)
	// Get 获取属性值
	// @param key any 属性键
	// @return @1 any 属性值
	// @return @2 bool 是否存在
	Get(key any) (any, bool)
	// Del 删除属性值
	// @param key any 属性键
	// @return @1 bool 是否删除成功
	Del(key any) bool
	// Clear 清空所有属性值
	Clear()
	// Visit 访问所有的属性值
	// @param fn func(key, value any) bool 遍历函数,返回 false 时停止遍历
	Visit(fn func(key, value any) bool)
}

Attr 连接属性接口

type Client

type Client interface {
	// Dial 拨号连接
	// @param addr ...string 拨号地址
	// @return @1 Conn 连接对象
	// @return @2 error 错误信息
	Dial(addr ...string) (Conn, error)
	// Protocol 获取协议名称
	// @return @1 string 协议名称
	Protocol() string
	// OnConnect 监听连接打开
	// @param handler ConnectHandler 连接打开处理函数
	OnConnect(handler ConnectHandler)
	// OnHeartbeat 监听心跳
	// @param handler HeartbeatHandler 心跳处理函数
	OnHeartbeat(handler HeartbeatHandler)
	// OnReceive 监听接收消息
	// @param handler ReceiveHandler 消息接收处理函数
	OnReceive(handler ReceiveHandler)
	// OnDisconnect 监听连接断开
	// @param handler DisconnectHandler 连接关闭处理函数
	OnDisconnect(handler DisconnectHandler)
}

Client 客户端接口

type CloseHandler

type CloseHandler func()

CloseHandler 服务器关闭处理函数

type Conn

type Conn interface {
	// ID 获取连接ID
	// @return @1 int64 连接ID
	ID() int64
	// UID 获取用户ID
	// @return @1 int64 用户ID
	UID() int64
	// Attr 获取属性接口
	// @return @1 Attr 属性接口
	Attr() Attr
	// Bind 绑定用户ID
	// @param uid int64 用户ID
	// @return @1 error 错误信息
	Bind(uid int64) error
	// Unbind 解绑用户ID
	// @return @1 error 错误信息
	Unbind() error
	// Push 低优先级发送消息
	// @param buf buffer.Buffer 消息内容消息发送失败自行控制释放buffer
	// @return @1 error 错误信息
	Push(buf buffer.Buffer) error
	// State 获取连接状态
	// @return @1 ConnState 连接状态
	State() ConnState
	// Close 关闭连接
	// @param force ...bool 是否强制关闭
	// @return @1 error 错误信息
	Close(force ...bool) error
	// LocalIP 获取本地IP
	// @return @1 string 本地IP地址
	// @return @2 error 错误信息
	LocalIP() (string, error)
	// LocalAddr 获取本地地址
	// @return @1 net.Addr 本地地址
	// @return @2 error 错误信息
	LocalAddr() (net.Addr, error)
	// RemoteIP 获取远端IP
	// @return @1 string 远端IP地址
	// @return @2 error 错误信息
	RemoteIP() (string, error)
	// RemoteAddr 获取远端地址
	// @return @1 net.Addr 远端地址
	// @return @2 error 错误信息
	RemoteAddr() (net.Addr, error)
}

Conn 连接接口

type ConnState

type ConnState int32

ConnState 连接状态

const (
	ConnOpened ConnState = iota + 1 // 连接打开
	ConnHanged                      // 连接挂起
	ConnClosed                      // 连接关闭
)

type ConnectHandler

type ConnectHandler func(conn Conn)

ConnectHandler 连接打开处理函数 @param conn Conn 连接

type DisconnectHandler

type DisconnectHandler func(conn Conn)

DisconnectHandler 连接关闭处理函数 @param conn Conn 连接

type HeartbeatHandler added in v2.6.0

type HeartbeatHandler func(conn Conn, heartbeatTime int64)

HeartbeatHandler 心跳处理函数 @param conn Conn 连接 @param heartbeatTime int64 客户端收到服务器发送的心跳时间(纳秒)

type ReceiveHandler

type ReceiveHandler func(conn Conn, buf buffer.Buffer)

ReceiveHandler 消息接收处理函数 @param conn Conn 连接 @param buf buffer.Buffer 消息缓冲区,用户层需自行控制释放点,避免内存泄漏

type Server

type Server interface {
	// Addr 获取监听地址
	// @return @1 string 监听地址
	Addr() string
	// Start 启动服务器
	// @return @1 error 错误信息
	Start() error
	// Stop 关闭服务器
	// @return @1 error 错误信息
	Stop() error
	// Protocol 获取协议名称
	// @return @1 string 协议名称
	Protocol() string
	// OnStart 监听服务器启动
	// @param handler StartHandler 服务器启动处理函数
	OnStart(handler StartHandler)
	// OnStop 监听服务器关闭
	// @param handler CloseHandler 服务器关闭处理函数
	OnStop(handler CloseHandler)
	// OnConnect 监听连接打开
	// @param handler ConnectHandler 连接打开处理函数
	OnConnect(handler ConnectHandler)
	// OnHeartbeat 监听心跳
	// @param handler HeartbeatHandler 心跳处理函数
	OnHeartbeat(handler HeartbeatHandler)
	// OnReceive 监听接收消息
	// @param handler ReceiveHandler 消息接收处理函数
	OnReceive(handler ReceiveHandler)
	// OnDisconnect 监听连接断开
	// @param handler DisconnectHandler 连接关闭处理函数
	OnDisconnect(handler DisconnectHandler)
}

Server 服务器接口

type StartHandler

type StartHandler func()

StartHandler 服务器启动处理函数

Jump to

Keyboard shortcuts

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