Documentation ¶
Overview ¶
Package wsutil provides abstractions around the Websocket, including rate limits.
Index ¶
- Constants
- Variables
- func HandleEvent(h EventHandler, ev Event) error
- func InjectValues(rawurl string, values url.Values) string
- func NewDialLimiter() *rate.Limiter
- func NewGlobalIdentityLimiter() *rate.Limiter
- func NewIdentityLimiter() *rate.Limiter
- func NewSendLimiter() *rate.Limiter
- func WaitForEvent(h EventHandler, ch <-chan Event, fn func(*OP) bool) error
- type Conn
- type Connection
- type Event
- type EventHandler
- type EventLoop
- type ExtraHandler
- type ExtraHandlers
- type OP
- type OPCode
- type PacemakerLoop
- type Websocket
Constants ¶
const CopyBufferSize = 2048
Variables ¶
var ( // WSTimeout is the timeout for connecting and writing to the Websocket, // before Gateway cancels and fails. WSTimeout = time.Minute // WSBuffer is the size of the Event channel. This has to be at least 1 to // make space for the first Event: Ready or Resumed. WSBuffer = 10 // WSError is the default error handler WSError = func(err error) { log.Println("Gateway error:", err) } // WSExtraReadTimeout is the duration to be added to Hello, as a read // timeout for the websocket. WSExtraReadTimeout = time.Second // WSDebug is used for extra debug logging. This is expected to behave // similarly to log.Println(). WSDebug = func(v ...interface{}) {} )
var CloseDeadline = time.Second
CloseDeadline controls the deadline to wait for sending the Close frame.
var ErrEmptyPayload = errors.New("Empty payload")
var ErrWebsocketClosed = errors.New("Websocket is closed.")
ErrWebsocketClosed is returned if the websocket is already closed.
Functions ¶
func HandleEvent ¶ added in v0.5.0
func HandleEvent(h EventHandler, ev Event) error
func NewDialLimiter ¶
func NewIdentityLimiter ¶
func NewSendLimiter ¶
func WaitForEvent ¶ added in v0.5.0
func WaitForEvent(h EventHandler, ch <-chan Event, fn func(*OP) bool) error
WaitForEvent blocks until fn() returns true. All incoming events are handled regardless.
Types ¶
type Conn ¶
Conn is the default Websocket connection. It compresses all payloads using zlib.
func NewConnWithDriver ¶ added in v0.5.0
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(context.Context, []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 EventHandler ¶ added in v0.5.0
type ExtraHandler ¶ added in v0.5.0
type ExtraHandlers ¶ added in v0.5.0
type ExtraHandlers struct {
// contains filtered or unexported fields
}
func (*ExtraHandlers) Add ¶ added in v0.5.0
func (ex *ExtraHandlers) Add(check func(*OP) bool) (<-chan *OP, func())
func (*ExtraHandlers) Check ¶ added in v0.5.0
func (ex *ExtraHandlers) Check(op *OP)
Check runs and sends OP data. It is not thread-safe.
type OP ¶ added in v0.5.0
type OP struct { Code OPCode `json:"op"` Data json.Raw `json:"d,omitempty"` // Only for Gateway Dispatch (op 0) Sequence int64 `json:"s,omitempty"` EventName string `json:"t,omitempty"` }
func AssertEvent ¶ added in v0.5.0
func (*OP) UnmarshalData ¶ added in v0.5.0
type PacemakerLoop ¶ added in v0.5.0
type PacemakerLoop struct { Extras ExtraHandlers ErrorLog func(error) // contains filtered or unexported fields }
PacemakerLoop provides an event loop with a pacemaker.
func NewLoop ¶ added in v0.5.0
func NewLoop(heartrate time.Duration, evs <-chan Event, evl EventLoop) *PacemakerLoop
func (*PacemakerLoop) Echo ¶ added in v0.5.0
func (p *PacemakerLoop) Echo()
func (*PacemakerLoop) Pace ¶ added in v0.5.0
func (p *PacemakerLoop) Pace() error
func (*PacemakerLoop) RunAsync ¶ added in v0.5.0
func (p *PacemakerLoop) RunAsync(exit func(error))
func (*PacemakerLoop) Stop ¶ added in v0.5.0
func (p *PacemakerLoop) Stop()
func (*PacemakerLoop) Stopped ¶ added in v0.5.0
func (p *PacemakerLoop) Stopped() bool
type Websocket ¶
type Websocket struct { Conn Connection Addr string // Timeout for connecting and writing to the Websocket, uses default // WSTimeout (global). Timeout time.Duration SendLimiter *rate.Limiter DialLimiter *rate.Limiter }
func NewCustom ¶
func NewCustom(conn Connection, addr string) *Websocket
NewCustom creates a new undialed Websocket.