Make the chatroom send each message individually

closes #24
This commit is contained in:
joeyak 2019-03-13 21:14:43 -04:00
parent 1cd490b04a
commit 18465c61f5
1 changed files with 16 additions and 11 deletions

View File

@ -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)
}
}
}
}