websockets

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2025 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FlagMessage    = "flag"
	FlagResponse   = "flag_response"
	ConfigMessage  = "config"
	ConfigResponse = "config_response"
)
View Source
const (
	ConnectionLifetime = 24 * time.Hour // Lifetime of the connection
)

Variables

View Source
var (
	ErrEventNotSupported = errors.New("this event type is not supported")
	ErrConnectionTimeout = errors.New("connection timeout exceeded")
)

Functions

func ConfigHandler

func ConfigHandler(event Event, client *Client) error

func CookieAuthMiddleware

func CookieAuthMiddleware(c *fiber.Ctx) error

func FlagHandler

func FlagHandler(event Event, client *Client) error

FlagHandler will send out a message to all other participants in the chat

func VerifyToken

func VerifyToken(token string) error

VerifyToken verifies the JWT token using the secret key

Types

type Client

type Client struct {
	Connection      *websocket.Conn
	Manager         *Manager
	Egress          chan []byte
	Closed          chan struct{}
	ConnectionTimer *time.Timer

	Number   int
	IsClosed bool
	// contains filtered or unexported fields
}

Client represents a WebSocket client connection

func NewClient

func NewClient(conn *websocket.Conn, manager *Manager) *Client

func (*Client) CloseConnection

func (client *Client) CloseConnection(reason string)

CloseConnection safely closes a client connection and prevents goroutine leaks

func (*Client) PongHandler

func (client *Client) PongHandler(pongMsg string) error

func (*Client) ReadMessages

func (client *Client) ReadMessages()

func (*Client) WriteMessages

func (client *Client) WriteMessages()

type ClientList

type ClientList map[*Client]bool

ClientList maps clients to a boolean value indicating their status

type Event

type Event struct {
	Type    string          `json:"type"`
	Payload json.RawMessage `json:"payload"`
}

type EventHandler

type EventHandler func(Event, *Client) error

EventHandler is a function type for handling WebSocket events

type Manager

type Manager struct {
	Clients  ClientList
	Handlers map[string]EventHandler
	sync.RWMutex
}

Manager handles WebSocket clients and event routing

var GlobalManager *Manager // WebSocket manager

func NewManager

func NewManager() *Manager

func (*Manager) AddClient

func (m *Manager) AddClient(client *Client)

func (*Manager) GetNextClientNumber

func (m *Manager) GetNextClientNumber() int

func (*Manager) RemoveClient

func (m *Manager) RemoveClient(client *Client)

func (*Manager) RouteEvent

func (m *Manager) RouteEvent(event Event, c *Client) error

func (*Manager) ServeWS

func (m *Manager) ServeWS(c *fiber.Ctx) error

func (*Manager) SetupEventHandlers

func (m *Manager) SetupEventHandlers()

type NewMessageEvent

type NewMessageEvent struct {
	Sent time.Time `json:"sent"`
}

NewMessageEvent represents a new message event

Jump to

Keyboard shortcuts

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