chat

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

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

Go to latest
Published: May 24, 2022 License: MIT Imports: 16 Imported by: 0

README

Chat

Usage

go run ./cmd --help

TODO

  • Unit tests
  • Production setup: Dockerfile, client install/usage instructions

Documentation

Overview

Package chat implements a basic chat room application over websockets.

Index

Constants

This section is empty.

Variables

View Source
var ErrConnectionClosed = errors.New("connection closed")

ErrConnectionClosed is the error returned when the connection is closed and still requesting I/O operations.

View Source
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

type EventConnected struct {
	EventMeta
	Users []string `json:"users"`
}

EventConnected is (guaranteed) the first event sent when a new connection is made

type EventMeta

type EventMeta struct {
	Time time.Time `json:"time"`
}

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".

func (*EventMeta) When

func (e *EventMeta) When() time.Time

When returns the time of the event.

type EventNewMessage

type EventNewMessage struct {
	EventMeta
	Sender  string `json:"sender"`
	Message string `json:"message"`
}

type EventSendMessage

type EventSendMessage struct {
	EventMeta
	Message string `json:"message"`
}

type EventUserEnter

type EventUserEnter struct {
	EventMeta
	Name string `json:"name"`
}

type EventUserLeave

type EventUserLeave struct {
	EventMeta
	Name string `json:"name"`
}

type EventUserListUpdate

type EventUserListUpdate struct {
	EventMeta
	Users []string `json:"users"`
}

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 NewHub

func NewHub(logger log.Logger) *Hub

func (*Hub) Close

func (h *Hub) Close() error

func (*Hub) Connect

func (h *Hub) Connect(username string, conn Connection) (hubId, error)

func (*Hub) Disconnect

func (h *Hub) Disconnect(userId hubId) error

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

Directories

Path Synopsis
util
now
test
Package test implements some test helpers.
Package test implements some test helpers.

Jump to

Keyboard shortcuts

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