From 0db2afea93d163f92b6f6fa673d816daf2f0e759 Mon Sep 17 00:00:00 2001 From: Gerasimos Maropoulos Date: Wed, 2 May 2018 17:47:14 +0300 Subject: [PATCH] websocket: fix a bug on emit on specific room when it doesn't exist it sends to all connections caused by a third-party contributor's PR... Former-commit-id: de6fddadd7cef8537ad1d1aff1acd991e4cf99cf --- websocket/connection.go | 1 + websocket/server.go | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/websocket/connection.go b/websocket/connection.go index 08defffe..748f8067 100644 --- a/websocket/connection.go +++ b/websocket/connection.go @@ -511,6 +511,7 @@ func (c *connection) To(to string) Emitter { } else if to == c.id { return c.self } + // is an emitter to another client/connection return newEmitter(c, to) } diff --git a/websocket/server.go b/websocket/server.go index 44120b90..b8f0d487 100644 --- a/websocket/server.go +++ b/websocket/server.go @@ -375,18 +375,20 @@ func (s *Server) GetConnectionsByRoom(roomName string) []Connection { // You SHOULD use connection.EmitMessage/Emit/To().Emit/EmitMessage instead. // let's keep it unexported for the best. func (s *Server) emitMessage(from, to string, data []byte) { - if to != All && to != Broadcast && s.rooms[to] != nil { - // it suppose to send the message to a specific room/or a user inside its own room - for _, connectionIDInsideRoom := range s.rooms[to] { - if c := s.connections.get(connectionIDInsideRoom); c != nil { - c.writeDefault(data) //send the message to the client(s) - } else { - // the connection is not connected but it's inside the room, we remove it on disconnect but for ANY CASE: - cid := connectionIDInsideRoom - if c != nil { - cid = c.id + if to != All && to != Broadcast { + if s.rooms[to] != nil { + // it suppose to send the message to a specific room/or a user inside its own room + for _, connectionIDInsideRoom := range s.rooms[to] { + if c := s.connections.get(connectionIDInsideRoom); c != nil { + c.writeDefault(data) //send the message to the client(s) + } else { + // the connection is not connected but it's inside the room, we remove it on disconnect but for ANY CASE: + cid := connectionIDInsideRoom + if c != nil { + cid = c.id + } + s.Leave(cid, to) } - s.Leave(cid, to) } } } else {