Skip to content

Commit 422dcae

Browse files
committed
Remove session from subscribed room on disconnect
1 parent 429d776 commit 422dcae

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

nsocket.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ func (nsoc *NSocket) unsubscribe(namespace string, sess *melody.Session) {
127127
nsoc.rwMutex.Unlock()
128128
}
129129

130+
func (nsoc *NSocket) unsubscribeFromAll(sess *melody.Session) {
131+
nsoc.rwMutex.Lock()
132+
for i, v := range nsoc.namespaces {
133+
tmp := []*melody.Session{}
134+
for _, s := range v {
135+
if s == sess || s.IsClosed() {
136+
s.Close()
137+
continue
138+
}
139+
tmp = append(tmp, s)
140+
}
141+
nsoc.namespaces[i] = tmp
142+
}
143+
nsoc.rwMutex.Unlock()
144+
}
145+
130146
func New(config Config) *NSocket {
131147
nsoc := &NSocket{
132148
rwMutex: sync.RWMutex{},
@@ -182,13 +198,15 @@ func New(config Config) *NSocket {
182198

183199
nsoc.melody.HandleDisconnect(func(s *melody.Session) {
184200
fmt.Printf("Client :%v Disconnected\n", s.RemoteAddr())
201+
nsoc.unsubscribeFromAll(s)
185202
events := nsoc.config.Namespace[Default]
186203
f, ok := events[OnNamespaceDisconnect]
187204
if !ok {
188205
return
189206
}
190207
f(s, nil, nsoc)
191208
})
209+
192210
if config.AllowedOrigins != nil {
193211
nsoc.melody.Upgrader.CheckOrigin = func(r *http.Request) (ok bool) {
194212
origin := r.Header.Get("origin")

0 commit comments

Comments
 (0)