-
Notifications
You must be signed in to change notification settings - Fork 52
/
websocket.go
170 lines (145 loc) · 5.31 KB
/
websocket.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"context"
"fmt"
socketio "github.com/googollee/go-socket.io"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"io"
"log"
"net/http"
"strings"
)
// ws handler
var sockets = make(map[string]socketio.Conn)
var listen = make(chan string)
var unlisten = make(chan string)
func websocket(dbpool *pgxpool.Pool) *socketio.Server {
wsServer := socketio.NewServer(nil)
wsServer.OnConnect("/", func(s socketio.Conn) error {
log.Println("wsServer connected", s.ID())
return nil
})
wsServer.OnEvent("/", "attach", func(s socketio.Conn, sessionId string) string {
if sessionId == "null" {
log.Println("wsServer attach received `null` as sessionId")
return "err"
}
log.Println("wsServer attaching", sessionId)
s.Emit("event", fmt.Sprintf("{\"type\": \"attached\", \"sessionId\": \"%s\"}", sessionId))
sockets[sessionId] = s
listen <- sessionId
return "ok"
})
sendEvent := func(s socketio.Conn, event string) {
s.Emit("event", fmt.Sprintf("{\"type\": \"event\", \"data\": %s}", string(event)))
}
go func(pool *pgxpool.Pool) {
// Need to acquire a connection that will LISTEN on this sessionId
cn, err := pool.Acquire(context.Background())
if err != nil {
log.Println("wsServer could not acquire persistent connection: ", err)
return
}
defer cn.Release()
done := make(chan bool)
var cancel func()
start := func(cn *pgx.Conn) {
done = make(chan bool)
cancelctx, cncl := context.WithCancel(context.Background())
cancel = cncl
for {
notification, er := cn.WaitForNotification(cancelctx)
// WaitForNotification on a loop - blocking
if er != nil {
log.Println("wsServer notification error: ", er)
break
} else {
log.Printf("wsServer notification %#v\n", notification.Channel)
sessionId := notification.Channel
sendEvent(sockets[sessionId], string(notification.Payload))
}
}
close(done)
}
for {
select {
case sessionId := <-listen:
if cancel != nil {
cancel()
cancel = nil
// wait until done cancelling
<-done
}
// listen
_, err = cn.Exec(context.Background(), fmt.Sprintf("listen \"%s\"", sessionId))
if err != nil {
log.Println("wsServer error calling listen: ", err)
return
}
// start wait process
go start(cn.Conn())
// select from event.event and publish those
rows, err := pool.Query(context.Background(), "select event from event.event where session_id=$1;", sessionId)
if err != nil {
fmt.Println("wsServer error reading queued events:", err)
}
for rows.Next() {
var event string
err := rows.Scan(&event)
if err != nil {
fmt.Println("wsServer error scanning queued event:", err)
continue
}
log.Printf("wsServer event.event: %#v\n", event)
sendEvent(sockets[sessionId], event)
}
rows.Close()
case sessionId := <-unlisten:
if cancel != nil {
cancel()
cancel = nil
// wait until done cancelling
<-done
}
// unlisten
_, err = cn.Exec(context.Background(), fmt.Sprintf("unlisten \"%s\"", sessionId))
if err != nil {
log.Println("wsServer error calling unlisten: ", err)
}
// start wait process
go start(cn.Conn())
_, err := pool.Exec(context.Background(), "delete from event.session where id=$1;", sessionId)
if err != nil {
fmt.Println("wsServer error deleting old session:", err)
}
}
}
}(dbpool)
wsServer.OnError("/", func(_ socketio.Conn, e error) {
// event.session_detach?
log.Println("wsServer error:", e)
})
wsServer.OnDisconnect("/", func(_ socketio.Conn, reason string) {
// event.session_detach?
log.Println("wsServer closed", reason)
})
// serve websocket
go func() {
if err := wsServer.Serve(); err != nil {
log.Fatalf("wsServer socketio listen error: %s\n", err)
}
}()
return wsServer
}
func websocketDetach(w http.ResponseWriter, req *http.Request) {
// /_socket/detach/${sessionId}
s := strings.SplitN(req.URL.Path, "/", 4)
sessionId := s[3]
log.Println("wsServer detaching", sessionId)
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(200)
io.WriteString(w, "")
delete(sockets, sessionId)
unlisten <- sessionId
}