连接管理

1. 方法

GetConn() *net.TCPConn //获取当前连接绑定的conn
GetID() uint32 //获取当前连接的id(note: 这里是连接的ID,不是消息的ID)
RemoteAddr() net.Addr //获取远程客户端信息
Send(id uint32, data []byte) error //发送数据
//连接属性,每个连接都有其专属的连接属性,里面可以存储任何数据
SetProperty(key string, val interface{}) 
GetProperty(key string) (interface{}, error)
DelProperty(key string)

在每个连接请求成功后,框架会自动生成连接ID,并为每个连接绑定连接管理模块

2. 连接Hook

在每个连接开始运作和停止运作都会运行一个Hook

//连接开始时
s.OnStart(func(c knet.IConnection) {
    c.SetProperty("name", "keing")
})
//连接结束时
s.OnStop(func(c knet.IConnection) {
    fmt.Println(c.GetProperty("name"))
})

results matching ""

    No results matching ""