Documentation ¶
Overview ¶
Package room provides a framework for REx servers and clients to communicate using arbitrary messages.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Bus ¶
type Bus struct {
// contains filtered or unexported fields
}
Bus is the communication bus for a Server.
func NewBus ¶
func NewBus(ctx context.Context, handlers ...MsgHandler) *Bus
NewBus initializes and returns a new Bus.
func (*Bus) AddHandler ¶
func (b *Bus) AddHandler(h MsgHandler)
AddHandler changes the bus message handler.
func (*Bus) Message ¶
Message is called by a subscriber to signal back to the bus owner via b.handler.
func (*Bus) Subscribe ¶
func (b *Bus) Subscribe(start int) *Subscription
Subscribe returns a new Subscription that new events from b.
func (*Bus) Unsubscribe ¶
func (b *Bus) Unsubscribe(s *Subscription)
Unsubscribe removes s from the recipients of b's events. After Unsubscribe returns no further events will be received in calls to s.Next().
type Client ¶
type Client struct { Host string Port int Handler EventHandler HTTP *http.Client Now func() *roomtime.Time Session string }
Client is an interface to a remote REx server.
func NewClient ¶
func NewClient(h EventHandler) *Client
NewClient allocates and returns a new client with its Handler set to h.
func NewClientRestore ¶
func NewClientRestore(h EventHandler, session string) *Client
NewClientRestore behaves like NewClient but sets the client's Session to session as well.
func (*Client) CreateSession ¶
CreateSession initializes c.Session by registering an identifier with the remote bus. If the value is already set no registration is performed.
type Codec ¶
type Codec interface { // EncodeMessage serializes v and returns the []byte or any error. EncodeMessage(v Msg) []byte // DecodeMessage deserializes b into a Msg and returns it with any error. DecodeMessage(b []byte) (Msg, error) // DecodeEvent deserializes b into an Event and returns it with any error. DecodeEvent(b []byte) (Event, error) // EncodeEvent serializes v and returns the []byte or any error. EncodeEvent(v Event) []byte }
Codec performs serialization for room data.
type Content ¶
type Content interface { // Data returns the event content is a slice of bytes (even if the content // is stored as a string). Applications should use whichever of Data or // Text methods they need and any translation necessary will happen as fast // as possible. Data() []byte // Text is like Data but returns a string representation of event data and // may be optmized to do that. Text() string }
Content is application data that is transmitted over a bus.
type Event ¶
Event is a broadcast message from the server to all clients. Unlike Msg an event does not have an associated session identifier because it is intended for all clients.
type EventHandler ¶
EventHandler is part of the client's event loop.
type Msg ¶
type Msg interface { // Session returns an identifier for the client (session) that originated // the message. Session() string Time() *roomtime.Time Content }
Msg is a piece of data originating from a client application.
type MsgHandler ¶
MsgHandler is a bus message handler.
type Room ¶
Room represents a single shared enivornment managed by a server. The service is advertised using mDNS an must conform to the format specified in RFC 6763 Section 7. The Name may contain any unicode text excluding ASCII control characters but is recommended to not contain '\n' bytes for display purposes. An mDNS instance identifier will be generated from the given name, the time and the process identifier.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a server used by a TV application to run a game or collaborative procedure.
func NewServer ¶
func NewServer(config *ServerConfig) *Server
NewServer initializes a new server, but does not start serving clients.
func (*Server) Event ¶
Event broadcasts c to all connected clients, giving it the next unused event index.
func (*Server) Run ¶
Run binds to a random port, begins broadcasting service metadata using mDNS, and begins streaming client events and dispatching client messages. Typically, Run never returns a value. If any critical error is encountered it will be returned.
Run is equivalent to calling Start, followed by s.Wait.
type ServerConfig ¶
type ServerConfig struct { Room *Room Bus *Bus // Addr is an optional address to bind. If empty, the address of ":0" will // be used. Addr string }
ServerConfig controls how a server advertises itself to potential clients as well as miscelaneous communication behaviors.
type Subscription ¶
type Subscription struct {
// contains filtered or unexported fields
}
Subscription represents a remote client that needs to receive messages from a Bus.
func (*Subscription) Event ¶
func (s *Subscription) Event() Event
Event returns the last received Event.
func (*Subscription) Next ¶
func (s *Subscription) Next(timeout <-chan time.Time) (ok bool)
Next waits for the next event to be received over the channel and returns it. If the subscription is terminated, or values is received over timeout Next will return a value value. Otherwise Next returns true and Event will return the event received.