-
Notifications
You must be signed in to change notification settings - Fork 1
/
room.go
150 lines (119 loc) · 3.01 KB
/
room.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
package main
import (
"encoding/json"
"fmt"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/lib/pq"
log "github.com/sirupsen/logrus"
)
const (
POSTGRES_CONNECT = "postgres://superuser:superuser@127.0.0.1:5432/fuckthesuits?sslmode=disable"
)
var (
project = &Project{}
)
func reportPostgresProblem(ev pq.ListenerEventType, err error) {
if err != nil {
fmt.Println("Postgres NOTIFY error: " + err.Error())
}
}
func initPostgresListener(tableName string) *pq.Listener {
listener := pq.NewListener(POSTGRES_CONNECT, 10*time.Second, time.Minute,
reportPostgresProblem)
err := listener.Listen(tableName)
if err != nil {
panic(err)
}
log.Printf("LISTENing for all changes to the `%s` table to push to WebSocket clients",
tableName)
return listener
}
func waitForNotification(tableName string) {
listener := initPostgresListener(tableName)
for {
select {
case n := <-listener.Notify:
if n == nil {
fmt.Printf("Got nil notification on table `%v`! Postgres may have crashed, or we just reconnected\n", tableName)
time.Sleep(1 * time.Second)
continue
}
fmt.Println("Received data from channel [", n.Channel, "] :", n.Extra)
project.BroadcastMessages(nil, Message(n.Extra))
case <-time.After(90 * time.Second):
go func() {
listener.Ping()
}()
}
}
}
func NewProject() *Project {
tables := []string{
// TODO(elimisteve): Add fuckthesuits DB table names here
}
for _, tableName := range tables {
tableName := tableName
go waitForNotification(tableName)
}
return &Project{}
}
type Project struct {
Clients []*Client
clientLock sync.RWMutex
}
func (p *Project) AddClient(c *Client) {
p.clientLock.Lock()
defer p.clientLock.Unlock()
p.Clients = append(p.Clients, c)
}
func (p *Project) RemoveClient(c *Client) {
p.clientLock.Lock()
defer p.clientLock.Unlock()
for i, client := range p.Clients {
if client == c {
p.Clients = append(p.Clients[:i], p.Clients[i+1:]...)
break
}
}
}
// If it is a message from the project, make the sender nil.
func (p *Project) BroadcastMessages(sender *Client, msgs ...Message) {
p.clientLock.RLock()
defer p.clientLock.RUnlock()
for _, client := range p.Clients {
go func(client *Client) {
err := client.SendMessages(msgs...)
if err != nil {
log.Debugf("Error sending message. Err: %s", err)
}
}(client)
}
}
type Client struct {
wsConn *websocket.Conn
writeLock sync.Mutex
project *Project
}
func (c *Client) SendMessages(msgs ...Message) error {
c.writeLock.Lock()
defer c.writeLock.Unlock()
outgoing := OutgoingPayload{Changes: msgs}
body, err := json.Marshal(outgoing)
if err != nil {
return err
}
err = c.wsConn.WriteMessage(websocket.TextMessage, body)
if err != nil {
log.Debugf("Error sending message to client. Removing client from project. Err: %s", err)
c.project.RemoveClient(c)
return err
}
return nil
}
func (c *Client) SendError(errStr string, secretErr error) error {
c.writeLock.Lock()
defer c.writeLock.Unlock()
return WSWriteError(c.wsConn, errStr, secretErr)
}