stubborn

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2021 License: Apache-2.0 Imports: 14 Imported by: 0

README

Stubborn WebSocket

Stubborn WebSocket is a framework that's written in Go for WebSocket clients.

Examples
Status

The Stubborn WebSocket framework provides a complete and tested persistent connection for the WebSocket protocol using various types of clients.

Installation
go get github.com/filinvadim/stubborn-ws
Stubborn WebSocket advantages
  1. Agnostic to any WebSocket clients by WSConnector interface.
  2. Therefore, it's fully testable using mock that implements three methods:
    Close() error
    ReadMessage() (messageType int, p []byte, err error)
    WriteMessage(messageType int, data []byte) error
    
  3. Persistence. Stubborn WebSocket framework won't stop fetching data even if panic() occurs (that's useful if for example Gorilla WebSocket was used as client).
  4. Fairly configurable for any WebSocker API requirements.
Stubborn WebSocket disadvantages
  1. Framework wasn't benchmark tested but expected average speed.

Documentation

Overview

Author of this file is https://github.com/nsf https://github.com/nsf/jsondiff/blob/master/LICENSE

Index

Constants

View Source
const (
	TextMessage   = 1
	BinaryMessage = 2
	PingMessage   = 9
	PongMessage   = 10
)

Variables

View Source
var (
	ErrCritical = errors.New("critical")
	ErrMajor    = errors.New("major")
	ErrMinor    = errors.New("minor")
)

Functions

func GZipCompress

func GZipCompress(input string) ([]byte, error)

func GZipDecompress

func GZipDecompress(input []byte) ([]byte, error)

func NewStubborn

func NewStubborn(
	conf Config,
) *client

Types

type AuthHandler

type AuthHandler func() (req []byte, resp []byte, err error)

type Backoff

type Backoff struct {
	Exponentiation float64
	//Jitter eases contention by randomizing backoff steps
	Jitter bool
	//Min and Max are the minimum and maximum values of the counter
	Min, Max time.Duration
	// contains filtered or unexported fields
}

func (*Backoff) Attempt

func (b *Backoff) Attempt() float64

Attempt returns the current attempt counter value.

func (*Backoff) Duration

func (b *Backoff) Duration() time.Duration

func (*Backoff) Reset

func (b *Backoff) Reset()

Reset restarts the current attempt counter at zero.

type Config

type Config struct {
	URL             string
	IsReconnectable bool
	// default message type
	MessageType int
	// use your favourite logger
	Print PrintFunc
	// use your favourite client
	Dialerf     DialerFunc
	AuthTimeOut time.Duration
	// it is easier to define expected error because exceptions always unexpected :)
	UnimportantErrs []error
}

type DialerFunc

type DialerFunc func() (WSConnector, error)

type Difference

type Difference int
const (
	FullMatch Difference = iota
	SupersetMatch
	NoMatch
	FirstArgIsInvalidJson
	SecondArgIsInvalidJson
	BothArgsAreInvalidJson
)

func CompareJSON

func CompareJSON(a, b []byte, opts *Options) (Difference, string)

Compares two JSON documents using given options. Returns difference type and a string describing differences.

FullMatch means provided arguments are deeply equal.

SupersetMatch means first argument is a superset of a second argument. In this diffContext being a superset means that for each object or array in the hierarchy which don't match exactly, it must be a superset of another one. For example:

{"a": 123, "b": 456, "c": [7, 8, 9]}

Is a superset of:

{"a": 123, "c": [7, 8]}

NoMatch means there is no match.

The rest of the difference types mean that one of or both JSON documents are invalid JSON.

Returned string uses a format similar to pretty printed JSON to show the human-readable difference between provided JSON documents. It is important to understand that returned format is not a valid JSON and is not meant to be machine readable.

func (Difference) String

func (d Difference) String() string

type ErrorHandler

type ErrorHandler func(err error)

type KeepAlive

type KeepAlive struct {
	Tick       <-chan time.Time
	CustomPing func() (msgType int, payload []byte)
	CustomPong func(msgTp int, data []byte) (msgType int, payload []byte)
}

type MessageHandler

type MessageHandler func(resp []byte)

type Options

type Options struct {
	Normal           Tag
	Added            Tag
	Removed          Tag
	Changed          Tag
	Prefix           string
	Indent           string
	PrintTypes       bool
	ChangedSeparator string
}

func DefaultConsoleOptions

func DefaultConsoleOptions() Options

Provides a set of options that are well suited for console output. Options use ANSI foreground color escape sequences to highlight changes.

func DefaultHTMLOptions

func DefaultHTMLOptions() Options

Provides a set of options that are well suited for HTML output. Works best inside <pre> tag.

func DefaultJSONOptions

func DefaultJSONOptions() Options

Provides a set of options in JSON format that are fully parseable.

type PrintFunc

type PrintFunc func(args ...interface{})

type Tag

type Tag struct {
	Begin string
	End   string
}

type WSConnector

type WSConnector interface {
	Close() error
	ReadMessage() (messageType int, p []byte, err error)
	WriteMessage(messageType int, data []byte) error
}

Jump to

Keyboard shortcuts

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