Documentation ¶
Index ¶
- Constants
- type Broker
- type BrokerMessage
- type ChatHandler
- type ChatMessage
- func NewAckMessage(messageID *uuid.UUID, clientID uuid.UUID) *ChatMessage
- func NewConnectionMessage(messageID *uuid.UUID, clientID uuid.UUID, payload ConnectionMessagePayload) *ChatMessage
- func NewErrorMessage(messageID *uuid.UUID, clientID uuid.UUID, payload ErrorMessagePayload) *ChatMessage
- func NewReceiveMessage(messageID *uuid.UUID, clientID uuid.UUID, payload ReceiveMessagePayload) *ChatMessage
- func NewRegistrationMessage(messageID *uuid.UUID, clientID uuid.UUID) *ChatMessage
- func NewSendMessage(messageID *uuid.UUID, clientID uuid.UUID, payload SendMessagePayload) *ChatMessage
- type Client
- type ConnectionMessagePayload
- type ErrorMessagePayload
- type ReceiveMessagePayload
- type RedisBroker
- func (b *RedisBroker) Poll(ctx context.Context) error
- func (b *RedisBroker) PumpMessages(channelsPattern string, out chan *redis.PMessage)
- func (b *RedisBroker) Register(client *Client) error
- func (b *RedisBroker) Send(receiverID uuid.UUID, message *BrokerMessage) error
- func (b *RedisBroker) Unregister(client *Client) error
- type SendMessagePayload
- type Server
Constants ¶
const ( // ErrorMessageKind is returned whenever an error prevents the node from processing a request. ErrorMessageKind = "error" // RegistrationKind is sent by a Client on its first connection to a server. RegistrationKind = "registration" // ConnectionMessageKind is sent a Client in response to a RegisterRequestKind. The response includes its ClientID. ConnectionMessageKind = "connection" // SendMessageKind is sent by a Client whenever it wants to transmit a message to another Client. SendMessageKind = "send" // ReceiveMessageKind is sent by a Server to a Client, when another Client wants to transmit a message to them. ReceiveMessageKind = "receive" // AcknowledgeMessageKind is sent after a SendMessageKind or a ReceiveMessageKind was properly processed by the node. AcknowledgeMessageKind = "ack" )
const RedisBrokerPrefix = "texto:"
RedisBrokerPrefix is the prefix used for all keys registered by the RedisBroker.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Broker ¶
type Broker interface { // Regsiter adds a Client to the Broker Register(client *Client) error // Unregister removes a client from the Broker Unregister(client *Client) error // Send sends the given message to the Client associated to the given ID. Send(receiverID uuid.UUID, message *BrokerMessage) error // Poll reads all incoming messages and transmit them to known Clients. Poll(ctx context.Context) error }
A Broker is responsible for transmitting messages between users.
type BrokerMessage ¶
type BrokerMessage struct { SenderID uuid.UUID RecipientID uuid.UUID Text string }
A BrokerMessage is sent between Brokers to transmit the messages to the right user.
type ChatHandler ¶
type ChatHandler struct { Log *logrus.Logger Broker Broker Upgrader websocket.Upgrader Timeout time.Duration }
ChatHandler is the HTTP Handler responsible for upgrading connections to the WebSocket Protocol and managing them.
func (*ChatHandler) ServeHTTP ¶
func (h *ChatHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP is the http.Handler implementation for ChatHandler.
type ChatMessage ¶
type ChatMessage struct { // Each client is assigned a UUID when it connects to a server. ClientID uuid.UUID `json:"client_id"` // Each message is identified by its own UUID ID uuid.UUID `json:"id"` // The kind of the message. Kind string `json:"kind"` // The actual content of the message, if any. Data interface{} `json:"data"` }
A ChatMessage conforms to the schema that the chat clients and servers use to communicate.
func NewAckMessage ¶
func NewAckMessage(messageID *uuid.UUID, clientID uuid.UUID) *ChatMessage
NewAckMessage creates a new ChatMessage of kind "acknowledge".
func NewConnectionMessage ¶
func NewConnectionMessage(messageID *uuid.UUID, clientID uuid.UUID, payload ConnectionMessagePayload) *ChatMessage
NewConnectionMessage creates a new ChatMessage of kind "connection", with a ConnectionMessagePayload.
func NewErrorMessage ¶
func NewErrorMessage(messageID *uuid.UUID, clientID uuid.UUID, payload ErrorMessagePayload) *ChatMessage
NewErrorMessage creates a new ChatMessage of kind "error", with an ErrorMessagePayload.
func NewReceiveMessage ¶
func NewReceiveMessage(messageID *uuid.UUID, clientID uuid.UUID, payload ReceiveMessagePayload) *ChatMessage
NewReceiveMessage creates a new ChatMessage of kind "send", with a ReceiveMessagePayload.
func NewRegistrationMessage ¶
func NewRegistrationMessage(messageID *uuid.UUID, clientID uuid.UUID) *ChatMessage
NewRegistrationMessage creates a new ChatMessage of kind "registration".
func NewSendMessage ¶
func NewSendMessage(messageID *uuid.UUID, clientID uuid.UUID, payload SendMessagePayload) *ChatMessage
NewSendMessage creates a new ChatMessage of kind "send", with a SendMessagePayload.
func (*ChatMessage) UnmarshalJSON ¶
func (m *ChatMessage) UnmarshalJSON(input []byte) error
MessageUnmarshalJSON unmarshals a JSON description of a ChatMessage into the given instance.
type Client ¶
type Client struct { // The universally unique ID of the user on this node. ID uuid.UUID // contains filtered or unexported fields }
A Client represents an open WebSocket connection with a user.
func (*Client) HandleMessage ¶
func (c *Client) HandleMessage(msg *ChatMessage) *ChatMessage
HandleMessage processes the given message and returns the ChatMessage that should be send back to the user.
type ConnectionMessagePayload ¶
type ConnectionMessagePayload struct {
ClientID uuid.UUID `json:"client_id"`
}
A ConnectionMessagePayload contains the id of a newly registered client.
type ErrorMessagePayload ¶
type ErrorMessagePayload struct { Code string `json:"code"` Description string `json:"description"` }
An ErrorMessagePayload contains the code and the human-readable description of an error.
type ReceiveMessagePayload ¶
type ReceiveMessagePayload struct { SenderID uuid.UUID `json:"sender_id"` Text string `json:"text"` }
A ReceiveMessagePayload contains the sender's ID and the content of the message.
type RedisBroker ¶
A RedisBroker transmits messages between users using Redis as its backend.
func NewRedisBroker ¶
func NewRedisBroker(log *logrus.Logger, addr string) (*RedisBroker, error)
NewRedisBroker creates a new RedisBroker instance, connecting to the Redis server using the given TCP address.
func (*RedisBroker) Poll ¶
func (b *RedisBroker) Poll(ctx context.Context) error
Poll reads all messages published on the Redis server. If a message is intended to a known user, the Broker will send it into the recipient's outboundChan.
func (*RedisBroker) PumpMessages ¶
func (b *RedisBroker) PumpMessages(channelsPattern string, out chan *redis.PMessage)
PumpMessages subscribe to Redis channels, reads all incoming messages and sends them into the given channel. If an error is encountered while reading the messages, the channel is closed and the function exits.
func (*RedisBroker) Register ¶
func (b *RedisBroker) Register(client *Client) error
Register registers a Client in the internal Client map of the Broker.
func (*RedisBroker) Send ¶
func (b *RedisBroker) Send(receiverID uuid.UUID, message *BrokerMessage) error
Send publishes the given message on the Redis server.
func (*RedisBroker) Unregister ¶
func (b *RedisBroker) Unregister(client *Client) error
Unregister removes a Client from the internal Client map of the Broker.
type SendMessagePayload ¶
type SendMessagePayload struct { ReceiverID uuid.UUID `json:"receiver_id"` Text string `json:"text"` }
A SendMessagePayload contains the receiver's ID and the content of the message.
type Server ¶
type Server struct { Log *logrus.Logger Broker Broker HTTPServer http.Server // contains filtered or unexported fields }
A Server bundles an HTTP Server and all the configuration required at runtime.
func NewServer ¶
func NewServer(parent context.Context, log *logrus.Logger, addr string, broker Broker) (*Server, error)
NewServer returns an initialized Server.