velox

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2021 License: MIT Imports: 21 Imported by: 23

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//MinThrottle is the minimum manual State.Throttle value.
	//15ms is approximately highest resolution on the JS eventloop.
	MinThrottle = 15 * time.Millisecond
	//DefaultThrottle is the default State.Throttle value.
	DefaultThrottle = 200 * time.Millisecond
	//DefaultWriteTimeout is the default State.Throttle value.
	DefaultWriteTimeout = 30 * time.Second
	//DefaultPingInterval is the default State.PingInterval value.
	DefaultPingInterval = 25 * time.Second
)
View Source
var JS = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
	filename := "bundle.js"
	b, _ := Asset(filename)
	info, _ := AssetInfo(filename)

	if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") && w.Header().Get("Content-Encoding") != "gzip" {
		gb := bytes.Buffer{}
		g := gzip.NewWriter(&gb)
		g.Write(b)
		g.Close()
		b = gb.Bytes()
		w.Header().Set("Content-Encoding", "gzip")
	}
	buff := bytes.NewReader(b)

	http.ServeContent(w, req, info.Name(), info.ModTime(), buff)
})

JS is an HTTP handler serving the velox.js frontend library

Functions

func Asset

func Asset(name string) ([]byte, error)

Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetDir

func AssetDir(name string) ([]string, error)

AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:

data/
  foo.txt
  img/
    a.png
    b.png

then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("notexist") would return an error AssetDir("") will return []string{"data"}.

func AssetInfo

func AssetInfo(name string) (os.FileInfo, error)

AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetNames

func AssetNames() []string

AssetNames returns the names of the assets.

func MustAsset

func MustAsset(name string) []byte

MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables.

func RestoreAsset

func RestoreAsset(dir, name string) error

RestoreAsset restores an asset under the given directory

func RestoreAssets

func RestoreAssets(dir, name string) error

RestoreAssets restores an asset under the given directory recursively

func SyncHandler

func SyncHandler(gostruct interface{}) http.Handler

SyncHandler is a small wrapper around Sync which simply synchronises all incoming connections. Use Sync if you wish to implement user authentication or any other request-time checks.

Types

type Conn

type Conn interface {
	ID() string
	Connected() bool
	Wait()
	Push()
	Close() error
}

Conn represents a single live connection being synchronised. ID is current set to the connection's remote address.

func Sync

func Sync(gostruct interface{}, w http.ResponseWriter, r *http.Request) (Conn, error)

Sync upgrades a given HTTP connection into a velox connection and synchronises the provided struct with the client. velox takes responsibility for writing the response in the event of failure. Default handlers close the TCP connection on return so when manually using this method, you'll most likely want to block using Conn.Wait().

type Pusher

type Pusher interface {
	Push() bool
}

Pusher implements a push method, similar to Flush

type State

type State struct {
	//configuration
	Throttle     time.Duration `json:"-"`
	WriteTimeout time.Duration `json:"-"`
	PingInterval time.Duration `json:"-"`
	// contains filtered or unexported fields
}

State must be embedded into a struct to make it syncable.

func (*State) ID

func (s *State) ID() string

ID uniquely identifies this state object

func (*State) NumConnections

func (s *State) NumConnections() int

NumConnections currently active

func (*State) Push

func (s *State) Push() bool

Push the changes from this object to all connected clients. Push is thread-safe and is throttled so it can be called with abandon. Returns false if a Push is already in progress.

func (*State) Version

func (s *State) Version() int64

Version of this state object (when the underlying struct is and a Push is performed, this version number is incremented).

Jump to

Keyboard shortcuts

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