Documentation
¶
Overview ¶
Package wsconn is a minimal RFC 6455 WebSocket implementation: a client-side Dial, a server-side Accept, and text-message Read/Write/Close on the resulting Conn. Shared by haws (both sides — Dial for the real client, Accept for hawstest's fake server) and zwavejs-go (Dial only), so neither depends on a third-party websocket library. No compression, subprotocol negotiation, or binary-message support — nothing either consumer's JSON-over-text protocol needs.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrConnClosed = errors.New("wsconn: connection closed")
ErrConnClosed is returned by Read once the peer has closed the connection (a close frame was received and the close handshake completed).
Functions ¶
This section is empty.
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is one WebSocket connection, usable from either the client (Dial) or server (Accept) side. A single goroutine may call Read at a time (it isn't reentrant), matching haws's own single-reader-loop design; Write and Close are safe to call concurrently with Read and with each other.
func Accept ¶
Accept upgrades an incoming HTTP request to a server-side WebSocket connection by hijacking the connection. On a malformed or non-upgrade request it writes an HTTP error response itself and returns a non-nil error; the caller shouldn't write to w afterward either way. Used only by hawstest's fake Home Assistant server.
func Dial ¶
Dial opens a client WebSocket connection to urlStr, performing the HTTP upgrade handshake and returning once the connection is ready to Read/Write. It accepts ws(s), http(s), protocol-relative, and bare host URLs. HTTP URLs are converted to their equivalent WebSocket scheme. ctx bounds the whole dial+handshake.
func (*Conn) Close ¶
func (c *Conn) Close(code StatusCode, reason string) error
Close sends a close frame (code and reason, truncated to fit a control frame if needed) and closes the underlying connection. Safe to call more than once, or concurrently with a Read that's mid-close-handshake with the peer — only the first close (whichever happens first) actually writes a frame and closes the socket.
type StatusCode ¶
type StatusCode uint16
StatusCode is a WebSocket close status code (RFC 6455 §7.4).
const ( // StatusNormalClosure indicates a clean, expected close. StatusNormalClosure StatusCode = 1000 // StatusAbnormalClosure has no defined on-the-wire meaning (the RFC // reserves it for reporting a close that never got a close frame at // all) — haws only ever sends it to simulate a dropped connection in // tests (see hawstest.Server.DropConnections). StatusAbnormalClosure StatusCode = 1006 )