You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
639 B
Go
41 lines
639 B
Go
package core
|
|
|
|
type (
|
|
LifetimeHandler func(conn *Connection)
|
|
|
|
lifetime struct {
|
|
onOpen []LifetimeHandler
|
|
onClosed []LifetimeHandler
|
|
}
|
|
)
|
|
|
|
var Lifetime = &lifetime{}
|
|
|
|
func (lt *lifetime) OnClosed(h LifetimeHandler) {
|
|
lt.onClosed = append(lt.onClosed, h)
|
|
}
|
|
|
|
func (lt *lifetime) OnOpen(h LifetimeHandler) {
|
|
lt.onOpen = append(lt.onOpen, h)
|
|
}
|
|
|
|
func (lt *lifetime) Open(conn *Connection) {
|
|
if len(lt.onOpen) <= 0 {
|
|
return
|
|
}
|
|
|
|
for _, handler := range lt.onOpen {
|
|
handler(conn)
|
|
}
|
|
}
|
|
|
|
func (lt *lifetime) Close(conn *Connection) {
|
|
if len(lt.onClosed) <= 0 {
|
|
return
|
|
}
|
|
|
|
for _, handler := range lt.onClosed {
|
|
handler(conn)
|
|
}
|
|
}
|