feat(live): just wanted to checkout how far I got on the websocket data source

This commit is contained in:
Torkel Ödegaard
2016-12-20 16:09:04 +01:00
parent e81e4ad06f
commit b7827962dc
6 changed files with 29 additions and 21 deletions

View File

@@ -7,6 +7,7 @@ import (
)
type hub struct {
log log.Logger
connections map[*connection]bool
streams map[string]map[*connection]bool
@@ -29,10 +30,10 @@ var h = hub{
unregister: make(chan *connection),
streamChannel: make(chan *dtos.StreamMessage),
subChannel: make(chan *streamSubscription),
log: log.New("live.hub"),
}
func (h *hub) removeConnection() {
}
func (h *hub) run() {
@@ -40,17 +41,17 @@ func (h *hub) run() {
select {
case c := <-h.register:
h.connections[c] = true
log.Info("Live: New connection (Total count: %v)", len(h.connections))
h.log.Info("New connection", "total", len(h.connections))
case c := <-h.unregister:
if _, ok := h.connections[c]; ok {
log.Info("Live: Closing Connection (Total count: %v)", len(h.connections))
h.log.Info("Closing connection", "total", len(h.connections))
delete(h.connections, c)
close(c.send)
}
// hand stream subscriptions
case sub := <-h.subChannel:
log.Info("Live: Subscribing to: %v, remove: %v", sub.name, sub.remove)
h.log.Info("Subscribing", "channel", sub.name, "remove", sub.remove)
subscribers, exists := h.streams[sub.name]
// handle unsubscribe
@@ -63,13 +64,14 @@ func (h *hub) run() {
subscribers = make(map[*connection]bool)
h.streams[sub.name] = subscribers
}
subscribers[sub.conn] = true
// handle stream messages
case message := <-h.streamChannel:
subscribers, exists := h.streams[message.Stream]
if !exists || len(subscribers) == 0 {
log.Info("Live: Message to stream without subscribers: %v", message.Stream)
h.log.Info("Message to stream without subscribers", "stream", message.Stream)
continue
}