wsutil

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package wsutil provides abstractions around the Websocket, including rate limits.

Index

Constants

View Source
const CopyBufferSize = 2048
View Source
const DefaultTimeout = time.Minute

Variables

View Source
var CloseDeadline = time.Second

CloseDeadline controls the deadline to wait for sending the Close frame.

Functions

func InjectValues added in v0.2.0

func InjectValues(rawurl string, values url.Values) string

func NewDialLimiter

func NewDialLimiter() *rate.Limiter

func NewGlobalIdentityLimiter

func NewGlobalIdentityLimiter() *rate.Limiter

func NewIdentityLimiter

func NewIdentityLimiter() *rate.Limiter

func NewSendLimiter

func NewSendLimiter() *rate.Limiter

Types

type Conn

type Conn struct {
	Conn *websocket.Conn
	json.Driver
	// contains filtered or unexported fields
}

Conn is the default Websocket connection. It compresses all payloads using zlib.

func NewConn

func NewConn(driver json.Driver) *Conn

func (*Conn) Close

func (c *Conn) Close(code int) error

func (*Conn) Dial

func (c *Conn) Dial(ctx context.Context, addr string) error

func (*Conn) Listen

func (c *Conn) Listen() <-chan Event

func (*Conn) Send

func (c *Conn) Send(b []byte) error

type Connection

type Connection interface {
	// Dial dials the address (string). Context needs to be passed in for
	// timeout. This method should also be re-usable after Close is called.
	Dial(context.Context, string) error

	// Listen sends over events constantly. Error will be non-nil if Data is
	// nil, so check for Error first.
	Listen() <-chan Event

	// Send allows the caller to send bytes. Thread safety is a requirement.
	Send([]byte) error

	// Close should close the websocket connection. The connection will not be
	// reused. Code should be sent as the status code for the close frame.
	Close(code int) error
}

Connection is an interface that abstracts around a generic Websocket driver. This connection expects the driver to handle compression by itself, including modifying the connection URL.

type Event

type Event struct {
	Data []byte

	// Error is non-nil if Data is nil.
	Error error
}

type Websocket

type Websocket struct {
	Conn Connection
	Addr string

	SendLimiter *rate.Limiter
	DialLimiter *rate.Limiter
}

func New

func New(addr string) *Websocket

func NewCustom

func NewCustom(conn Connection, addr string) *Websocket

NewCustom creates a new undialed Websocket.

func (*Websocket) Close

func (ws *Websocket) Close() error

func (*Websocket) Dial added in v0.0.5

func (ws *Websocket) Dial(ctx context.Context) error

func (*Websocket) Listen

func (ws *Websocket) Listen() <-chan Event

func (*Websocket) Send

func (ws *Websocket) Send(b []byte) error

Jump to

Keyboard shortcuts

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