wsubs

package module
v0.0.0-...-fd86327 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2015 License: GPL-2.0 Imports: 14 Imported by: 1

README

wsubs

WebSocket subscription handling in a RESTy way

Documentation

Index

Constants

View Source
const (
	SubscribeType   = "Subscribe"
	UnsubscribeType = "Unsubscribe"
	UpdateType      = "Update"
	CreateType      = "Create"
	DeleteType      = "Delete"
	RPCType         = "RPC"
	ErrorType       = "Error"
)
View Source
const (
	FatalLevel = iota
	ErrorLevel
	InfoLevel
	DebugLevel
	TraceLevel
)
View Source
const (
	FetchType = "Fetch"
)

Variables

This section is empty.

Functions

func Prettify

func Prettify(obj interface{}) string

Prettify will return a nicely indented JSON encoding of obj

Types

type Context

type Context interface {
	Logger
	Conn() *websocket.Conn
	Message() *Message
	Principal() string
	Match() []string
	SetMatch([]string)
	Data() JSON
	SetData(JSON)
	IsSubscribing(principal, uri string, timeout time.Duration) bool
}

Context describes a single WebSocket message and its environment

func NewContext

func NewContext(conn *websocket.Conn, message *Message, principal string, parent LoggingSubscriptionManager) Context

type Error

type Error struct {
	Cause interface{}
	Error string
}

Error encapsulates an error

type JSON

type JSON struct {
	Data interface{}
}

JSON wraps anything that is a JSON object.

func (JSON) Get

func (self JSON) Get(key string) JSON

Get returns the value under key as another JSON.

func (JSON) GetString

func (self JSON) GetString(key string) string

GetString returns the value under key as a string.

func (JSON) GetStringSlice

func (self JSON) GetStringSlice(key string) (result []string)

GetStringSLice returns the value under key as a string slice.

func (JSON) Overwrite

func (self JSON) Overwrite(dest interface{})

Overwrite will JSON encode itself and decode it into dest.

type Logger

type Logger interface {
	Fatalf(format string, params ...interface{})
	Errorf(format string, params ...interface{})
	Infof(format string, params ...interface{})
	Debugf(format string, params ...interface{})
	Tracef(format string, params ...interface{})
}

Loggers be loggin'

type LoggingSubscriptionManager

type LoggingSubscriptionManager interface {
	SubscriptionManager
	Logger
}

LoggingSubscriptionManager logs and manages subscriptions...

type Message

type Message struct {
	Type   string
	Object *Object `json:",omitempty"`
	Method *Method `json:",omitempty"`
	Error  *Error  `json:",omitempty"`
}

Message wraps Objects in JSON messages.

type Method

type Method struct {
	Name string
	Id   string
	Data interface{} `json:",omitempty"`
}

Method is used to send JSON RPC requests.

type Object

type Object struct {
	URI  string
	Data interface{} `json:",omitempty"`
}

Object is used to send JSON messages to subscribing WebSockets.

type RPC

type RPC struct {
	Method        string
	Handler       RPCHandler
	Authenticated bool
}

RPC describes how the router ought to treat incoming requests for a given RPC method

func (*RPC) Auth

func (self *RPC) Auth() *RPC

Auth tells the router that the RPC should only receive messages from authenticated requests (where the Context has a Principal())

type RPCHandler

type RPCHandler func(c Context) (result interface{}, err error)

RPCHandlers handle RPC requests

type RPCs

type RPCs []*RPC

type Resource

type Resource struct {
	Path          *regexp.Regexp
	Handlers      map[string]ResourceHandler
	Authenticated map[string]bool
	// contains filtered or unexported fields
}

Resource describes how the router ought to treat incoming requests for a resource found under a given URI regexp

func (*Resource) Auth

func (self *Resource) Auth() *Resource

Auth tells the router that the op/handler combination defined in the last Handle call should only receive messages from authenticated requests (where the Context has a Principal())

func (*Resource) Handle

func (self *Resource) Handle(op string, handler ResourceHandler) *Resource

Handle tells the router how to handle a given operation on the resource

type ResourceHandler

type ResourceHandler func(c Context) error

ResourceHandler will handle a message regarding an operation on a resource

type Resources

type Resources []*Resource

type Router

type Router struct {
	Resources           Resources
	RPCs                RPCs
	Logger              *log.Logger
	LogLevel            int
	OnDisconnectFactory func(ws *websocket.Conn, principal string) func()
	OnConnect           func(ws *websocket.Conn, principal string)
	DevMode             bool

	Secret string
	// contains filtered or unexported fields
}

Router controls incoming WebSocket messages

func NewRouter

func NewRouter() (result *Router)

NewRouter returns a router connected to db

func (*Router) CheckPrincipal

func (self *Router) CheckPrincipal(r *http.Request) (principal string, ok bool)

func (*Router) Debugf

func (self *Router) Debugf(format string, args ...interface{})

Debugf is shorthand for Logf(DebugLevel...

func (*Router) DefaultOnConnect

func (self *Router) DefaultOnConnect(ws *websocket.Conn, principal string)

DefaultOnConnect will just log the incoming connection

func (*Router) DefaultOnDisconnectFactory

func (self *Router) DefaultOnDisconnectFactory(ws *websocket.Conn, principal string) func()

DefaultOnDisconnectFactory will return functions that just log the disconnecting connection

func (*Router) DeliverError

func (self *Router) DeliverError(ws *websocket.Conn, cause interface{}, err error)

DeliverError sends an error message along the provided WebSocket connection

func (*Router) Errorf

func (self *Router) Errorf(format string, args ...interface{})

Errorf is shorthand for Logf(ErrorLevel...

func (*Router) Fatalf

func (self *Router) Fatalf(format string, args ...interface{})

Fatalf is shorthand for Logf(FatalLevel...

func (*Router) HandleRPCMessage

func (self *Router) HandleRPCMessage(c Context) (err error)

HandleRPCMessage will handle the message that produced c, by finding a matching RPC method (if there is one) and sending it the context.

func (*Router) HandleResourceMessage

func (self *Router) HandleResourceMessage(c Context) (err error)

HandleResourceMessage will handle the message that produced c, by finding a matching resource (if there is one) and sending it the context.

func (*Router) Infof

func (self *Router) Infof(format string, args ...interface{})

Infof is shorthand for Logf(InfoLevel...

func (*Router) IsSubscribing

func (self *Router) IsSubscribing(principal, uri string, timeout time.Duration) (result bool)

IsSubscriber returns true if principal is currently subscribing to uri.

func (*Router) Logf

func (self *Router) Logf(level int, format string, args ...interface{})

Logf will log the format and args if level is less than the LogLevel of this Router

func (*Router) MarkActive

func (self *Router) MarkActive(principal string)

MarkActive will timestamp the principal as active, for purposes of checking whether the principal is subscribing.

func (*Router) ProcessMessages

func (self *Router) ProcessMessages(ws *websocket.Conn, principal string, handlerFunc func(*Message) error)

ProcessMessages will decode messages from the ws, and handle them with handlerFunc

func (*Router) RPC

func (self *Router) RPC(method string, handler RPCHandler) (result *RPC)

RPC creates an RPC method receiving messages matching the provided method name

func (*Router) RemoveSubscriber

func (self *Router) RemoveSubscriber(principal, uri string)

RemoveSubscriber will remember that principal stopped subscribing to uri

func (*Router) Resource

func (self *Router) Resource(exp string) (result *Resource)

Resource creates a resource receiving messages matching the provided regexp

func (*Router) ServeHTTP

func (self *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

Implements http.Handler

func (*Router) SetupConnection

func (self *Router) SetupConnection(ws *websocket.Conn) (principal string, ok bool)

SetupConnection will try to find a principal for the provided connection, log it, and return if it's ok to continue processing it.

func (*Router) Tracef

func (self *Router) Tracef(format string, args ...interface{})

Tracef is shorthand for Logf(TraceLevel...

func (*Router) Upgrader

func (self *Router) Upgrader() *websocket.Upgrader

type SubscriptionManager

type SubscriptionManager interface {
	IsSubscribing(principal, uri string, timeout time.Duration) bool
}

SubscriptionManager be managin' subscriptions

type Token

type Token struct {
	Principal string
	Timeout   time.Time
	Hash      []byte
	Encoded   string
}

Token handles authentication of the sockets connecting to a router

func DecodeToken

func DecodeToken(secret, s string) (result *Token, err error)

DecodeToken unpacks s into a Token, that it validates (hash and timeout) and returns

func (*Token) Encode

func (self *Token) Encode(secret string) (err error)

Encode will set the Encoded field for this Token to an encoded version of itself

func (*Token) GetHash

func (self *Token) GetHash(secret string) (result []byte, err error)

GetHash calculates a hash for this Token

Jump to

Keyboard shortcuts

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