chats

package
v0.0.0-...-a24ed69 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWebsocketRequired = &api.Error{

		Code:    426,
		Message: "this endpoint requires a web socket connection",
	}

	ErrChatAlreadyExists = &api.Error{

		Code:    409,
		Message: "chat already exists",
	}
)
View Source
var (
	DefaultHandler = func() *ChatHandler {
		var (
			s *Server
		)

		var once sync.Once

		once.Do(func() {
			s = &Server{
				Chats:      make(map[uuid.UUID]*Chat),
				Register:   make(chan *Client),
				Unregister: make(chan *Client),

				Broadcast: make(chan *Message, 5),
			}
		})

		return &ChatHandler{
			Server: s,
		}
	}()
)

Functions

This section is empty.

Types

type Chat

type Chat struct {
	ID      uuid.UUID             `json:"id"`
	Clients map[uuid.UUID]*Client `json:"clients"`
}

type ChatHandler

type ChatHandler struct {
	Server *Server
	*users.UserService
	// contains filtered or unexported fields
}

func NewHandler

func NewHandler(s *Server) *ChatHandler

func (*ChatHandler) Route

func (h *ChatHandler) Route(r fiber.Router)

type Client

type Client struct {
	Connection *websocket.Conn

	Details *users.User   `json:"client_details"`
	ChatID  uuid.UUID     `json:"chat_id"`
	Message chan *Message `json:"message"`
}

func (*Client) Read

func (c *Client) Read(s *Server)

func (*Client) Write

func (c *Client) Write()

type Message

type Message struct {
	ChatID   uuid.UUID `json:"chat_id"`
	Username string    `json:"username"`
	Content  string    `json:"content"`
}

type Server

type Server struct {
	// Chats is a map where each chat
	// room is identified by its UUID.
	Chats map[uuid.UUID]*Chat

	// Register channel for new
	// clients to join a chat room.
	Register chan *Client

	// Unregister channel for
	// clients leaving a chat room.
	Unregister chan *Client

	// Broadcast channel for sending messages
	// to all clients in a chat room.
	Broadcast chan *Message
	// contains filtered or unexported fields
}

Server struct manages multiple chat rooms and handles client registration, unregistration, and message broadcasting.

func NewServer

func NewServer() *Server

NewServer initializes a new Server instance with channels and an empty map of chats.

func (*Server) Run

func (s *Server) Run()

Run is the main loop that continuously listens for Register, Unregister, and Broadcast events, delegating tasks to respective handler functions.

Jump to

Keyboard shortcuts

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