-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
48 lines (42 loc) · 909 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
46
47
48
package main
import (
"github.com/gorilla/websocket"
"log"
)
/* To figure out if they wanna broadcast to all or broadcast to all except them */
type Message struct {
mtype string
msg []byte
}
/* Reads and writes messages from client */
type Client struct {
conn *websocket.Conn
out chan Message
}
/* Reads and pumps to out channel */
func (c *Client) ReadLoop() {
defer close(c.out)
for {
_, message, err := c.conn.ReadMessage()
if err != nil {
// log.Println("read:", err)
break
}
msg := Message{"ex", message}
c.out <- msg
}
}
/* Writes a message to the client */
func (c *Client) WriteMessage(msg []byte) {
err := c.conn.WriteMessage(websocket.TextMessage, msg)
if err != nil {
log.Println("write:", err)
}
}
/* Constructor */
func NewClient(conn *websocket.Conn) *Client {
client := new(Client)
client.conn = conn
client.out = make(chan Message)
return client
}