mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
Merge pull request #1110 from eryx/master
Fix panic error in concurrent calling with websocket.Connection.Emit() Former-commit-id: d9c1ab5ca244abded583885d418c556d5f9cf409
This commit is contained in:
commit
22ec02d53d
|
@ -52,9 +52,9 @@ type (
|
||||||
func New(cfg Config) *Server {
|
func New(cfg Config) *Server {
|
||||||
cfg = cfg.Validate()
|
cfg = cfg.Validate()
|
||||||
return &Server{
|
return &Server{
|
||||||
config: cfg,
|
config: cfg,
|
||||||
connections: sync.Map{}, // ready-to-use, this is not necessary.
|
connections: sync.Map{}, // ready-to-use, this is not necessary.
|
||||||
rooms: make(map[string][]string),
|
rooms: make(map[string][]string),
|
||||||
onConnectionListeners: make([]ConnectionFunc, 0),
|
onConnectionListeners: make([]ConnectionFunc, 0),
|
||||||
upgrader: websocket.Upgrader{
|
upgrader: websocket.Upgrader{
|
||||||
HandshakeTimeout: cfg.HandshakeTimeout,
|
HandshakeTimeout: cfg.HandshakeTimeout,
|
||||||
|
@ -352,9 +352,12 @@ func (s *Server) GetConnectionsByRoom(roomName string) []Connection {
|
||||||
// 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 {
|
if to != All && to != Broadcast {
|
||||||
if s.rooms[to] != nil {
|
s.mu.RLock()
|
||||||
|
room := s.rooms[to]
|
||||||
|
s.mu.RUnlock()
|
||||||
|
if room != nil {
|
||||||
// it suppose to send the message to a specific room/or a user inside its own room
|
// it suppose to send the message to a specific room/or a user inside its own room
|
||||||
for _, connectionIDInsideRoom := range s.rooms[to] {
|
for _, connectionIDInsideRoom := range room {
|
||||||
if c, ok := s.getConnection(connectionIDInsideRoom); ok {
|
if c, ok := s.getConnection(connectionIDInsideRoom); ok {
|
||||||
c.writeDefault(data) //send the message to the client(s)
|
c.writeDefault(data) //send the message to the client(s)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user