-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
44 lines (40 loc) · 805 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
package main
import (
"github.com/gorilla/websocket"
"time"
)
// client: A Chat Client
type client struct {
// client websocket connection
socket *websocket.Conn
// client message channel
send chan *message
// client current chatroom
room *room
// client's userData
userData map[string]interface{}
}
func (c *client) read() {
for {
var msg *message
if err := c.socket.ReadJSON(&msg); err == nil {
msg.SentTime = time.Now()
msg.Sender = c.userData["name"].(string)
if avatarURL, noErr := c.userData["avatar_url"]; noErr {
msg.AvatarURL = avatarURL.(string)
}
c.room.forward <- msg
} else {
break
}
}
c.socket.Close()
}
func (c *client) write() {
for msg := range c.send {
if err := c.socket.WriteJSON(msg); err != nil {
break
}
}
c.socket.Close()
}