Documentation
¶
Overview ¶
Package chat implements a basic chat room application over websockets.
Index ¶
- Variables
- type Connection
- type ErrUserIdNotFound
- type ErrUserNotFound
- type ErrUsernameExists
- type Event
- type EventConnected
- type EventMeta
- type EventNewMessage
- type EventSendMessage
- type EventUserEnter
- type EventUserLeave
- type EventUserListUpdate
- type GUIFrontend
- type Hub
- type StdoutFrontend
- type TestConnection
- func (c *TestConnection) Close(err error) error
- func (c *TestConnection) Closed() bool
- func (c *TestConnection) Err() error
- func (c *TestConnection) ReadEvent() (Event, error)
- func (c *TestConnection) SendEvent(e Event) error
- func (c *TestConnection) Wait() error
- func (c *TestConnection) WaitContext(ctx context.Context) error
Constants ¶
This section is empty.
Variables ¶
var ErrConnectionClosed = errors.New("connection closed")
ErrConnectionClosed is the error returned when the connection is closed and still requesting I/O operations.
var ErrHubClosed = errors.New("hub closed")
Used when disconnecting users on close, or trying to connect when already closed.
Functions ¶
This section is empty.
Types ¶
type Connection ¶
type Connection interface { // SendEvent sends event to the receiver. // Returns ErrConnectionClosed when connection closed. // Returns error when sending failed. SendEvent(e Event) error // ReadEvent wiats for next Event. Error when reading fails. // Returns error ErrConnectionClosed when connection closed. ReadEvent() (Event, error) // Wait waits until connection is closed. // Returns error with which the connection was closed (or nil) Wait() error // WaitContext waits until connection is closed. // Returns error with which the connection was closed (or nil) WaitContext(ctx context.Context) error // CLose closes connection, if connected. // Blocks until disconnected. Close(error) error // Closed return chan that is closed when connection is closed. Closed() bool // Err returns the error with which the connection closed, or nil. Err() error }
Connection is the interface that describes the connection between senders and receivers of events (e.g. client/server).
type ErrUserIdNotFound ¶
type ErrUserIdNotFound struct {
// contains filtered or unexported fields
}
ErrUserIdNotFound when hub did not find the user by id.
func (*ErrUserIdNotFound) Error ¶
func (e *ErrUserIdNotFound) Error() string
type ErrUserNotFound ¶
type ErrUserNotFound struct {
// contains filtered or unexported fields
}
ErrHubUserNotFound when hub did not find the user by id.
func (*ErrUserNotFound) Error ¶
func (e *ErrUserNotFound) Error() string
type ErrUsernameExists ¶
type ErrUsernameExists struct {
// contains filtered or unexported fields
}
ErrUsernameExists for when the hub already has the user(name)
func (*ErrUsernameExists) Error ¶
func (e *ErrUsernameExists) Error() string
type Event ¶
type Event interface { // When returns time of the event. // Important we define *something* more than interface{} // for static analysis to work on *Struct{} vs Struct{} When() time.Time }
Event is the interface for all events
type EventConnected ¶
EventConnected is (guaranteed) the first event sent when a new connection is made
type EventMeta ¶
EventMeta is a base struct for other events to include basic meta data that all events have
func NewEventMetaNow ¶
func NewEventMetaNow() *EventMeta
NewEventMetaNow returns EventMeta with time set to "now".
type EventNewMessage ¶
type EventSendMessage ¶
type EventUserEnter ¶
type EventUserLeave ¶
type EventUserListUpdate ¶
type GUIFrontend ¶
type GUIFrontend struct {
// contains filtered or unexported fields
}
func NewGUIFrontend ¶
func NewGUIFrontend(conn Connection, logger log.Logger) (*GUIFrontend, error)
func (*GUIFrontend) Start ¶
func (f *GUIFrontend) Start() error
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub is the chat hub/room where users can connect to.
func (*Hub) Disconnect ¶
type StdoutFrontend ¶
type StdoutFrontend struct {
// contains filtered or unexported fields
}
func NewStdoutFrontend ¶
func NewStdoutFrontend(conn Connection, logger log.Logger) *StdoutFrontend
func (*StdoutFrontend) Start ¶
func (f *StdoutFrontend) Start() error
type TestConnection ¶
type TestConnection struct { // EventOutCh is the channel used for sending events. EventOutCh chan<- Event // EventInCh is the channel used for receiving events. EventInCh <-chan Event // contains filtered or unexported fields }
TestConnection is a simple example implementation of the Connection interface.
func NewTestConnection ¶
func NewTestConnection( eventInCh <-chan Event, eventOutCh chan<- Event, ) *TestConnection
NewTestConnection creates a TestConnection using the passed in/out channels.
func (*TestConnection) Close ¶
func (c *TestConnection) Close(err error) error
func (*TestConnection) Closed ¶
func (c *TestConnection) Closed() bool
func (*TestConnection) Err ¶
func (c *TestConnection) Err() error
func (*TestConnection) ReadEvent ¶
func (c *TestConnection) ReadEvent() (Event, error)
func (*TestConnection) SendEvent ¶
func (c *TestConnection) SendEvent(e Event) error
func (*TestConnection) Wait ¶
func (c *TestConnection) Wait() error
func (*TestConnection) WaitContext ¶
func (c *TestConnection) WaitContext(ctx context.Context) error