Documentation
¶
Overview ¶
Package chat provides a chat room implementation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ActionType ¶
type ActionType string
ActionType is the type of action.
const ( JoinAction ActionType = "joined" LeaveAction ActionType = "left" MessageAction ActionType = "message" )
Action types.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a middleman between the websocket connection and the room.
func (*Client) ReadSocket ¶
func (c *Client) ReadSocket()
ReadSocket reads messages from the websocket connection then broadcasts it to a room through a channel.
A ReadSocket goroutine is started for each connection. The application ensures that there is at most one reader on a connection by executing all reads from this goroutine.
func (*Client) WriteSocket ¶
func (c *Client) WriteSocket()
WriteSocket writes messages posted from a room to the websocket connection.
A WriteSocket goroutine is started for each connection. The application ensures that there is at most one writer to a connection by executing all writes from this goroutine.
type Clients ¶
type Clients struct {
// contains filtered or unexported fields
}
Clients maintains the set of active clients in a room.
func (*Clients) GetClients ¶
GetClients returns all the clients in the room.
func (*Clients) NumClients ¶
NumClients returns the number of clients in the room.
type Message ¶
type Message struct { // Room name. RoomName string // Sender name. Sender string // Message. Message []byte // Participants in the room. Participants map[string]bool // Action type. Action ActionType }
Message is a message sent from a client to a room along with its meta details.
func NewMessage ¶
func NewMessage(roomName, sender string, message []byte, action ActionType) Message
NewMessage creates a new message.
func (*Message) SetParticipants ¶
SetParticipants includes the participant list in the message.
type PubSubber ¶
type PubSubber interface { Publish(ctx context.Context, topic string, msg []byte) error Subscribe(ctx context.Context, topic string, payload chan []byte) }
PubSubber is an interface for subscribing to a room and publishing messages to it.
type Room ¶
type Room struct {
// contains filtered or unexported fields
}
Room maintains the set of active clients and broadcasts messages to them.
func (*Room) BroadcastMessage ¶
BroadcastMessage sends a message to all clients in the room.