|
|
|
@ -2,6 +2,7 @@ package chat
@@ -2,6 +2,7 @@ package chat
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"encoding/json" |
|
|
|
|
"fmt" |
|
|
|
|
"net/http" |
|
|
|
|
"sync" |
|
|
|
|
"time" |
|
|
|
@ -71,6 +72,7 @@ func (s *Server) Run() {
@@ -71,6 +72,7 @@ func (s *Server) Run() {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
case message := <-s.inbound: |
|
|
|
|
fmt.Println("inbound...") |
|
|
|
|
s.eventReceived(message) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -214,21 +216,31 @@ func (s *Server) Broadcast(payload events.EventPayload) error {
@@ -214,21 +216,31 @@ func (s *Server) Broadcast(payload events.EventPayload) error {
|
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
s.mu.Lock() |
|
|
|
|
defer s.mu.Unlock() |
|
|
|
|
go func() { |
|
|
|
|
s.mu.Lock() |
|
|
|
|
defer s.mu.Unlock() |
|
|
|
|
|
|
|
|
|
for _, client := range s.clients { |
|
|
|
|
if client == nil { |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
defer func() { |
|
|
|
|
if a := recover(); a != nil { |
|
|
|
|
fmt.Println("RECOVER", a) |
|
|
|
|
} |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
select { |
|
|
|
|
case client.send <- data: |
|
|
|
|
default: |
|
|
|
|
client.close() |
|
|
|
|
delete(s.clients, client.id) |
|
|
|
|
for _, client := range s.clients { |
|
|
|
|
if client == nil { |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if client.send != nil { |
|
|
|
|
select { |
|
|
|
|
case client.send <- data: |
|
|
|
|
default: |
|
|
|
|
client.close() |
|
|
|
|
delete(s.clients, client.id) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
@ -241,7 +253,9 @@ func (s *Server) Send(payload events.EventPayload, client *Client) {
@@ -241,7 +253,9 @@ func (s *Server) Send(payload events.EventPayload, client *Client) {
|
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
client.send <- data |
|
|
|
|
if client.send != nil { |
|
|
|
|
client.send <- data |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// DisconnectUser will forcefully disconnect all clients belonging to a user by ID.
|
|
|
|
|