Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents a WebRCON connection to a Rust server. It manages the connection lifecycle, sending commands, and handling background tasks like reconnections and pings.
func NewClient ¶
func NewClient(opts ClientOptions) *Client
NewClient initializes a new WebRCON client with the given options. If the provided options lack a Logger, it falls back to the DefaultOptions.
func (*Client) Close ¶
func (c *Client) Close()
Close gracefully terminates the connection and background routines. It cancels the internal context, causing the read and write pumps to shut down safely.
func (*Client) Connect ¶
func (c *Client) Connect()
Connect starts the background connection and event loops. This function initializes the connection asynchronously and manages its own reconnections via the ConnectionManager.
func (*Client) SendCommand ¶
SendCommand sends a command synchronously, waiting for a response with the same Identifier. It returns the exact string response from the server or an error if the context times out or the connection drops.
func (*Client) SetBroadcastCallback ¶
func (c *Client) SetBroadcastCallback(callback func(RustPayload))
SetBroadcastCallback registers a function to be called when an unsolicited message is received. Unsolicited messages include server chat, console logs, and any payload that does not match a pending synchronous command's Identifier.
type ClientOptions ¶
type ClientOptions struct {
IP string
Port int
Password string
Name string // Application name to send in payloads
RateLimiter RateLimiter
Logger *slog.Logger
// MaxRetryBackoff is the maximum time to wait before retrying a connection.
MaxRetryBackoff time.Duration
// ReadBufferSize limits the size of the buffer for reading responses.
ReadBufferSize int
// WriteBufferSize limits the size of the buffer for sending requests.
WriteBufferSize int
}
ClientOptions defines configuration for the WebRCON client. It includes connection credentials, rate limiting settings, logging, and buffer sizes.
func DefaultOptions ¶
func DefaultOptions(ip string, port int, password string) ClientOptions
DefaultOptions returns ClientOptions with sensible defaults. It configures an in-memory rate limiter (20 burst, 10 events/sec), standard slog logger, and a 30-second max reconnection backoff.
type ConnectionManager ¶
type ConnectionManager struct {
// contains filtered or unexported fields
}
ConnectionManager handles the lifecycle of the WebSocket connection.
func (*ConnectionManager) Start ¶
func (cm *ConnectionManager) Start()
type DefaultRateLimiter ¶
type DefaultRateLimiter struct {
// contains filtered or unexported fields
}
DefaultRateLimiter uses the standard x/time/rate package to provide an in-memory sliding window/token bucket rate limiter.
func NewDefaultRateLimiter ¶
func NewDefaultRateLimiter(burst int, eventsPerSecond float64) *DefaultRateLimiter
NewDefaultRateLimiter creates a new in-memory rate limiter. burst specifies maximum burst size, and eventsPerSecond specifies operations per second.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine manages pending synchronous requests and async event dispatching.
func (*Engine) Dispatch ¶
func (e *Engine) Dispatch(payload RustPayload)
Dispatch processes an incoming payload from the read pump.
func (*Engine) SendCommandSync ¶
SendCommandSync enqueues a command and waits for the specific response.
func (*Engine) SetBroadcastCallback ¶
func (e *Engine) SetBroadcastCallback(cb func(RustPayload))
SetBroadcastCallback sets the callback.
type RateLimiter ¶
type RateLimiter interface {
// Wait blocks until a rate limit token is available or context is cancelled.
Wait(ctx context.Context) error
}
RateLimiter defines the interface for rate limiting outbound commands. It allows users to plug in custom implementations (e.g., Redis-based limiters for distributed setups) or use the default in-memory token bucket.
type RustPayload ¶
type RustPayload struct {
Identifier int `json:"Identifier"`
Message string `json:"Message"`
Name string `json:"Name"`
}
RustPayload represents the exact JSON structure required by Rust's WebRCON. The Identifier field is used to map server responses to the original client requests.