Skip to content

Commit

Permalink
Set nickname on clients
Browse files Browse the repository at this point in the history
  • Loading branch information
lakinduakash committed May 22, 2019
1 parent a1b6157 commit cccbfc2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
25 changes: 18 additions & 7 deletions websocket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package websocket

import (
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"log"
)
Expand All @@ -20,9 +19,10 @@ type Message struct {
}

type MessageBody struct {
From string `json:"from"`
To string `json:"to"`
Message string `json:"message"`
From string `json:"from"`
To string `json:"to"`
Message string `json:"message"`
Nickname string `json:"nickname"`
}

func (c *Client) Read() {
Expand All @@ -44,9 +44,20 @@ func (c *Client) Read() {
log.Println(err)
continue
}
body.From = c.ID
message := Message{Type: 1, Body: body}

var message Message

if body.Nickname != "" {
c.Nickname = body.Nickname
body.From = c.ID
message = Message{Type: 4, Body: body}
} else {
body.From = c.ID
message = Message{Type: 1, Body: body}
}

c.Pool.Broadcast <- &message
fmt.Printf("Message Received: %+v\n", message)

//fmt.Printf("Message Received: %+v\n", message)
}
}
19 changes: 19 additions & 0 deletions websocket/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ func (pool *Pool) Start() {
}

}

for _, client2 := range pool.Clients {
if err := client.Conn.WriteJSON(Message{Type: 2, Body: MessageBody{Message: client2.ID, Nickname: client2.Nickname}}); err != nil {
log.Fatal("Error on write")
continue
}

}

break
case client := <-pool.Unregister:
delete(pool.Clients, client.ID)
Expand All @@ -71,6 +80,16 @@ func (pool *Pool) Start() {
fmt.Println(err)
}
}
} else if message.Type == 4 {

for _, client := range pool.Clients {

if err := client.Conn.WriteJSON(*message); err != nil {
fmt.Println(err)
continue
}

}
} else {
//fmt.Println("Sending message to all clients in Pool")
for _, client := range pool.Clients {
Expand Down

0 comments on commit cccbfc2

Please sign in to comment.