-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrawing-hub.go
151 lines (124 loc) · 2.65 KB
/
drawing-hub.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"math/rand"
"time"
"go.uber.org/multierr"
)
type DrawingHub struct {
idCount int64
lastX int
dataBuf []Message
colors map[string]int64
users map[int64]*User
read chan Message
close chan error
}
func (h *DrawingHub) Open() {
log.Println("Hub started listening")
go h.listen()
}
func (h *DrawingHub) Close(ctx context.Context) error {
close(h.read)
select {
case err := <-h.close:
return err
case <-ctx.Done():
return ctx.Err()
}
}
func (h *DrawingHub) listen() {
defer func() {
log.Println("Hub is closing")
h.close <- h.closeAll()
}()
for msg := range h.read {
switch msg.Kind() {
case TypeUserJoined:
h.idCount++
log.Printf("User: %d joined", h.idCount)
// generate unique color.
color := genRandomColorHex()
_, ok := h.colors[color]
for ok {
color = genRandomColorHex()
_, ok = h.colors[color]
}
// populate user.
pMsg := msg.(*MessageUserJoined)
user := pMsg.User
user.Id = h.idCount
user.Color = color
user.commSend = h.read
// add user to hub.
h.users[user.Id] = user
h.colors[color] = user.Id
// notify client side of their color and id.
msg := newFirstMessage(user.Id, user.Color, h.dataBuf)
payload, _ := json.Marshal(msg)
user.write(payload)
// start user.
user.Start()
case TypeUserLeft:
pMsg := msg.(*MessageUserLeft)
userId := pMsg.Id
user := h.users[userId]
log.Printf("User: %d is leaving\n", userId)
delete(h.users, userId)
delete(h.colors, user.Color)
case TypePointStart, TypePoint, TypePointEnd:
log.Printf("Drawing: %d From user: %d", msg.Kind(), msg.SenderId())
h.saveMessage(msg)
h.broadcast(msg, msg.SenderId())
}
}
}
func (h *DrawingHub) saveMessage(msg Message) {
if msg.Kind() != TypeDraw {
return
}
if h.lastX == cap(h.dataBuf) {
h.lastX = 0
}
h.dataBuf[h.lastX] = msg
h.lastX++
}
func (h *DrawingHub) broadcast(msg Message, sender int64) {
payload, _ := json.Marshal(msg)
for _, user := range h.users {
if user.Id != sender {
user.write(payload)
}
}
}
func (h *DrawingHub) closeAll() error {
var err error
for _, user := range h.users {
if err := user.conn.Close(); err != nil {
multierr.AppendInto(&err, err)
}
}
return err
}
func genRandomColorHex() string {
rand.Seed(time.Now().Unix())
r := rand.Intn(255)
g := rand.Intn(255)
b := rand.Intn(255)
hexR := fmt.Sprintf("%x", r)
if len(hexR) == 1 {
hexR = "0" + hexR
}
hexG := fmt.Sprintf("%x", g)
if len(hexG) == 1 {
hexG = "0" + hexG
}
hexB := fmt.Sprintf("%x", b)
if len(hexB) == 1 {
hexB = "0" + hexB
}
return "#" + hexR + hexG + hexB
}