-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.go
78 lines (67 loc) · 1.62 KB
/
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
package main
import (
"encoding/json"
"log"
"github.com/google/uuid"
"gorm.io/datatypes"
)
type Hub struct {
boardId int
// Registered clients.
clients map[*Client]bool
// Inbound messages from the clients.
broadcast chan []byte
// Register requests from the clients.
register chan *Client
// Unregister requests from clients.
unregister chan *Client
}
func newHub(boardId int) *Hub {
return &Hub{
boardId: boardId,
broadcast: make(chan []byte),
register: make(chan *Client),
unregister: make(chan *Client),
clients: make(map[*Client]bool),
}
}
type TestMessageAllPointsForUUID struct {
Id uuid.UUID `json:"id"`
Data datatypes.JSON `json:"data"`
Event string `json:"event"`
}
func (h *Hub) run() {
for {
select {
case client := <-h.register:
h.clients[client] = true
// Grab all points from DB and send
var lines []Line
db.Where("board_id = ?", h.boardId).Find(&lines)
for _, l := range lines {
uuidAndPoints := TestMessageAllPointsForUUID{Id: l.Id, Data: l.Points, Event: "New connection"}
jsonMessage, err := json.Marshal(uuidAndPoints)
if err != nil {
log.Printf("Error marshalling uuid and points: %v", err)
break
}
//log.Printf("Sending message to clients %s", jsonMessage)
client.send <- jsonMessage
}
case client := <-h.unregister:
if _, ok := h.clients[client]; ok {
delete(h.clients, client)
close(client.send)
}
case message := <-h.broadcast:
for client := range h.clients {
select {
case client.send <- message:
default:
close(client.send)
delete(h.clients, client)
}
}
}
}
}