-
Notifications
You must be signed in to change notification settings - Fork 0
/
realtime.go
111 lines (79 loc) · 2.16 KB
/
realtime.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
package main
import (
"context"
"net/http"
"github.com/jackc/pgx/v5/pgtype"
)
func (cfg *apiconfig) subscribeTotimeline(w http.ResponseWriter, ctx context.Context, userid pgtype.UUID) {
f, ok := w.(http.Flusher)
if !ok {
respondWithError(w, http.StatusBadRequest, "streaming unsupported")
}
ti := make(chan timeline_item)
cl := &timelineclient{
timeline: ti,
Userid: userid,
}
cfg.Clients.timelineClients.Store(cl, nil)
go func() {
<-ctx.Done()
cfg.Clients.timelineClients.Delete(cl)
close(ti)
}()
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
for item := range ti {
writesse(w, item)
f.Flush()
}
}
func (cfg *apiconfig) subscribeTocomments(w http.ResponseWriter, ctx context.Context, userid pgtype.UUID, proseid pgtype.UUID) {
f, ok := w.(http.Flusher)
if !ok {
respondWithError(w, http.StatusBadRequest, "streaming unsupported")
}
c := make(chan Comment)
cl := &commentclient{
comments: c,
Userid: userid,
Proseid: proseid,
}
cfg.Clients.commentClients.Store(cl, nil)
go func() {
<-ctx.Done()
cfg.Clients.timelineClients.Delete(cl)
close(c)
}()
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
for item := range c {
writesse(w, item)
f.Flush()
}
}
func (cfg *apiconfig) subscribeTonotifications(w http.ResponseWriter, ctx context.Context, userid pgtype.UUID) {
f, ok := w.(http.Flusher)
if !ok {
respondWithError(w, http.StatusBadRequest, "streaming unsupported")
}
n := make(chan Notification)
cl := ¬ificationclient{
notifications: n,
Userid: userid,
}
cfg.Clients.notificationClients.Store(cl, nil)
go func() {
<-ctx.Done()
cfg.Clients.timelineClients.Delete(cl)
close(n)
}()
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
for item := range n {
writesse(w, item)
f.Flush()
}
}