chat

package
v0.0.0-...-5cdd8e1 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package chat provides a chat room implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ServeWs

func ServeWs(room *Room, w http.ResponseWriter, r *http.Request)

ServeWs handles websocket requests from the peer.

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 NewClient

func NewClient(name string, room *Room, conn *websocket.Conn) *Client

NewClient creates a new client.

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 NewClients

func NewClients(roomName string) *Clients

NewClients creates a new client store.

func (*Clients) Add

func (cs *Clients) Add(c *Client)

Add adds a client to the room.

func (*Clients) GetClients

func (cs *Clients) GetClients() map[*Client]bool

GetClients returns all the clients in the room.

func (*Clients) NumClients

func (cs *Clients) NumClients() int

NumClients returns the number of clients in the room.

func (*Clients) Remove

func (cs *Clients) Remove(c *Client)

Remove removes a client from 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) JSON

func (m *Message) JSON() ([]byte, error)

JSON serializes the message to JSON.

func (*Message) Marshal

func (m *Message) Marshal() ([]byte, error)

Marshal serializes the message.

func (*Message) SetParticipants

func (m *Message) SetParticipants(clients *Clients)

SetParticipants includes the participant list in the message.

func (*Message) Unmarshal

func (m *Message) Unmarshal(b []byte) error

Unmarshal deserializes 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 NewRoom

func NewRoom(ctx context.Context, cancel context.CancelFunc, name string, pubsub PubSubber) *Room

NewRoom creates a new room.

func (*Room) Broadcast

func (r *Room) Broadcast(msg []byte)

Broadcast sends a message to all clients in the room.

func (*Room) BroadcastMessage

func (r *Room) BroadcastMessage(m Message)

BroadcastMessage sends a message to all clients in the room.

func (*Room) Run

func (r *Room) Run(rooms *Rooms)

Run runs the room.

func (*Room) Subscribe

func (r *Room) Subscribe()

Subscribe subscribes to the pubsub topic for each room.

type Rooms

type Rooms struct {
	// contains filtered or unexported fields
}

Rooms maintains the set of active rooms.

func NewRooms

func NewRooms() *Rooms

NewRooms creates a new rooms store.

func (*Rooms) Add

func (rs *Rooms) Add(name string, room *Room)

Add adds a room to the room store.

func (*Rooms) Get

func (rs *Rooms) Get(name string) (*Room, bool)

Get returns a room based on the room name.

func (*Rooms) GetAll

func (rs *Rooms) GetAll() map[string]*Room

GetAll returns all the rooms.

func (*Rooms) Remove

func (rs *Rooms) Remove(name string)

Remove removes a room from the room store.

Jump to

Keyboard shortcuts

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