|
|
|
@ -31,20 +31,7 @@ func WithHeartbeatWS(interval time.Duration, hbdFn WsHeartbeatFn) nnet.RunOption
|
|
|
|
|
panic("dataFn must not be nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return func(ngin *nnet.Engine) {
|
|
|
|
|
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) {
|
|
|
|
|
var startFn event.OnConnectedFn = func(nc *conn.Connection) {
|
|
|
|
|
ticker := time.NewTicker(m.interval)
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
atomic.StoreInt64(&m.lastAt, time.Now().Unix())
|
|
|
|
|
}
|
|
|
|
|