hub

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2021 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeTyping          = "typing"
	TypeMessage         = "message"
	TypePeerList        = "peer.list"
	TypePeerInfo        = "peer.info"
	TypePeerJoin        = "peer.join"
	TypePeerLeave       = "peer.leave"
	TypePeerRateLimited = "peer.ratelimited"
	TypeRoomDispose     = "room.dispose"
	TypeRoomFull        = "room.full"
	TypeNotice          = "notice"
	TypeHandle          = "handle"
)

Types of messages sent to peers.

Variables

This section is empty.

Functions

func GenerateGUID

func GenerateGUID(n int) (string, error)

GenerateGUID generates a cryptographically random, alphanumeric string of length n.

Types

type Config

type Config struct {
	Address string `koanf:"address"`
	RootURL string `koanf:"root_url"`

	Name              string        `koanf:"name"`
	RoomIDLen         int           `koanf:"room_id_length"`
	MaxCachedMessages int           `koanf:"max_cached_messages"`
	MaxMessageLen     int           `koanf:"max_message_length"`
	WSTimeout         time.Duration `koanf:"websocket_timeout"`
	MaxMessageQueue   int           `koanf:"max_message_queue"`
	RateLimitInterval time.Duration `koanf:"rate_limit_interval"`
	RateLimitMessages int           `koanf:"rate_limit_messages"`
	MaxRooms          int           `koanf:"max_rooms"`
	MaxPeersPerRoom   int           `koanf:"max_peers_per_room"`
	PeerHandleFormat  string        `koanf:"peer_handle_format"`
	RoomTimeout       time.Duration `koanf:"room_timeout"`
	RoomAge           time.Duration `koanf:"room_age"`
	SessionCookie     string        `koanf:"session_cookie"`
	Storage           string        `koanf:"storage"`
}

Config represents the app configuration.

type Hub

type Hub struct {
	Store store.Store
	// contains filtered or unexported fields
}

Hub acts as the controller and container for all chat rooms.

func NewHub

func NewHub(cfg *Config, store store.Store, l *log.Logger) *Hub

NewHub returns a new instance of Hub.

func (*Hub) ActivateRoom

func (h *Hub) ActivateRoom(id string) (*Room, error)

ActivateRoom loads a room from the store into the hub if it's not already active.

func (*Hub) AddRoom

func (h *Hub) AddRoom(name string, password []byte) (*Room, error)

AddRoom creates a new room in the store, adds it to the hub, and returns the room (which has to be .Run() on a goroutine then).

func (*Hub) GetRoom

func (h *Hub) GetRoom(id string) *Room

GetRoom retrives an active room from the hub.

type Peer

type Peer struct {
	// Peer's chat handle.
	ID     string
	Handle string
	// contains filtered or unexported fields
}

Peer represents an individual peer / connection into a room.

func (*Peer) RunListener

func (p *Peer) RunListener()

RunListener is a blocking function that reads incoming messages from a peer's WS connection until its dropped or there's an error. This should be invoked as a goroutine.

func (*Peer) RunWriter

func (p *Peer) RunWriter()

RunWriter is a blocking function that writes messages in a peer's queue to the peer's WS connection. This should be invoked as a goroutine.

func (*Peer) SendData

func (p *Peer) SendData(b []byte)

SendData queues a message to be written to the peer's WS.

type Room

type Room struct {
	ID       string
	Name     string
	Password []byte
	// contains filtered or unexported fields
}

Room represents a chat room.

func NewRoom

func NewRoom(id, name string, password []byte, h *Hub) *Room

NewRoom returns a new instance of Room.

func (*Room) AddPeer

func (r *Room) AddPeer(id, handle string, ws *websocket.Conn)

AddPeer adds a new peer to the room given a WS connection from an HTTP handler.

func (*Room) Broadcast

func (r *Room) Broadcast(data []byte, record bool)

Broadcast broadcasts a message to all connected peers.

func (*Room) Dispose

func (r *Room) Dispose()

Dispose signals the room to notify all connected peer messages, and dispose of itself.

Jump to

Keyboard shortcuts

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