wip: 构思新的event系统。
parent
3bc9c095df
commit
e9bb90ba8c
@ -0,0 +1 @@
|
||||
package nnet
|
@ -0,0 +1,14 @@
|
||||
package config
|
||||
|
||||
type (
|
||||
WSEvent struct {
|
||||
// 连接成功回调
|
||||
OnConnected func()
|
||||
// 连接异常回调,在准备进行连接的过程中发生异常时触发
|
||||
OnConnectError func(err error)
|
||||
// 连接断开回调,网络异常,服务端掉线等情况时触发
|
||||
OnDisconnected func(err error)
|
||||
// 连接关闭回调,服务端发起关闭信号或客户端主动关闭时触发
|
||||
OnClose func(code int, text string)
|
||||
}
|
||||
)
|
@ -0,0 +1,39 @@
|
||||
package event
|
||||
|
||||
import "git.noahlan.cn/noahlan/nnet/connection"
|
||||
|
||||
type (
|
||||
ConnFn func(conn *connection.Connection)
|
||||
ErrFn func(err error)
|
||||
|
||||
// ConnEvents 连接事件
|
||||
ConnEvents interface {
|
||||
// OnConnected 连接成功回调
|
||||
OnConnected(h ConnFn)
|
||||
// OnConnectError 连接异常回调, 在准备进行连接的过程中发生异常时触发
|
||||
OnConnectError(err error)
|
||||
// OnDisconnected 连接断开回调,网络异常,服务端掉线等情况时触发
|
||||
OnDisconnected(conn *connection.Connection, err error)
|
||||
// OnClose 连接关闭回调,服务端发起关闭信号或客户端主动关闭时触发
|
||||
OnClose(details any, err error)
|
||||
}
|
||||
|
||||
// MessageEvents 消息事件
|
||||
MessageEvents interface {
|
||||
// OnSentError 发送消息异常回调
|
||||
OnSentError(details any, err error)
|
||||
// OnReceiveError 接收消息异常回调
|
||||
OnReceiveError(details any, err error)
|
||||
}
|
||||
)
|
||||
|
||||
type Manager struct {
|
||||
ConnEvents
|
||||
MessageEvents
|
||||
|
||||
onConnected []OnConnectedFn
|
||||
}
|
||||
|
||||
func NewEventManager() *Manager {
|
||||
return &Manager{}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package packet
|
||||
|
||||
// Entry 入口原始数据
|
||||
type Entry struct {
|
||||
Header any
|
||||
Raw []byte
|
||||
}
|
||||
|
||||
func NewEntry(header any, raw []byte) *Entry {
|
||||
return &Entry{
|
||||
Header: header,
|
||||
Raw: raw,
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package packet
|
||||
|
||||
type (
|
||||
// Packer 数据帧 封包/解包
|
||||
Packer interface {
|
||||
// Pack 封包,将原始数据构造为二进制流数据帧
|
||||
Pack(header any, data []byte) ([]byte, error)
|
||||
|
||||
// Unpack 解包
|
||||
Unpack(header any, data []byte) ([]IPacket, error)
|
||||
}
|
||||
|
||||
// PackerBuilder Packer构建器
|
||||
PackerBuilder func() Packer
|
||||
)
|
Loading…
Reference in New Issue