tws

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	HandshakeTimeout: 5 * time.Second,

	TCPTimeout: 30 * time.Second,

	PingInterval: 30 * time.Second,
	RequirePong:  true,
}

DefaultConfig is the default Config value.

View Source
var StreamerConfig = func() Config {
	config := DefaultConfig
	config.RequirePong = false
	return config
}()

StreamerConfig is the recommended configuration for high-traffic protocols where participants cannot be expected to be responsive all the time.

Functions

func Dial

func Dial(ctx context.Context, url string, headers http.Header, config Config, sessionFn SessionFn) error

Dial connects to WebSocket server and executes the interaction scenario described by the session function.

func Serve

func Serve(w http.ResponseWriter, r *http.Request, config Config, sessionFn SessionFn)

Serve handles an HTTP request by upgrading the connection to WebSocket and executing the interaction scenario described by the session function.

The context passed into the session function is a descendant of the request context.

func TuneTCP

func TuneTCP(conn net.Conn, config Config) error

TuneTCP configures ACk timeouts on TCP level.

func WithWSScheme

func WithWSScheme(addr string) string

WithWSScheme changes http to ws and https to wss.

Types

type Config

type Config struct {
	// Timeout for the WebSocket protocol upgrade
	HandshakeTimeout time.Duration

	// Disconnect when an outgoing packet is not acknowledged for this long.
	// 0 for kernel default.
	TCPTimeout time.Duration

	// Send pings this often. 0 to disable.
	PingInterval time.Duration

	// Disconnect if a pong doesn't arrive during PingInterval
	RequirePong bool
}

Config is the WebSocket configuration.

type SessionFn

type SessionFn func(ctx context.Context, incoming <-chan []byte, outgoing chan<- []byte) error

SessionFn is a function that implements a WebSocket interaction scenario.

The function receives incoming messages through one channel and sends outgoing messages through another. Both the incoming channel and the context will be closed when the connection closes. Once the session function returns, the connection will be closed if it's still open. If the session function returns nil, the closure will be graceul: the incoming channel will be drained first. If the session function returns an error, the connection will be closed immediately.

Jump to

Keyboard shortcuts

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