Skip to content

Commit

Permalink
update private functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lakinduakash committed May 22, 2019
1 parent 5299be6 commit 336cf47
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,21 @@ func GetClients() map[string]*websocket.Client {
return pool.GetClients()
}

//This function will notify when new user is registered to chat server
func ListenClientAddChanges() chan websocket.Client {
c := make(chan websocket.Client)
websocket.CBR.AddWorker(c)
return c
}

//This function will notify when a user is unregiterd from chat server
func ListenClientRemoveChanges() chan websocket.Client {
c := make(chan websocket.Client)
websocket.CBU.AddWorker(c)
return c
}

//This function will notify when new message is arrived to chat server
func ListenMessageChanges() chan websocket.Message {
c := make(chan websocket.Message)
websocket.MB.AddWorker(c)
Expand Down
8 changes: 4 additions & 4 deletions websocket/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ type ClientBCastChannel struct {
clientList []chan Client
}

func NewMessageBCastChannel(size int) *MessageBCastChannel {
func newMessageBCastChannel(size int) *MessageBCastChannel {
return &MessageBCastChannel{clientList: make([]chan Message, size)}
}

func NewClientBCastChannel(size int) *ClientBCastChannel {
func newClientBCastChannel(size int) *ClientBCastChannel {
return &ClientBCastChannel{clientList: make([]chan Client, size)}
}

Expand All @@ -33,7 +33,7 @@ func (bc *ClientBCastChannel) AddWorker(c chan Client) {
mutex2.Unlock()
}

func (bc *MessageBCastChannel) BroadCast(data Message) {
func (bc *MessageBCastChannel) broadCast(data Message) {
mutex1.Lock()
for _, v := range bc.clientList {
if v != nil {
Expand All @@ -45,7 +45,7 @@ func (bc *MessageBCastChannel) BroadCast(data Message) {
mutex1.Unlock()
}

func (bc *ClientBCastChannel) BroadCast(data Client) {
func (bc *ClientBCastChannel) broadCast(data Client) {
mutex2.Lock()
for _, v := range bc.clientList {
if v != nil {
Expand Down
12 changes: 6 additions & 6 deletions websocket/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func NewPool() *Pool {

}

var CBR = NewClientBCastChannel(2)
var CBU = NewClientBCastChannel(2)
var MB = NewMessageBCastChannel(2)
var CBR = newClientBCastChannel(2)
var CBU = newClientBCastChannel(2)
var MB = newMessageBCastChannel(2)

func (pool *Pool) GetClients() map[string]*Client {
return pool.Clients
Expand All @@ -39,7 +39,7 @@ func (pool *Pool) Start() {
pool.Clients[client.ID] = client
//fmt.Println("New user", client.ID)
//fmt.Println("Size of Connection Pool: ", len(pool.Clients))
CBR.BroadCast(*client)
CBR.broadCast(*client)

for _, client2 := range pool.Clients {
if err := client2.Conn.WriteJSON(Message{Type: 2, Body: MessageBody{Message: client.ID}}); err != nil {
Expand All @@ -52,7 +52,7 @@ func (pool *Pool) Start() {
case client := <-pool.Unregister:
delete(pool.Clients, client.ID)
//fmt.Println("Size of Connection Pool: ", len(pool.Clients))
CBU.BroadCast(*client)
CBU.broadCast(*client)
for _, client := range pool.Clients {
if err := client.Conn.WriteJSON(Message{Type: 3, Body: MessageBody{Message: client.ID}}); err != nil {
log.Fatal("Error on write")
Expand All @@ -62,7 +62,7 @@ func (pool *Pool) Start() {
break
case message := <-pool.Broadcast:

MB.BroadCast(*message)
MB.broadCast(*message)
if message.Body.To != "" {
//fmt.Println("Sending message to", message.Body.To)

Expand Down

0 comments on commit 336cf47

Please sign in to comment.