-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
187 lines (164 loc) · 4.22 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"encoding/json"
"log"
"time"
"github.com/gorilla/websocket"
)
// Client is a middleman between the websocket connection and the hub.
type Client struct {
// The websocket connection
conn *websocket.Conn
// Outbound messages
send chan []byte
// Marks the client as authenticated
auth bool
}
// MessageType are the types of messages that can be sent/received
type MessageType uint
const (
// NEW creates a new talk
NEW MessageType = iota
// HIDE hides a talk
HIDE
// DELETE deletes a talk
DELETE
// AUTH communicates authentication request/response
AUTH
// SYNC requests a sync of the talks
SYNC
)
// Message is the format of messages sent between the client and server
// Since go doesn't have the strongest type system we pack every message
// into a single struct
type Message struct {
Type MessageType `json:"type"`
New *NewMessage `json:"new,omitempty"`
Hide *HideMessage `json:"hide,omitempty"`
Del *DeleteMessage `json:"delete,omitempty"`
Auth *AuthMessage `json:"auth,omitempty"`
Sync *SyncMessage `json:"sync,omitempty"`
}
// NewMessage gives the client all the information needed to add a new talk to
// the page
type NewMessage struct {
ID uint32 `json:"id"`
Name string `json:"name"`
Talktype TalkType `json:"talktype"`
Description string `json:"description"`
Week string `json:"week"`
}
// HideMessage gives the client the ID of the talk to hide
type HideMessage struct {
ID uint32 `json:"id"`
}
// DeleteMessage gives the client the ID of the talk to delete
type DeleteMessage struct {
ID uint32 `json:"id"`
}
// AuthMessage gives the client the password to authenticate
type AuthMessage struct {
Password string `json:"password"`
}
// SyncMessage starts and caps off a sync
type SyncMessage struct {
Week string `json:"week"`
}
func authenticatedMessage(b bool) []byte {
if b {
return []byte("{\"type\": 3, \"auth\": {\"status\": true}}")
}
return []byte("{\"type\": 3, \"auth\": {\"status\": false}}")
}
func (c *Client) read() {
defer func() {
hub.unregister <- c
c.conn.Close()
}()
for {
// Read from the connection
_, raw, err := c.conn.ReadMessage()
if err != nil {
log.Printf("[WARN] %v", err)
break
}
// Format the message
var message Message
err = json.Unmarshal(raw, &message)
if err != nil {
// Print the message and continue
log.Printf("[WARN] %v", err)
continue
}
switch message.Type {
case NEW, HIDE, DELETE:
// NEW, HIDE, and DELETE need to be serialized through the hub
hub.broadcast <- message
case AUTH:
// AUTH is handled without having to contact the hub
log.Printf("[INFO] Client %v is trying to authenticate", c.conn.RemoteAddr())
c.send <- authenticatedMessage(message.Auth.Password == config.Password)
case SYNC:
// SYNC messages don't need to be serialized, go straight to the db
log.Printf("[INFO] Client %v is requesting a sync", c.conn.RemoteAddr())
for _, talk := range talks.AllTalks(message.Sync.Week) {
var msg Message
if talk.Hidden {
// Send a hide message
msg = Message{
Type: HIDE,
Hide: &HideMessage{
ID: talk.ID,
},
}
} else {
// Send a create message
msg = Message{
Type: NEW,
New: &NewMessage{
ID: talk.ID,
Name: talk.Name,
Talktype: talk.Type,
Description: talk.Description,
Week: talk.Week,
},
}
}
// Send the message
raw, _ := json.Marshal(msg)
c.send <- raw
}
raw, _ := json.Marshal(message)
c.send <- raw
default:
log.Printf("[WARN] Client %v sent an invalid message type", c.conn.RemoteAddr())
}
}
}
func (c *Client) write() {
defer func() {
c.conn.WriteMessage(websocket.CloseMessage, []byte{})
c.conn.Close()
}()
ticker := time.NewTicker(30 * time.Second)
for {
select {
case message, ok := <-c.send:
if !ok {
log.Println("[INFO] Closing connection")
return
}
err := c.conn.WriteMessage(websocket.TextMessage, message)
if err != nil {
log.Printf("[WARN] %v", err)
return
}
case <-ticker.C:
err := c.conn.WriteMessage(websocket.PingMessage, []byte{})
if err != nil {
log.Printf("[WARN] %v", err)
return
}
}
}
}