/
thunderbird.go
103 lines (86 loc) · 2.11 KB
/
thunderbird.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
package thunderbird
import (
"net/http"
"sync"
"github.com/gorilla/websocket"
)
type Adapter interface {
Subscribe(channel string) error
Unsubscribe(channel string) error
Broadcast(channel string, payload []byte) error
}
func New() *Thunderbird {
return &Thunderbird{
connections: make(map[*Connection]bool),
channelHandlers: make(map[string][]ChannelHandler),
}
}
type Thunderbird struct {
channelHandlers map[string][]ChannelHandler
chanMutex sync.RWMutex
connections map[*Connection]bool
connMutex sync.RWMutex
}
func (tb *Thunderbird) Broadcast(channel, body string) {
tb.connMutex.Lock()
for conn, _ := range tb.connections {
if conn.isSubscribedTo(channel) {
event := Event{
Type: "message",
Channel: channel,
Body: body,
}
conn.send <- event
}
}
tb.connMutex.Unlock()
}
func (tb *Thunderbird) newConnection(ws *websocket.Conn) *Connection {
return &Connection{
tb: tb,
subscriptions: make(map[string]bool),
ws: ws,
send: make(chan Event),
}
}
func (tb *Thunderbird) HTTPHandler() http.Handler {
return &httpHandler{
tb: tb,
upgrader: websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
},
}
}
func (tb *Thunderbird) HTTPHandlerWithUpgrader(upgrader websocket.Upgrader) http.Handler {
return &httpHandler{tb: tb, upgrader: upgrader}
}
func (tb *Thunderbird) connected(c *Connection) {
tb.connMutex.Lock()
tb.connections[c] = true
tb.connMutex.Unlock()
}
func (tb *Thunderbird) subscribed(e Event) {
}
func (tb *Thunderbird) disconnected(c *Connection) {
tb.connMutex.Lock()
if _, ok := tb.connections[c]; ok {
delete(tb.connections, c)
close(c.send)
}
tb.connMutex.Unlock()
}
func (tb *Thunderbird) HandleChannel(channel string, handler ChannelHandler) {
tb.chanMutex.Lock()
tb.channelHandlers[channel] = append(tb.channelHandlers[channel], handler)
tb.chanMutex.Unlock()
}
func (tb *Thunderbird) Channels(channel string) []ChannelHandler {
tb.chanMutex.Lock()
ch := tb.channelHandlers[channel]
tb.chanMutex.Unlock()
return ch
}
type ChannelHandler interface {
Received(Event)
}