broadcast

package
v0.0.0-...-0ddd045 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package broadcast provides utilities to broadcast messages Inspired by https://github.com/DATA-DOG/golang-websocket-hub and https://github.com/gorilla/websocket/tree/master/examples/chat

Index

Constants

This section is empty.

Variables

View Source
var (
	ReadBufferSize  int = 1024
	WriteBufferSize int = 1024
)
View Source
var (
	ErrInvalidToken             = errors.New("invalid-token")
	ErrInvalidMessageType       = errors.New("invalid-message-type")
	ErrMessageTypeAlreadyExists = errors.New("message-type-already-exists")
)

Functions

This section is empty.

Types

type AuthenticationMessage

type AuthenticationMessage struct {
	Username string `json:"username"`
	Token    string `json:"token"`
}

func (*AuthenticationMessage) Handle

func (a *AuthenticationMessage) Handle(h *Hub) error

Handle handles the authentication of a user

func (*AuthenticationMessage) New

func (a *AuthenticationMessage) New() func() Message

New returns a function to create a new AuthenticationMessage

func (*AuthenticationMessage) Type

func (a *AuthenticationMessage) Type() string

Type returns the type of this message as a string

type AvailabilityChange

type AvailabilityChange struct {
	Username string                      `json:"username"`
	State    constants.AvailabilityState `json:"state"`
}

AvailabilityChange defines the event when the availability status of a user changes.

func (*AvailabilityChange) Handle

func (a *AvailabilityChange) Handle(h *Hub) error

Handle handles the change of the availability state of one user

func (*AvailabilityChange) New

func (a *AvailabilityChange) New() func() Message

New returns a function to create a new AvailabilityChange message

func (*AvailabilityChange) Type

func (a *AvailabilityChange) Type() string

Type returns the type of this message as a string

type Hub

type Hub struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Hub is a broadcasting hub to deliver real time chat messages

func NewHub

func NewHub(logger *log.Logger) *Hub

NewHub returns a new messaging hub

func (*Hub) AuthenticatePeer

func (h *Hub) AuthenticatePeer(username, token string) error

AuthenticatePeer authenticates a peer undentified by username with the provided token. If the authentication fails, an error is returned

func (*Hub) Broadcast

func (h *Hub) Broadcast(m *Message)

Broadcast broadcasts a message

func (*Hub) NewPeer

func (h *Hub) NewPeer(username string, w http.ResponseWriter, r *http.Request) (*Peer, error)

NewPeer creates, registers and returns a new peer. If opening the websocket connection fails, an error is returned

func (*Hub) RegisterMessages

func (h *Hub) RegisterMessages(messages []Message) error

RegisterMessages registers an array of messages

func (*Hub) Run

func (h *Hub) Run() error

Run registers all messages and starts the main loop

func (*Hub) Send

func (h *Hub) Send(m *Message)

Send sends the message to one or more receivers

func (*Hub) Token

func (h *Hub) Token(key string) (string, error)

Token returns a cryptographically secure random string. If the generation fails, an error is returned

type Message

type Message interface {
	// Handle handles the execution of message related tasks. If the tasks fail, an error
	// is returned
	Handle(*Hub) error

	// Type returns the type of the message as a string
	Type() string

	// New returns a constructor to initialize a new message
	New() func() Message
}

Message defines an interface for a more specific message

func AllMessages

func AllMessages() []Message

AllMessages returns a slice of all available message types

type Peer

type Peer struct {
	Username string
	Token    string
	// contains filtered or unexported fields
}

Peer describes one client connected to the hub. Each peer has a unique username to identify itself, a token to authenticate and the underlying websocket connection for real-time communication

func (*Peer) Authenticate

func (p *Peer) Authenticate(token string) error

Authenticate authenticates a peer. If the authentication fails, an error is returned

func (*Peer) Listen

func (p *Peer) Listen()

Listen starts the listing process of listening for incoming and outgoing messages

type TextMessage

type TextMessage struct {
}

func (*TextMessage) Handle

func (t *TextMessage) Handle(h *Hub) error

Handle handles the forwarding of a text message

func (*TextMessage) New

func (t *TextMessage) New() func() Message

New returns a function to create a new TextMessage

func (*TextMessage) Type

func (t *TextMessage) Type() string

Type returns the type of this message as a string

type Typed

type Typed struct {
	Type string          `json:"type"`
	Data json.RawMessage `json:"data"`
}

Typed is a generic typed message

func ToTyped

func ToTyped(message Message) (Typed, error)

ToTyped returns the specific message as a generic typed message

func (Typed) ToMessage

func (t Typed) ToMessage(h *Hub) (Message, error)

ToMessage returns the generic typed message as a specific message

type TypingChange

type TypingChange struct {
	Scope    string                `json:"scope"`
	Username string                `json:"username"`
	State    constants.TypingState `json:"state"`
}

func (*TypingChange) Handle

func (t *TypingChange) Handle(h *Hub) error

Handle handles the change of the typing state of one user

func (*TypingChange) New

func (t *TypingChange) New() func() Message

New returns a function to create a new TypingChange message

func (*TypingChange) Type

func (t *TypingChange) Type() string

Type returns the type of this message as a string

Jump to

Keyboard shortcuts

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