-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add broadcaster to notify clients and message changes
- Loading branch information
1 parent
485fe84
commit 63fe20c
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package websocket | ||
|
||
import "sync" | ||
|
||
var mutex1 sync.Mutex | ||
var mutex2 sync.Mutex | ||
|
||
type MessageBCastChannel struct { | ||
clientList []chan Message | ||
} | ||
|
||
type ClientBCastChannel struct { | ||
clientList []chan Client | ||
} | ||
|
||
func NewMessageBCastChannel(size int) *MessageBCastChannel { | ||
return &MessageBCastChannel{clientList: make([]chan Message, size)} | ||
} | ||
|
||
func NewClientBCastChannel(size int) *ClientBCastChannel { | ||
return &ClientBCastChannel{clientList: make([]chan Client, size)} | ||
} | ||
|
||
func (bc *MessageBCastChannel) AddWorker(c chan Message) { | ||
mutex1.Lock() | ||
bc.clientList = append(bc.clientList, c) | ||
mutex1.Unlock() | ||
} | ||
|
||
func (bc *ClientBCastChannel) AddWorker(c chan Client) { | ||
mutex2.Lock() | ||
bc.clientList = append(bc.clientList, c) | ||
mutex2.Unlock() | ||
} | ||
|
||
func (bc *MessageBCastChannel) BroadCast(data Message) { | ||
mutex1.Lock() | ||
for _, v := range bc.clientList { | ||
if v != nil { | ||
go func() { | ||
v <- data | ||
}() | ||
} | ||
} | ||
mutex1.Unlock() | ||
} | ||
|
||
func (bc *ClientBCastChannel) BroadCast(data Client) { | ||
mutex2.Lock() | ||
for _, v := range bc.clientList { | ||
if v != nil { | ||
go func() { | ||
v <- data | ||
}() | ||
} | ||
} | ||
mutex2.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters