Move broadcast logic to be self contained in the function

This commit is contained in:
joeyak 2019-03-15 18:57:50 -04:00
parent a3d3c170c9
commit bc90744798
1 changed files with 6 additions and 17 deletions

View File

@ -56,12 +56,7 @@ func newChatRoom() (*ChatRoom, error) {
fmt.Printf("Loaded %d emotes\n", num) fmt.Printf("Loaded %d emotes\n", num)
//the "heartbeat" for broadcasting messages //the "heartbeat" for broadcasting messages
go func() { go cr.BroadCast()
for {
cr.BroadCast()
time.Sleep(50 * time.Millisecond)
}
}()
return cr, nil return cr, nil
} }
@ -370,8 +365,7 @@ func (cr *ChatRoom) UserCount() int {
//broadcasting all the messages in the queue in one block //broadcasting all the messages in the queue in one block
func (cr *ChatRoom) BroadCast() { func (cr *ChatRoom) BroadCast() {
running := true for {
for running {
cr.clientsMtx.Lock() cr.clientsMtx.Lock()
select { select {
case msg := <-cr.queue: case msg := <-cr.queue:
@ -381,14 +375,6 @@ func (cr *ChatRoom) BroadCast() {
for _, conn := range cr.tempConn { for _, conn := range cr.tempConn {
conn.WriteJSON(msg) conn.WriteJSON(msg)
} }
default:
// No messages to send
// This default block is required so the above case
// does not block.
}
// Mod queue
select {
case msg := <-cr.modqueue: case msg := <-cr.modqueue:
for _, client := range cr.clients { for _, client := range cr.clients {
if client.IsMod || client.IsAdmin { if client.IsMod || client.IsAdmin {
@ -396,7 +382,10 @@ func (cr *ChatRoom) BroadCast() {
} }
} }
default: default:
running = false time.Sleep(50 * time.Millisecond)
// No messages to send
// This default block is required so the above case
// does not block.
} }
cr.clientsMtx.Unlock() cr.clientsMtx.Unlock()
} }