wsutil

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2020 License: ISC Imports: 12 Imported by: 5

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.

View Source
var ErrWebsocketClosed = errors.New("Websocket is closed.")

ErrWebsocketClosed is returned if the websocket is already closed.

Functions

func InjectValues

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() (err 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.
	Close() 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

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