This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
hub.tofix
173 lines (145 loc) · 3.85 KB
/
hub.tofix
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
package staticbackend
import (
"fmt"
"strings"
"github.com/gbrlsnchs/jwt/v3"
"github.com/gorilla/websocket"
"github.com/staticbackendhq/core/backend"
"github.com/staticbackendhq/core/cache"
"github.com/staticbackendhq/core/model"
)
// Hub maintains the set of active clients and broadcasts messages to the
// clients.
type Hub struct {
// Registered clients.
sockets map[*Socket]string
// Reverse ID => socket
ids map[string]*Socket
// Socket's subscribed channels
channels map[*Socket][]chan bool
// Inbound messages from the clients.
broadcast chan model.Command
// Register requests from the clients.
register chan *Socket
// Unregister requests from clients.
unregister chan *Socket
// Cache used for keys and pub/sub (Redis)
volatile cache.Volatilizer
}
func newHub(c cache.Volatilizer) *Hub {
return &Hub{
broadcast: make(chan model.Command),
register: make(chan *Socket),
unregister: make(chan *Socket),
sockets: make(map[*Socket]string),
ids: make(map[string]*Socket),
channels: make(map[*Socket][]chan bool),
volatile: c,
}
}
func (h *Hub) run() {
for {
select {
case sck := <-h.register:
h.sockets[sck] = sck.id
h.ids[sck.id] = sck
cmd := model.Command{
Type: "init",
Data: sck.id,
}
sck.send <- cmd
case sck := <-h.unregister:
if _, ok := h.sockets[sck]; ok {
h.unsub(sck)
delete(h.sockets, sck)
delete(h.ids, sck.id)
delete(h.channels, sck)
//time.AfterFunc(500*time.Millisecond, func() {
close(sck.send)
//})
}
case msg := <-h.broadcast:
sockets, p := h.getTargets(msg)
for _, sck := range sockets {
select {
case sck.send <- p:
default:
h.unsub(sck)
close(sck.send)
delete(h.ids, msg.SID)
delete(h.sockets, sck)
delete(h.channels, sck)
}
}
}
}
}
func (h *Hub) getTargets(msg model.Command) (sockets []*Socket, payload model.Command) {
sender, ok := h.ids[msg.SID]
if !ok {
return
}
switch msg.Type {
case model.MsgTypeEcho:
sockets = append(sockets, sender)
payload = msg
payload.Data = "echo: " + msg.Data
case model.MsgTypeAuth:
sockets = append(sockets, sender)
var pl model.JWTPayload
if _, err := jwt.Verify([]byte(msg.Data), model.HashSecret, &pl); err != nil {
payload = model.Command{Type: model.MsgTypeError, Data: "invalid token"}
return
}
var a model.Auth
if err := backend.Cache.GetTyped(pl.Token, &a); err != nil {
payload = model.Command{Type: model.MsgTypeError, Data: "invalid token"}
} else {
payload = model.Command{Type: model.MsgTypeToken, Data: pl.Token}
}
case model.MsgTypeJoin:
subs, ok := h.channels[sender]
if !ok {
subs = make([]chan bool, 0)
}
closeSubChan := make(chan bool)
subs = append(subs, closeSubChan)
go h.volatile.Subscribe(sender.send, msg.Token, msg.Data, closeSubChan)
sockets = append(sockets, sender)
payload = model.Command{Type: model.MsgTypeJoined, Data: msg.Data}
case model.MsgTypeChanIn:
sockets = append(sockets, sender)
if len(msg.Channel) == 0 {
payload = model.Command{Type: model.MsgTypeError, Data: "no channel was specified"}
return
} else if strings.HasPrefix(strings.ToLower(msg.Channel), "db-") {
payload = model.Command{
Type: model.MsgTypeError,
Data: "you cannot write to database channel",
}
return
}
if err := h.volatile.Publish(msg); err != nil {
payload = model.Command{Type: model.MsgTypeError, Data: "unable to send your message"}
return
}
payload = model.Command{Type: model.MsgTypeOk}
default:
sockets = append(sockets, sender)
payload.Type = model.MsgTypeError
payload.Data = fmt.Sprintf(`%s command not found`, msg.Type)
}
return
}
func (h *Hub) join(scksck *websocket.Conn, channel string) {
}
func (h *Hub) unsub(sck *Socket) {
subs, ok := h.channels[sck]
if !ok {
return
}
for _, sub := range subs {
sub <- true
close(sub)
}
}