Documentation
¶
Overview ¶
Package websocket upgrades HTTP connections to WebSocket connections and manages the resulting connection. It has no business logic: callers write their own read loop, decide what messages mean, and own any application concepts (rooms, hubs, auth) on top of the raw Conn.
WebSocket.Upgrade performs the HTTP-to-WebSocket handshake and returns a Conn. Conn.ReadMessage blocks for the next inbound frame and must be used from a single goroutine. Write, WriteMessage, and WriteJSON are safe for concurrent use: a mutex serializes them onto the underlying connection and each call blocks until its frame is written or WriteTimeout elapses. A background goroutine pings the peer every PingInterval; a peer that stops answering trips the read deadline, which surfaces as an error from ReadMessage.
Basic usage:
ws, err := websocket.New(&websocket.Config{AllowedOrigins: []string{"https://example.com"}})
if err != nil {
log.Fatal(err)
}
e.GET("/ws", func(c echo.Context) error {
conn, err := ws.Upgrade(c.Response(), c.Request())
if err != nil {
return err
}
defer conn.Close()
for {
_, data, err := conn.ReadMessage()
if err != nil {
return nil
}
// caller decides what data means
}
})
Index ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
ReadBufferSize int
WriteBufferSize int
PingInterval time.Duration
PingTimeout time.Duration
WriteTimeout time.Duration
ReadLimit int64
AllowedOrigins []string
}