From 18465c61f5d5de8342754fe10c1fe3ecbc3df18c Mon Sep 17 00:00:00 2001 From: joeyak Date: Wed, 13 Mar 2019 21:14:43 -0400 Subject: [PATCH] Make the chatroom send each message individually closes #24 --- chatroom.go | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/chatroom.go b/chatroom.go index 201fdba..75023da 100644 --- a/chatroom.go +++ b/chatroom.go @@ -39,7 +39,7 @@ type ChatRoom struct { //initializing the chatroom func newChatRoom() (*ChatRoom, error) { cr := &ChatRoom{ - queue: make(chan string, 5), + queue: make(chan string, 100), clients: make(map[string]*Client), tempConn: make(map[string]*websocket.Conn), } @@ -349,22 +349,27 @@ func (cr *ChatRoom) UserCount() int { //broadcasting all the messages in the queue in one block func (cr *ChatRoom) BroadCast() { - msgBlock := "" -infLoop: + var msgs []string for { + leave := false select { case m := <-cr.queue: - msgBlock += m + msgs = append(msgs, m) default: - break infLoop + leave = true + } + if leave { + break } } - if len(msgBlock) > 0 { - for _, client := range cr.clients { - client.Send(msgBlock) - } - for _, conn := range cr.tempConn { - connSend(msgBlock, conn) + for _, msg := range msgs { + if len(msg) > 0 { + for _, client := range cr.clients { + client.Send(msg) + } + for _, conn := range cr.tempConn { + connSend(msg, conn) + } } } }