-
Notifications
You must be signed in to change notification settings - Fork 6
/
hub.go
177 lines (143 loc) · 4.43 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
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
// Portions copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ws
import (
"github.com/goradd/goradd/pkg/log"
"time"
)
// clientMessage is the information that is passed to the client for each message
type clientMessage struct {
Channel string `json:"channel"`
Message string `json:"message"`
}
type subscription struct {
clientID string
channel string
}
const (
// Time allowed to write a message to the peer.
writeWaitDefault = 2 * time.Second
// Time allowed to read the next pong message from the peer.
pongWaitDefault = 60 * time.Second
// Send pings to peer with this period. Must be less than pongWait.
pingPeriodDefault = (pongWaitDefault * 9) / 10
// Maximum message size allowed from peer.
maxMessageSizeDefault = 512
)
type WebSocketHub struct {
// Channels that clients have subscribed to. Each channel points to a map of client IDs
channels map[string]map[string]bool
// Registered clients, keyed by client ID
clients map[string]*Client
// Inbound messages from the clients.
//Broadcast chan []byte
// Register requests from the clients.
register chan *Client
// Unregister requests from clients.
unregister chan *Client
send chan clientMessage
subscribe chan subscription
// Time to wait for a write to complete
WriteWait time.Duration
// Time allowed to read the next pong message from the peer.
PongWait time.Duration
// Send pings to peer with this period. Must be less than pongWait.
PingPeriod time.Duration
// Maximum message size allowed from peer.
MaxMessageSize int64
}
func NewWebSocketHub() *WebSocketHub {
return &WebSocketHub{
//Broadcast: make(chan []byte),
register: make(chan *Client),
unregister: make(chan *Client),
send: make(chan clientMessage),
clients: make(map[string]*Client),
channels: make(map[string]map[string]bool),
subscribe: make(chan subscription),
WriteWait: writeWaitDefault,
PongWait: pongWaitDefault,
PingPeriod: pingPeriodDefault,
MaxMessageSize: maxMessageSizeDefault,
}
}
func (h *WebSocketHub) run() {
for {
select {
case client := <-h.register:
log.FrameworkInfof("New client registering - client ID: %s", client.clientID)
if _, ok := h.clients[client.clientID]; ok {
// The same client is registering again. Unregister first.
h.unregisterClient(client.clientID)
}
h.clients[client.clientID] = client
case client := <-h.unregister:
log.FrameworkInfof("Client unregistering clientID: %s", client.clientID)
h.unregisterClient(client.clientID)
case msg := <-h.send:
if clientIDs, ok := h.channels[msg.Channel]; ok {
log.FrameworkDebugf("Sending to channel %s - %v", msg.Channel, msg.Message)
for clientID := range clientIDs {
if client, ok2 := h.clients[clientID]; ok2 {
client.send <- msg
}
}
} else {
// Sending to a channel that has not been allocated, so ignore it
log.FrameworkDebugf("Could not find channel %s", msg.Channel)
}
case sub := <-h.subscribe:
log.FrameworkInfof("Subscribing to channel %s - %v", sub.clientID, sub.channel)
h.subscribeChannel(sub.clientID, sub.channel)
/* not broadcasting currently. This might change
case message := <-h.Broadcast:
for client := range h.clients {
select {
case client.send <- message: // echo
default:
close(client.send)
delete(h.clients, client)
}
}
*/
}
}
}
func (h *WebSocketHub) unregisterClient(clientID string) {
var client, _ = h.clients[clientID]
if client == nil {
return
}
for channel := range client.channels {
h.unsubscribeChannel(clientID, channel)
}
delete(h.clients, clientID)
}
func (h *WebSocketHub) subscribeChannel(clientID string, channel string) {
var client, _ = h.clients[clientID]
if client == nil {
return
}
client.channels[channel] = true
if clientIDs, ok := h.channels[channel]; !ok {
clientIDs = make(map[string]bool)
clientIDs[clientID] = true
h.channels[channel] = clientIDs
} else {
clientIDs[clientID] = true
}
}
func (h *WebSocketHub) unsubscribeChannel(clientID string, channel string) {
if clientIDs, ok := h.channels[channel]; ok {
delete(clientIDs, clientID)
if len(clientIDs) == 0 {
delete(h.channels, channel)
} else {
h.channels[channel] = clientIDs
}
}
if client, ok := h.clients[clientID]; ok {
delete(client.channels, channel)
}
}