Documentation
¶
Index ¶
- Variables
- func Dial(ctx context.Context, url string, headers http.Header, config Config, ...) error
- func Serve(w http.ResponseWriter, r *http.Request, config Config, sessionFn SessionFn)
- func TuneTCP(conn net.Conn, config Config) error
- func WithWSScheme(addr string) string
- type Config
- type SessionFn
Constants ¶
This section is empty.
Variables ¶
var DefaultConfig = Config{ HandshakeTimeout: 5 * time.Second, TCPTimeout: 30 * time.Second, PingInterval: 30 * time.Second, RequirePong: true, }
DefaultConfig is the default Config value.
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 ¶
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 WithWSScheme ¶
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 ¶
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.