-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
45 lines (40 loc) · 889 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"time"
"github.com/gorilla/websocket"
)
// client represents a single chatting user
type client struct {
// socket is the web socket for this client
socket *websocket.Conn
// send is a channel on which messages are sent
send chan *message
// room is the room this client is chattng in.
room *room
// userData holds information about the user
userData map[string]interface{}
}
func (c *client) read() {
defer c.socket.Close()
for {
var msg *message
if err := c.socket.ReadJSON(&msg); err != nil {
return
}
msg.When = time.Now()
msg.Name = c.userData["name"].(string)
if avatarURL, ok := c.userData["avatar_url"]; ok {
msg.AvatarURL = avatarURL.(string)
}
c.room.forward <- msg
}
}
func (c *client) write() {
defer c.socket.Close()
for msg := range c.send {
err := c.socket.WriteJSON(msg)
if err != nil {
break
}
}
}