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
This commit is contained in:
Gerasimos Maropoulos 2018-05-02 17:47:14 +03:00
parent 087c8c8b3a
commit 0db2afea93
2 changed files with 14 additions and 11 deletions

View File

@ -511,6 +511,7 @@ func (c *connection) To(to string) Emitter {
} else if to == c.id { } else if to == c.id {
return c.self return c.self
} }
// is an emitter to another client/connection // is an emitter to another client/connection
return newEmitter(c, to) return newEmitter(c, to)
} }

View File

@ -375,18 +375,20 @@ func (s *Server) GetConnectionsByRoom(roomName string) []Connection {
// You SHOULD use connection.EmitMessage/Emit/To().Emit/EmitMessage instead. // You SHOULD use connection.EmitMessage/Emit/To().Emit/EmitMessage instead.
// let's keep it unexported for the best. // let's keep it unexported for the best.
func (s *Server) emitMessage(from, to string, data []byte) { func (s *Server) emitMessage(from, to string, data []byte) {
if to != All && to != Broadcast && s.rooms[to] != nil { if to != All && to != Broadcast {
// it suppose to send the message to a specific room/or a user inside its own room if s.rooms[to] != nil {
for _, connectionIDInsideRoom := range s.rooms[to] { // it suppose to send the message to a specific room/or a user inside its own room
if c := s.connections.get(connectionIDInsideRoom); c != nil { for _, connectionIDInsideRoom := range s.rooms[to] {
c.writeDefault(data) //send the message to the client(s) if c := s.connections.get(connectionIDInsideRoom); c != nil {
} else { c.writeDefault(data) //send the message to the client(s)
// the connection is not connected but it's inside the room, we remove it on disconnect but for ANY CASE: } else {
cid := connectionIDInsideRoom // the connection is not connected but it's inside the room, we remove it on disconnect but for ANY CASE:
if c != nil { cid := connectionIDInsideRoom
cid = c.id if c != nil {
cid = c.id
}
s.Leave(cid, to)
} }
s.Leave(cid, to)
} }
} }
} else { } else {