forked from anycable/anycable-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriber.go
42 lines (36 loc) · 855 Bytes
/
subscriber.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
package main
import (
"encoding/json"
"github.com/garyburd/redigo/redis"
"github.com/soveran/redisurl"
)
type Subscriber struct {
host string
channel string
}
func (s *Subscriber) run() {
c, err := redisurl.ConnectToURL(s.host)
if err != nil {
log.Criticalf("failed to subscribe to Redis: %v", err)
return
}
psc := redis.PubSubConn{Conn: c}
psc.Subscribe(s.channel)
for {
switch v := psc.Receive().(type) {
case redis.Message:
log.Debugf("[Redis] channel %s: message: %s\n", v.Channel, v.Data)
msg := &StreamMessage{}
if err := json.Unmarshal(v.Data, &msg); err != nil {
log.Debugf("Unknown message: %s", v.Data)
} else {
log.Debugf("Broadcast %v", msg)
hub.stream_broadcast <- msg
}
case redis.Subscription:
log.Debugf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
case error:
break
}
}
}