client

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2025 License: None detected not legal advice Imports: 0 Imported by: 0

README ΒΆ

Client Package

The client package represents a single active WebSocket connection to the server. It is responsible for sending and receiving messages on behalf of a user and acts as the communication bridge between the frontend and the hub.

Architecture

Responsibilities
  • Manage the lifecycle of an individual WebSocket connection.
  • Read and deserialize incoming messages from the client.
  • Send messages asynchronously via a buffered channel.
  • Pass valid messages to the hub for routing and broadcasting.
  • Identify the source client type using OAuth client ID (e.g., ChatClient, WebClient).
  • Track connection timestamps for session analytics.
Key Struct
  • Client:
    • Username: user-visible name.
    • Sub: stable unique identifier (from OAuth provider).
    • ClientID: identifies the type of application used (admin dashboard, chat UI, etc.).
    • Conn: the active WebSocket connection.
    • Send: channel for outgoing messages.
    • Hub: reference to the central HubInterface.
    • ConnectedAt: timestamp of connection start.

πŸ” Workflow

  1. Connection Setup:

    • A Client is created with an active WebSocket connection and registered with the hub.
  2. Message Receiving (ReadPump):

    • Runs in a goroutine.
    • Listens for JSON messages from the WebSocket.
    • Deserializes into a lightweight struct.
    • Constructs appropriate BaseMessage objects based on message type.
    • Sends the message to the hub for processing.
  3. Message Sending (WritePump):

    • Also runs in a goroutine.
    • Listens on the Send channel.
    • Encodes BaseMessage as JSON and writes it to the WebSocket.
    • Handles cleanup on failure or disconnect.
  4. Disconnection:

    • Triggers unregistration from the hub.
    • Closes the connection and the send channel.

Usage Example

client := &Client{
    Username: "alice",
    Sub: "user-alice-123",
    ClientID: "ChatClient",
    Conn: conn, // *websocket.Conn
    Send: make(chan messages.BaseMessage, 16),
    Hub: hub,
}

go client.ReadPump()
go client.WritePump()

hub.Register <- client

πŸ“ TODO

  • Optionally support a shutdown message or ping/pong handling to detect dead clients early.
  • Add per-client rate-limiting or mute functionality at the client level.
  • Log dropped messages if the Send channel is full or unresponsive.

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type Client ΒΆ

type Client struct {
	Username    string
	Conn        *websocket.Conn
	Send        chan messages.BaseMessage
	Hub         interfaces.HubInterface
	Sub         string // Keycloak stable user ID
	ClientID    string // OAuth client ID, e.g., "ChatClient" or "WebClient"
	ConnectedAt time.Time
}

Client represents a single WebSocket connection from a user. It manages receiving and sending messages to/from the server.

func (*Client) CloseSendChannel ΒΆ

func (c *Client) CloseSendChannel()

CloseSendChannel closes the client's outgoing message channel.

func (*Client) GetClientID ΒΆ

func (c *Client) GetClientID() string

GetClientID returns the OAuth client ID used to identify the source application.

func (*Client) GetConnectedAt ΒΆ

func (c *Client) GetConnectedAt() time.Time

GetConnectedAt returns the timestamp when the client connected.

func (*Client) GetID ΒΆ

func (c *Client) GetID() string

GetID returns the stable user ID (usually from Keycloak).

func (*Client) GetUsername ΒΆ

func (c *Client) GetUsername() string

GetUsername returns the client's username.

func (*Client) ReadPump ΒΆ

func (c *Client) ReadPump()

ReadPump listens for incoming messages from the WebSocket and processes them. Parsed messages are sent to the hub for broadcast or private delivery.

func (*Client) SendMessage ΒΆ

func (c *Client) SendMessage(msg messages.BaseMessage)

SendMessage places a message into the send channel to be picked up by WritePump().

func (*Client) StartConnectionTimer ΒΆ

func (c *Client) StartConnectionTimer()

StartConnectionTimer records the time when the client connects.

func (*Client) WritePump ΒΆ

func (c *Client) WritePump()

WritePump listens for messages on the send channel and writes them to the WebSocket. It ensures that outgoing messages are sent asynchronously.

Jump to

Keyboard shortcuts

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