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 ¶
- Variables
- type AuthenticationMessage
- type AvailabilityChange
- type Hub
- func (h *Hub) AuthenticatePeer(username, token string) error
- func (h *Hub) Broadcast(m *Message)
- func (h *Hub) NewPeer(username string, w http.ResponseWriter, r *http.Request) (*Peer, error)
- func (h *Hub) RegisterMessages(messages []Message) error
- func (h *Hub) Run() error
- func (h *Hub) Send(m *Message)
- func (h *Hub) Token(key string) (string, error)
- type Message
- type Peer
- type TextMessage
- type Typed
- type TypingChange
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type AuthenticationMessage ¶
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 ¶
Hub is a broadcasting hub to deliver real time chat messages
func (*Hub) AuthenticatePeer ¶
AuthenticatePeer authenticates a peer undentified by username with the provided token. If the authentication fails, an error is returned
func (*Hub) NewPeer ¶
NewPeer creates, registers and returns a new peer. If opening the websocket connection fails, an error is returned
func (*Hub) RegisterMessages ¶
RegisterMessages registers an array of messages
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
type Peer ¶
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 ¶
Authenticate authenticates a peer. If the authentication fails, an error is returned
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 ¶
ToTyped returns the specific message as a generic typed 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