Documentation
¶
Overview ¶
Package ws provides a lightweight WebSocket helper built on top of golang.org/x/net/websocket (stdlib-compatible, no CGO).
For bidirectional real-time communication between server and frontend clients. For one-way server→client streams, use pkg/sse instead.
Usage:
func chatHandler(w http.ResponseWriter, r *http.Request) {
ws.Handle(w, r, func(conn *ws.Conn) {
for {
var msg ws.Message
if err := conn.Recv(&msg); err != nil {
return
}
conn.Send(ws.Message{Type: "echo", Data: msg.Data})
}
})
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrUpgradeFailed = errors.New("ws: upgrade failed")
ErrUpgradeFailed is returned when the HTTP→WebSocket upgrade fails.
Functions ¶
func Handle ¶
func Handle(w http.ResponseWriter, r *http.Request, fn Handler)
Handle upgrades the HTTP connection to WebSocket and calls fn. It uses the standard HTTP hijack mechanism via golang.org/x/net/websocket semantics but implemented over net/http for zero extra dependencies.
The connection is closed automatically when fn returns.
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn wraps the underlying connection with typed send/receive helpers.
Click to show internal directories.
Click to hide internal directories.