-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontext.go
More file actions
73 lines (65 loc) · 2.09 KB
/
Copy pathcontext.go
File metadata and controls
73 lines (65 loc) · 2.09 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
// Copyright 2025 coregx. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package stream
import (
"net/http"
"github.com/coregx/fursy"
"github.com/coregx/stream/sse"
"github.com/coregx/stream/websocket"
)
// SSEUpgrade upgrades HTTP connection to Server-Sent Events.
//
// It performs the SSE upgrade and calls the user's handler with the connection.
//
// The connection is automatically closed when the handler returns.
//
// Example:
//
// return stream.SSEUpgrade(c, func(conn *sse.Conn) error {
// hub.Register(conn)
// defer hub.Unregister(conn)
// <-conn.Done()
// return nil
// })
func SSEUpgrade(c *fursy.Context, handler func(conn *sse.Conn) error) error {
conn, err := sse.UpgradeWithContext(c.Request.Context(), c.Response, c.Request)
if err != nil {
return c.Problem(fursy.InternalServerError("SSE upgrade failed: " + err.Error()))
}
defer func() {
_ = conn.Close() // Error on close is not critical for SSE.
}()
return handler(conn)
}
// WebSocketUpgrade upgrades HTTP connection to WebSocket.
//
// It performs the WebSocket upgrade and calls the user's handler with the connection.
//
// The connection is automatically closed when the handler returns.
//
// Example:
//
// return stream.WebSocketUpgrade(c, func(conn *websocket.Conn) error {
// hub.Register(conn)
// defer hub.Unregister(conn)
// // ... read/write loop
// return nil
// }, opts)
func WebSocketUpgrade(c *fursy.Context, handler func(conn *websocket.Conn) error, opts *websocket.UpgradeOptions) error {
conn, err := websocket.Upgrade(c.Response, c.Request, opts)
if err != nil {
return c.Problem(fursy.NewProblem(http.StatusBadRequest, "WebSocket Upgrade Failed", err.Error()))
}
defer func() {
_ = conn.Close() // Error on close is not critical for WebSocket.
}()
return handler(conn)
}
// init is kept for potential future registration hooks.
//
// Users should call plugins/stream helpers directly:
// - return stream.SSEUpgrade(c, handler)
// - return stream.WebSocketUpgrade(c, handler, opts)
func init() {
}