interfaces

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 ¶

Interfaces Package

The interfaces package defines contracts for the core components of the chat server, specifically ClientInterface and HubInterface. These interfaces promote decoupling and testability by abstracting the concrete behavior of clients and hubs.

Purpose

This package prevents circular dependencies between components like client, hub, and messages by introducing a layer of abstraction. It allows:

  • The hub to interact with any type that satisfies ClientInterface, without needing to import the client package.
  • The client to interact with any hub implementation that satisfies HubInterface.

Interfaces

ClientInterface

Represents a single user session over a WebSocket.

Method Description
GetUsername() Returns the client's display name.
SendMessage(msg) Sends a message to the client’s send channel.
CloseSendChannel() Closes the channel used to deliver outgoing messages.
GetID() Returns the stable user ID (e.g., from Keycloak).
GetClientID() Returns the OAuth client ID indicating the source app (e.g., WebClient, ChatClient).
StartConnectionTimer() Records the connection start time for session logging.
GetConnectedAt() Returns the timestamp of when the client connected.
HubInterface

Defines behavior expected from the central message dispatcher and coordinator.

Method Description
Broadcast(msg) Sends a message to all connected clients.
Whisper(msg) Sends a private message between clients.
RegisterClient(client, id) Registers a client with a unique connection ID.
UnregisterClient(client, id) Removes a client from the hub and ends their session.
SendMessage(msg) Pushes a message into the hub’s processing loop.
GetConnectedUsers() Returns all currently connected users.
GetCachedChatMessages() Retrieves recent messages from the message cache.
FindUsernameByUserID(id) Resolves a user ID to a username, if connected.

Use Cases

  • The hub package depends on ClientInterface to avoid importing the concrete client package.
  • The client package depends on HubInterface to send and receive routed messages without tight coupling.

📝 TODO

  • Add Disconnect() to ClientInterface to enable graceful shutdowns or ban logic.
  • Expand HubInterface with scene/channel routing if multi-room support is added.
  • Create mock implementations for use in unit tests.

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type ClientInterface ¶

type ClientInterface interface {
	// GetUsername returns the client's username.
	GetUsername() string

	// SendMessage sends a message to the client over the websocket.
	SendMessage(messages.BaseMessage)

	// CloseSendChannel closes the client's outgoing message channel.
	CloseSendChannel()

	// GetID returns the stable user ID (e.g., from Keycloak).
	GetID() string

	// GetClientID returns the OAuth client ID used to identify the source application,
	// such as "ChatClient" or "WebClient".
	GetClientID() string

	// StartConnectionTimer records the time the client connected.
	StartConnectionTimer()

	// GetConnectedAt returns the timestamp when the client connected.
	GetConnectedAt() time.Time
}

ClientInterface defines the contract for a client that connects to the hub over a websocket and can send and receive messages.

type HubInterface ¶

type HubInterface interface {
	// Broadcast sends a message to all connected clients.
	Broadcast(messages.BaseMessage)

	// Whisper sends a private message to a specific client.
	Whisper(messages.BaseMessage)

	// RegisterClient adds a client to the hub, associating it with a unique client ID.
	RegisterClient(ClientInterface, string)

	// UnregisterClient removes a client from the hub using the provided client ID.
	UnregisterClient(ClientInterface, string)

	// SendMessage sends a message into the hub’s internal message loop for processing.
	SendMessage(messages.BaseMessage)

	// GetConnectedUsers returns a list of users currently connected to the hub.
	GetConnectedUsers() []chat.UserStatusPayload

	// GetCachedChatMessages returns a list of recent chat messages from the cache.
	GetCachedChatMessages() []models.ChatMessage

	// FindUsernameByUserID returns the username associated with the given user ID, if any.
	FindUsernameByUserID(userID string) (string, bool)
}

HubInterface defines the contract for a Hub that manages connected clients, message broadcasting, private messaging, and cached message retrieval.

Jump to

Keyboard shortcuts

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