fix: 修复ws-heartbeat的eventtype illegal 错误

main v1.2.2
NoahLan 1 year ago
parent 58d5c8f8e9
commit 570946dcea

@ -31,20 +31,7 @@ func WithHeartbeatWS(interval time.Duration, hbdFn WsHeartbeatFn) nnet.RunOption
panic("dataFn must not be nil") panic("dataFn must not be nil")
} }
return func(ngin *nnet.Engine) { var startFn event.OnConnectedFn = func(nc *conn.Connection) {
ngin.EventManager().RegisterEvent(event.EvtOnConnected, m.start)
ngin.Use(func(next rt.HandlerFunc) rt.HandlerFunc {
return func(conn *conn.Connection, pkg packet.IPacket) {
m.handle(conn, pkg)
next(conn, pkg)
}
})
}
}
func (m *HeartbeatWsMiddleware) start(nc *conn.Connection) {
ticker := time.NewTicker(m.interval) ticker := time.NewTicker(m.interval)
defer func() { defer func() {
@ -73,6 +60,19 @@ func (m *HeartbeatWsMiddleware) start(nc *conn.Connection) {
} }
} }
return func(ngin *nnet.Engine) {
ngin.EventManager().RegisterEvent(event.EvtOnConnected, startFn)
ngin.Use(func(next rt.HandlerFunc) rt.HandlerFunc {
return func(conn *conn.Connection, pkg packet.IPacket) {
m.handle(conn, pkg)
next(conn, pkg)
}
})
}
}
func (m *HeartbeatWsMiddleware) handle(_ *conn.Connection, _ packet.IPacket) { func (m *HeartbeatWsMiddleware) handle(_ *conn.Connection, _ packet.IPacket) {
atomic.StoreInt64(&m.lastAt, time.Now().Unix()) atomic.StoreInt64(&m.lastAt, time.Now().Unix())
} }

Loading…
Cancel
Save