ws

package
v0.13.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package ws implements RFC 6455 WebSocket framing with zero external dependencies. Only the server side (upgrade + read/write frames).

This is a minimal, auditable implementation — ~200 LOC. It handles text frames, close frames, and ping/pong. Fragmentation is not supported (every frame is FIN=true), which is fine for JSON messages.

Index

Constants

View Source
const (
	OpText   = 0x1
	OpBinary = 0x2
	OpClose  = 0x8
	OpPing   = 0x9
	OpPong   = 0xA
)

Opcodes.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

Conn wraps a net.Conn with WebSocket framing.

func Dial

func Dial(rawurl string) (*Conn, *http.Response, error)

Dial connects to a WebSocket server. Used for testing.

func Upgrade

func Upgrade(w http.ResponseWriter, r *http.Request) (*Conn, error)

Upgrade performs the WebSocket upgrade handshake and returns a Conn. The ResponseWriter is hijacked — do not use it after calling Upgrade.

func (*Conn) Close

func (c *Conn) Close() error

Close sends a close frame and then closes the underlying connection.

func (*Conn) ReadMessage

func (c *Conn) ReadMessage() (opcode int, payload []byte, err error)

ReadMessage reads a complete message from the WebSocket connection. Returns the opcode and payload. For text messages (opcode 0x1), the payload is a UTF-8 string. This is a blocking call.

func (*Conn) WriteMessage

func (c *Conn) WriteMessage(opcode int, data []byte) error

WriteMessage sends a text or binary message. For text messages, pass OpText and a valid UTF-8 byte slice.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL