connection

package
v0.0.0-...-2187358 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleConnection

func HandleConnection(handler ConnectionHandler, conn Connection)

Types

type ConnHandler

type ConnHandler struct {
	// contains filtered or unexported fields
}

ConnHandler implements Handler

func (*ConnHandler) Authorizer

func (h *ConnHandler) Authorizer() rbac.Authorizer

func (*ConnHandler) Connection

func (h *ConnHandler) Connection(uuid string) (Connection, bool)

func (*ConnHandler) DeleteConnection

func (h *ConnHandler) DeleteConnection(conn Connection)

func (*ConnHandler) Handle

func (h *ConnHandler) Handle(conn Connection)

func (*ConnHandler) NamespaceByName

func (h *ConnHandler) NamespaceByName(ns string) (Namespace, bool)

func (*ConnHandler) NewConnection

func (h *ConnHandler) NewConnection(uuid string, ws *websocket.Conn, w http.ResponseWriter, r *http.Request) Connection

type ConnHandlerWithRBAC

type ConnHandlerWithRBAC struct {
	ConnectionHandler
	// contains filtered or unexported fields
}

func (*ConnHandlerWithRBAC) Authorizer

func (r *ConnHandlerWithRBAC) Authorizer() rbac.Authorizer

type Connection

type Connection interface {
	// Broadcast calls the namespace handler Broadcast method
	// to scope the function's effects to the current connection's namespace
	Broadcast(string, string, []byte)
	// BroadcastFrom behaves like Broadcast, except the connection id provided
	// is skipped from any effects or mutations taken by the handler's method.
	BroadcastFrom(string, string, []byte)
	// Metadata returns ConnectionMetadata for the current connection
	Metadata() ConnectionMetadata
	// Connections returns socket connections that are in the same namespace as the connection
	Connections() []Connection
	// Emit iterates through all stored SocketEventCallback functions and calls
	// them with the given Message argument.
	Emit(string, MessageDataCodec)
	// UUID retrieves the connection's uuid
	UUID() string
	// Join assigns the connection to a namespace
	Join(string)
	// TODO: add support for multiple namespaces per connection
	// Leave leaves any namespace the connection has been assigned to
	Leave(string)
	// Namespace returns the namespace the connection has been bound to
	// or a boolean false if the connection has not yet been bound to one.
	Namespace() (Namespace, bool)
	// On receives a key and a SocketEventCallback and pushes the SocketEventCallback
	// to a list of SocketEventCallback functions mapped to the given key
	On(string, SocketEventCallback)
	// ReadMessage reads a text message from the connection
	ReadMessage() (int, []byte, error)
	// ResponseWriter returns the saved http.ResponseWriter for this connection
	ResponseWriter() http.ResponseWriter
	// Request returns the saved http.Request for this connection
	Request() *http.Request
	// Send receives an array of bytes to send to the connection
	Send([]byte)
	// WriteMessage sends a text message as an array of bytes to the connection
	WriteMessage(int, []byte) error
}

func NewConnection

func NewConnection(nsHandler NamespaceHandler, ws *websocket.Conn, w http.ResponseWriter, r *http.Request) Connection

func NewConnectionWithUUID

func NewConnectionWithUUID(uuid string, nsHandler NamespaceHandler, ws *websocket.Conn, w http.ResponseWriter, r *http.Request) Connection

type ConnectionHandler

type ConnectionHandler interface {
	// Authorizer returns an RBAC authorizer or nil
	Authorizer() rbac.Authorizer
	// NewConnection instantiates a new Connection
	// if a non-empty uuid string is given, a new
	// connection is spawned with the given uuid.
	// Returns the newly created Connection
	NewConnection(string, *websocket.Conn, http.ResponseWriter, *http.Request) Connection
	// Connection receives a Connection uuid, and returns the
	// associated connection. Returns a boolean false if no Connection
	// exists by the given uuid.
	Connection(string) (Connection, bool)
	// DeleteConnection receives a Connection and removes it from the internal list
	DeleteConnection(Connection)
	// NamespaceByName returns a connection Namespace for the given Namespace name
	// Returns a boolean (false) if a namespace by the given name does not exist
	NamespaceByName(string) (Namespace, bool)
	// Handle receives a Connection and creates a goroutine
	// to parse and handle callbacks for incoming messages
	Handle(Connection)
}

ConnectionHandler provides methods for managing multiple socket connections

func NewHandler

func NewHandler(nsHandler NamespaceHandler) ConnectionHandler

func NewHandlerWithRBAC

func NewHandlerWithRBAC(authorizer rbac.Authorizer, nsHandler NamespaceHandler) ConnectionHandler

type ConnectionMetadata

type ConnectionMetadata interface {
	CreationTimestamp() time.Time
}

func NewConnectionMetadata

func NewConnectionMetadata() ConnectionMetadata

type ConnectionMetadataSpec

type ConnectionMetadataSpec struct {
	// contains filtered or unexported fields
}

func (*ConnectionMetadataSpec) CreationTimestamp

func (m *ConnectionMetadataSpec) CreationTimestamp() time.Time

type Message

type Message struct {
	Event string           `json:"event"`
	Data  MessageDataCodec `json:"data"`
}

type MessageData

type MessageData interface {
	MessageDataCodec

	Key(string) (value interface{}, keyExists bool)
	Set(string, interface{})
}

func NewMessageData

func NewMessageData() MessageData

type MessageDataCodec

type MessageDataCodec interface {
	Serialize() ([]byte, error)
}

MessageDataCodec is a serializable schema representing the contents of a socket connection message

type MessageDataSchema

type MessageDataSchema map[string]interface{}

MessageDataSchema implements MessageData

func (*MessageDataSchema) Key

func (d *MessageDataSchema) Key(key string) (interface{}, bool)

func (*MessageDataSchema) Serialize

func (d *MessageDataSchema) Serialize() ([]byte, error)

func (*MessageDataSchema) Set

func (d *MessageDataSchema) Set(key string, val interface{})

type Namespace

type Namespace interface {
	// Add receives a Connection to compose
	Add(Connection) error
	// Connection receives a connection uuid and returns the
	// connection associated with it, or a boolean (false)
	// if a connection does not exist by the specified uuid.
	Connection(string) (Connection, bool)
	// Connections returns a slice of connections aggregated by
	// the current namespace
	Connections() []Connection
	// Name returns the given namespace name
	Name() string
	// Remove receives a Connection to remove from the list
	// of composed connections. Returns an error if the
	// connection is not aggregated by this namespace
	Remove(Connection) error
	// UUID returns the unique identifier for the namespace
	UUID() string
}

func NewNamespace

func NewNamespace(name string) Namespace

type NamespaceHandler

type NamespaceHandler interface {
	// AddToNamespace receives a namespace name and a Connection and adds the
	// Connection to the specified namespace. If the specified namespace does
	// not exist, a new one is created.
	AddToNamespace(string, Connection)
	// NamespaceByName receives a namespace name and returns the corresponding connections
	// assigned to it, or a boolean (false) if it does not exist.
	NamespaceByName(string) (Namespace, bool)
	// NewNamespace creates a namespace with the given name
	// or returns an existing namespace if one already exists
	// by the given name
	NewNamespace(string) Namespace
	// RemoveFromNamespace receives a namespace name and a Connection and removes
	// the Connection from the specified namespace. If the namespace does not exist,
	// a no-op occurs. If removing the Connection from the namespace results in an empty
	// namespace, the namespace is deleted.
	RemoveFromNamespace(string, Connection)
	// DeleteNamespaceByName receives a namespace name and removes it
	// from the list of composed namespaces
	DeleteNamespaceByName(string) error
	// Broadcast iterates through all connections in a namespace and sends received data.
	// If a given namespace does not exist, a no-op occurs
	Broadcast(int, string, string, []byte)
	// BroadcastFrom behaves like Broadcast, except the connection with provided id is skipped.
	BroadcastFrom(int, string, string, string, []byte)
}

Namespace provides convenience methods for handling segments of registered Connections

func NewNamespaceHandler

func NewNamespaceHandler() NamespaceHandler

type NamespaceHandlerSpec

type NamespaceHandlerSpec struct {
	// contains filtered or unexported fields
}

NamespaceHandlerSpec implements Namespace

func (*NamespaceHandlerSpec) AddToNamespace

func (h *NamespaceHandlerSpec) AddToNamespace(ns string, conn Connection)

func (*NamespaceHandlerSpec) Broadcast

func (h *NamespaceHandlerSpec) Broadcast(messageType int, ns, eventName string, data []byte)

func (*NamespaceHandlerSpec) BroadcastFrom

func (h *NamespaceHandlerSpec) BroadcastFrom(messageType int, connId, ns, eventName string, data []byte)

func (*NamespaceHandlerSpec) DeleteNamespaceByName

func (h *NamespaceHandlerSpec) DeleteNamespaceByName(ns string) error

func (*NamespaceHandlerSpec) NamespaceByName

func (h *NamespaceHandlerSpec) NamespaceByName(ns string) (Namespace, bool)

func (*NamespaceHandlerSpec) NewNamespace

func (h *NamespaceHandlerSpec) NewNamespace(ns string) Namespace

func (*NamespaceHandlerSpec) RemoveFromNamespace

func (h *NamespaceHandlerSpec) RemoveFromNamespace(ns string, conn Connection)

type NamespaceSpec

type NamespaceSpec struct {
	// contains filtered or unexported fields
}

func (*NamespaceSpec) Add

func (n *NamespaceSpec) Add(conn Connection) error

func (*NamespaceSpec) Connection

func (n *NamespaceSpec) Connection(uuid string) (Connection, bool)

func (*NamespaceSpec) Connections

func (n *NamespaceSpec) Connections() []Connection

func (*NamespaceSpec) Name

func (n *NamespaceSpec) Name() string

func (*NamespaceSpec) Remove

func (n *NamespaceSpec) Remove(conn Connection) error

func (*NamespaceSpec) UUID

func (n *NamespaceSpec) UUID() string

type SocketConn

type SocketConn struct {
	*websocket.Conn
	// contains filtered or unexported fields
}

Socket composes a websocket.Conn and implements Connection

func (*SocketConn) Broadcast

func (c *SocketConn) Broadcast(roomName, eventName string, data []byte)

func (*SocketConn) BroadcastFrom

func (c *SocketConn) BroadcastFrom(roomName, eventName string, data []byte)

func (*SocketConn) Connections

func (c *SocketConn) Connections() []Connection

func (*SocketConn) Emit

func (c *SocketConn) Emit(eventName string, data MessageDataCodec)

func (*SocketConn) Join

func (c *SocketConn) Join(roomName string)

func (*SocketConn) Leave

func (c *SocketConn) Leave(roomName string)

func (*SocketConn) Metadata

func (c *SocketConn) Metadata() ConnectionMetadata

func (*SocketConn) Namespace

func (c *SocketConn) Namespace() (Namespace, bool)

func (*SocketConn) On

func (c *SocketConn) On(eventName string, callback SocketEventCallback)

func (*SocketConn) ReadMessage

func (c *SocketConn) ReadMessage() (int, []byte, error)

func (*SocketConn) Request

func (c *SocketConn) Request() *http.Request

func (*SocketConn) ResponseWriter

func (c *SocketConn) ResponseWriter() http.ResponseWriter

func (*SocketConn) Send

func (c *SocketConn) Send(data []byte)

func (*SocketConn) UUID

func (c *SocketConn) UUID() string

func (*SocketConn) WriteMessage

func (c *SocketConn) WriteMessage(messageType int, data []byte) error

type SocketEventCallback

type SocketEventCallback func(MessageDataCodec)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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