0

Pass along just the client id instead a reference to the entire client when unregistering the client

This commit is contained in:
Gabe Kangas 2021-07-26 17:30:28 -07:00
parent f07c9e2e00
commit f85b54cfeb
3 changed files with 7 additions and 7 deletions

View File

@ -25,7 +25,7 @@ func GetChatMessages(w http.ResponseWriter, r *http.Request) {
messages := chat.GetChatHistory() messages := chat.GetChatHistory()
if err := json.NewEncoder(w).Encode(messages); err != nil { if err := json.NewEncoder(w).Encode(messages); err != nil {
log.Errorln(err) log.Debugln(err)
} }
default: default:
w.WriteHeader(http.StatusNotImplemented) w.WriteHeader(http.StatusNotImplemented)

View File

@ -155,7 +155,7 @@ func (c *ChatClient) close() {
log.Traceln("client closed:", c.User.DisplayName, c.id, c.ipAddress) log.Traceln("client closed:", c.User.DisplayName, c.id, c.ipAddress)
c.conn.Close() c.conn.Close()
c.server.unregister <- c c.server.unregister <- c.id
if c.send != nil { if c.send != nil {
close(c.send) close(c.send)
c.send = nil c.send = nil

View File

@ -32,7 +32,7 @@ type ChatServer struct {
inbound chan chatClientEvent inbound chan chatClientEvent
// unregister requests from clients. // unregister requests from clients.
unregister chan *ChatClient unregister chan uint // the ChatClient id
} }
func NewChat() *ChatServer { func NewChat() *ChatServer {
@ -40,7 +40,7 @@ func NewChat() *ChatServer {
clients: map[uint]*ChatClient{}, clients: map[uint]*ChatClient{},
outbound: make(chan []byte), outbound: make(chan []byte),
inbound: make(chan chatClientEvent), inbound: make(chan chatClientEvent),
unregister: make(chan *ChatClient), unregister: make(chan uint),
maxClientCount: handleMaxConnectionCount(), maxClientCount: handleMaxConnectionCount(),
} }
@ -50,10 +50,10 @@ func NewChat() *ChatServer {
func (s *ChatServer) Run() { func (s *ChatServer) Run() {
for { for {
select { select {
case client := <-s.unregister: case clientId := <-s.unregister:
if _, ok := s.clients[client.id]; ok { if _, ok := s.clients[clientId]; ok {
s.mu.Lock() s.mu.Lock()
delete(s.clients, client.id) delete(s.clients, clientId)
s.mu.Unlock() s.mu.Unlock()
} }