diff options
Diffstat (limited to 'internal/net')
| -rw-r--r-- | internal/net/server.go | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/internal/net/server.go b/internal/net/server.go index 9da90ea..da1c01b 100644 --- a/internal/net/server.go +++ b/internal/net/server.go @@ -100,10 +100,11 @@ type Server struct { } type Hub struct { - mu sync.Mutex - sessions map[*Session]bool - rooms map[int]map[*Session]bool - onRemove func(*Session) + mu sync.Mutex + sessions map[*Session]bool + rooms map[int]map[*Session]bool + onRemove func(*Session) + onDisconnect func(*Session) } func NewHub() *Hub { @@ -119,6 +120,12 @@ func (h *Hub) OnRemove(cb func(*Session)) { h.onRemove = cb } +func (h *Hub) OnDisconnect(cb func(*Session)) { + h.mu.Lock() + defer h.mu.Unlock() + h.onDisconnect = cb +} + func (h *Hub) Add(s *Session) { h.mu.Lock() defer h.mu.Unlock() @@ -128,13 +135,18 @@ func (h *Hub) Add(s *Session) { func (h *Hub) Remove(s *Session) { h.mu.Lock() - defer h.mu.Unlock() if s.Player != nil && s.State == StateGame && !s.Disconnecting { s.Disconnecting = true s.DisconnectTicks = 10 + onDisconnect := h.onDisconnect + h.mu.Unlock() + if onDisconnect != nil { + onDisconnect(s) + } return } h.hardRemoveLocked(s) + h.mu.Unlock() } func (h *Hub) HardRemove(s *Session) { |
