-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
59 lines (50 loc) · 1.12 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"time"
"github.com/gorilla/websocket"
"github.com/deven96/gosock/pkg/custlog"
)
func init(){
// log file flag
defwriters := custlog.DefaultWriters(*LogFile, true)
//TRACE will be Discarded, while the rest will be routed accordingly
custlog.LogInit(defwriters)
}
type client struct {
// websocket per client
socket *websocket.Conn
// send is a channel on which messages are sent
send chan *message
// room is the room this client is chatting on
room *room
// color assigned to the client
color string
}
// read from the websocket
func (c *client) read(){
defer c.socket.Close()
for {
var msg *message
err := c.socket.ReadJSON(&msg)
if err != nil {
custlog.Trace.Println(err)
return
}
// send msg to the forward channel of the client
custlog.Info.Printf("Message received from %s: %s", c.color, msg.Message)
msg.When = time.Now()
msg.Code = c.color
c.room.forward <- msg
}
}
// write to the websocket
func (c *client) write(){
defer c.socket.Close()
for msg := range c.send {
err := c.socket.WriteJSON(msg)
if err != nil {
custlog.Error.Println(err)
return
}
}
}