-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathsocket.go
More file actions
320 lines (279 loc) · 7.35 KB
/
socket.go
File metadata and controls
320 lines (279 loc) · 7.35 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package live
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"slices"
"sync"
"time"
"github.com/coder/websocket"
"github.com/rs/xid"
"golang.org/x/net/html"
)
const (
// maxMessageBufferSize the maximum number of messages per socket in a buffer.
maxMessageBufferSize = 16
// cookieSocketID name for a cookie which holds the current socket ID.
cookieSocketID = "_psid"
// infiniteTTL
infiniteTTL = 10_000 * (24 * time.Hour)
)
type SocketID string
// Socket describes a socket from the outside.
type Socket struct {
id SocketID
engine *Engine
connected bool
currentRender *html.Node
renderMu sync.Mutex
msgs chan Event
closeSlow func()
uploadMu sync.Mutex
uploadConfigs []*UploadConfig
uploads UploadContext
selfChan chan socketSelfOp
}
// Render executes the template, computes a diff against the previous render,
// sends any patches to the client, and updates the stored render tree.
// It is safe for concurrent use.
func (s *Socket) Render(ctx context.Context) error {
s.renderMu.Lock()
defer s.renderMu.Unlock()
render, err := renderSocket(ctx, s.engine, s)
if err != nil {
return err
}
s.updateRender(render)
return nil
}
type socketSelfOp struct {
Event Event
resp chan bool
err chan error
}
// NewID returns a new ID.
func NewID() string {
return xid.New().String()
}
// NewSocketFromRequest creates a new default socket from a request.
func NewSocketFromRequest(ctx context.Context, e *Engine, r *http.Request) (*Socket, error) {
sockID, err := socketIDFromReq(r)
if err != nil {
return nil, fmt.Errorf("socket id not found: %w", err)
}
existingSock, err := e.GetSocket(sockID)
if err == nil {
return existingSock, nil
}
return NewSocket(ctx, e, sockID), nil
}
func socketIDFromReq(r *http.Request) (SocketID, error) {
c, err := r.Cookie(cookieSocketID)
if err == nil {
return SocketID(c.Value), nil
}
v := r.FormValue(cookieSocketID)
if v != "" {
return SocketID(v), nil
}
return "", fmt.Errorf("socket id not found in cookie or form data")
}
// NewSocket creates a new default socket.
func NewSocket(ctx context.Context, e *Engine, withID SocketID) *Socket {
s := &Socket{
id: withID,
engine: e,
connected: withID != "",
closeSlow: func() {},
uploadConfigs: []*UploadConfig{},
msgs: make(chan Event, maxMessageBufferSize),
selfChan: make(chan socketSelfOp),
}
if withID == "" {
s.id = SocketID(NewID())
}
go s.operate(ctx)
return s
}
func (s *Socket) WriteFlashCookie(w http.ResponseWriter) {
http.SetCookie(w, &http.Cookie{
Name: cookieSocketID,
Value: string(s.id),
Path: "/",
HttpOnly: false,
SameSite: http.SameSiteStrictMode,
MaxAge: 1,
})
}
// ID gets the socket ID.
func (s *Socket) ID() SocketID {
return s.id
}
// Assigns returns the data currently assigned to this
// socket.
func (s *Socket) Assigns() any {
state, _ := s.engine.socketStateStore.Get(s.id)
return state.Data
}
// Assign sets data to this socket. This will happen automatically
// if you return data from an `EventHander`.
func (s *Socket) Assign(data any) {
state, _ := s.engine.socketStateStore.Get(s.id)
state.Data = data
ttl := 10 * time.Second
if s.connected {
ttl = infiniteTTL
}
s.engine.socketStateStore.Set(s.id, state, ttl)
}
// Connected returns if this socket is connected via the websocket.
func (s *Socket) Connected() bool {
return s.connected
}
// Self sends an event to this socket itself. Will be handled in the
// handlers HandleSelf function.
func (s *Socket) Self(ctx context.Context, event string, data any) error {
op := socketSelfOp{
Event: Event{T: event, SelfData: data},
resp: make(chan bool),
err: make(chan error),
}
s.selfChan <- op
select {
case <-op.resp:
return nil
case err := <-op.err:
return err
}
}
func (s *Socket) operate(ctx context.Context) {
for {
select {
case op := <-s.selfChan:
s.engine.self(ctx, s, op.Event)
op.resp <- true
case <-ctx.Done():
return
}
}
}
// Broadcast sends an event to all sockets on this same engine.
func (s *Socket) Broadcast(event string, data any) error {
return s.engine.Broadcast(event, data)
}
// Send an event to this socket's client, to be handled there.
func (s *Socket) Send(event string, data any, options ...EventConfig) error {
payload, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("could not encode data for send: %w", err)
}
msg := Event{T: event, Data: payload}
for _, o := range options {
if err := o(&msg); err != nil {
return fmt.Errorf("could not configure event: %w", err)
}
}
select {
case s.msgs <- msg:
default:
go s.closeSlow()
}
return nil
}
// PatchURL sends an event to the client to update the
// query params in the URL.
func (s *Socket) PatchURL(values url.Values) {
s.Send(EventParams, values.Encode())
}
// Redirect sends a redirect event to the client. This will trigger the browser to
// redirect to a URL.
func (s *Socket) Redirect(u *url.URL) {
s.Send(EventRedirect, u.String())
}
// AllowUploads indicates that his socket should accept uploads.
func (s *Socket) AllowUploads(config *UploadConfig) {
s.uploadMu.Lock()
defer s.uploadMu.Unlock()
s.uploadConfigs = append(s.uploadConfigs, config)
}
// UploadConfigs returns the configs for this socket.
func (s *Socket) UploadConfigs() []*UploadConfig {
s.uploadMu.Lock()
defer s.uploadMu.Unlock()
out := make([]*UploadConfig, len(s.uploadConfigs))
copy(out, s.uploadConfigs)
return out
}
// Uploads returns the sockets uploads.
func (s *Socket) Uploads() UploadContext {
s.uploadMu.Lock()
defer s.uploadMu.Unlock()
out := make(UploadContext, len(s.uploads))
for k, v := range s.uploads {
cp := make([]*Upload, len(v))
copy(cp, v)
out[k] = cp
}
return out
}
// AssignUpload sets uploads to this socket.
func (s *Socket) AssignUpload(config string, upload *Upload) {
s.uploadMu.Lock()
defer s.uploadMu.Unlock()
if s.uploads == nil {
s.uploads = map[string][]*Upload{}
}
if _, ok := s.uploads[config]; !ok {
s.uploads[config] = []*Upload{}
}
for idx, u := range s.uploads[config] {
if u.Name == upload.Name {
s.uploads[config][idx] = upload
return
}
}
s.uploads[config] = append(s.uploads[config], upload)
}
// ClearUploads clears this sockets upload map.
func (s *Socket) ClearUploads() {
s.uploadMu.Lock()
defer s.uploadMu.Unlock()
s.uploads = map[string][]*Upload{}
}
// ClearUpload clears a specific upload from this socket.
func (s *Socket) ClearUpload(config string, upload *Upload) {
s.uploadMu.Lock()
defer s.uploadMu.Unlock()
if s.uploads == nil {
return
}
if _, ok := s.uploads[config]; !ok {
return
}
for idx, u := range s.uploads[config] {
if u.Name == upload.Name {
s.uploads[config] = slices.Delete(s.uploads[config], idx, idx+1)
return
}
}
}
// latestRender returns the last render result of this socket.
func (s *Socket) latestRender() *html.Node {
return s.currentRender
}
// updateRender replaces the last render result of this socket.
func (s *Socket) updateRender(render *html.Node) {
s.currentRender = render
}
// Messages returns a channel of event messages sent and received by this socket.
func (s *Socket) Messages() chan Event {
return s.msgs
}
// assignWS connect a web socket to a socket.
func (s *Socket) assignWS(ws *websocket.Conn) {
s.closeSlow = func() {
ws.Close(websocket.StatusPolicyViolation, "socket too slow to keep up with messages")
}
}