|
@@ -17,8 +17,9 @@ type hub struct {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type streamSubscription struct {
|
|
type streamSubscription struct {
|
|
|
- conn *connection
|
|
|
|
|
- name string
|
|
|
|
|
|
|
+ conn *connection
|
|
|
|
|
+ name string
|
|
|
|
|
+ remove bool
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
var h = hub{
|
|
var h = hub{
|
|
@@ -40,15 +41,24 @@ func (h *hub) run() {
|
|
|
case c := <-h.register:
|
|
case c := <-h.register:
|
|
|
h.connections[c] = true
|
|
h.connections[c] = true
|
|
|
log.Info("Live: New connection (Total count: %v)", len(h.connections))
|
|
log.Info("Live: New connection (Total count: %v)", len(h.connections))
|
|
|
|
|
+
|
|
|
case c := <-h.unregister:
|
|
case c := <-h.unregister:
|
|
|
if _, ok := h.connections[c]; ok {
|
|
if _, ok := h.connections[c]; ok {
|
|
|
|
|
+ log.Info("Live: Closing Connection (Total count: %v)", len(h.connections))
|
|
|
delete(h.connections, c)
|
|
delete(h.connections, c)
|
|
|
close(c.send)
|
|
close(c.send)
|
|
|
}
|
|
}
|
|
|
// hand stream subscriptions
|
|
// hand stream subscriptions
|
|
|
case sub := <-h.subChannel:
|
|
case sub := <-h.subChannel:
|
|
|
- log.Info("Live: Connection subscribing to: %v", sub.name)
|
|
|
|
|
|
|
+ log.Info("Live: Subscribing to: %v, remove: %v", sub.name, sub.remove)
|
|
|
subscribers, exists := h.streams[sub.name]
|
|
subscribers, exists := h.streams[sub.name]
|
|
|
|
|
+
|
|
|
|
|
+ // handle unsubscribe
|
|
|
|
|
+ if exists && sub.remove {
|
|
|
|
|
+ delete(subscribers, sub.conn)
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if !exists {
|
|
if !exists {
|
|
|
subscribers = make(map[*connection]bool)
|
|
subscribers = make(map[*connection]bool)
|
|
|
h.streams[sub.name] = subscribers
|
|
h.streams[sub.name] = subscribers
|
|
@@ -58,13 +68,19 @@ func (h *hub) run() {
|
|
|
// handle stream messages
|
|
// handle stream messages
|
|
|
case message := <-h.streamChannel:
|
|
case message := <-h.streamChannel:
|
|
|
subscribers, exists := h.streams[message.Stream]
|
|
subscribers, exists := h.streams[message.Stream]
|
|
|
- if !exists {
|
|
|
|
|
|
|
+ if !exists || len(subscribers) == 0 {
|
|
|
log.Info("Live: Message to stream without subscribers: %v", message.Stream)
|
|
log.Info("Live: Message to stream without subscribers: %v", message.Stream)
|
|
|
continue
|
|
continue
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
messageBytes, _ := simplejson.NewFromAny(message).Encode()
|
|
messageBytes, _ := simplejson.NewFromAny(message).Encode()
|
|
|
for sub := range subscribers {
|
|
for sub := range subscribers {
|
|
|
|
|
+ // check if channel is open
|
|
|
|
|
+ if _, ok := h.connections[sub]; !ok {
|
|
|
|
|
+ delete(subscribers, sub)
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
select {
|
|
select {
|
|
|
case sub.send <- messageBytes:
|
|
case sub.send <- messageBytes:
|
|
|
default:
|
|
default:
|