Table of Contents

Class RustPlusSocket

Namespace
RustPlusApi
Assembly
RustPlusApi.dll

A Rust+ API client made in C#.

public abstract class RustPlusSocket : IRustPlusSocket, IDisposable, IAsyncDisposable
Inheritance
RustPlusSocket
Implements
Derived
Inherited Members

Constructors

RustPlusSocket(RustPlusConnection, RustPlusSocketOptions?, ILoggerFactory?)

A Rust+ API client made in C#.

protected RustPlusSocket(RustPlusConnection connection, RustPlusSocketOptions? options = null, ILoggerFactory? loggerFactory = null)

Parameters

connection RustPlusConnection

The server endpoint and player credentials to connect as.

options RustPlusSocketOptions

Tuning options (timeouts, keep-alive, buffer size); defaults are used when null.

loggerFactory ILoggerFactory

Routes the client's diagnostics into your logging stack; logging is disabled (a no-op NullLogger) when null.

Properties

IsConnected

Gets a value indicating whether the client is currently connected to the Rust+ socket (the underlying WebSocket is open).

public bool IsConnected { get; }

Property Value

bool

PlayerId

The Steam ID requests are issued as.

protected ulong PlayerId { get; }

Property Value

ulong

Methods

ConnectAsync(CancellationToken)

Asynchronously connects to the Rust+ server using a WebSocket. Raises Connecting before attempting to connect and Connected upon successful connection. Starts background tasks for receiving and sending messages. On failure, ErrorOccurred is raised and the exception is rethrown to the caller. An instance can reconnect: after DisconnectAsync(bool) (or a dropped connection), calling this again opens a fresh socket; the previous one is released, never leaked.

public Task ConnectAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A token to cancel the connection attempt.

Returns

Task

Exceptions

ObjectDisposedException

Thrown when the client has been disposed.

InvalidOperationException

Thrown when the client is already connected.

WebSocketException

Thrown when the WebSocket connect fails (also raised via ErrorOccurred).

DisconnectAsync(bool)

Asynchronously disconnects from the Rust+ server, waiting for pending responses unless forceClose is true. Raises Disconnecting before disconnecting and Disconnected after.

public Task DisconnectAsync(bool forceClose = false)

Parameters

forceClose bool

When true, skips draining in-flight requests.

Returns

Task

Dispose()

Disposes the Rust+ API client, cancelling background work and releasing the underlying WebSocket.

public void Dispose()

Dispose(bool)

Releases the resources used by the RustPlusSocket.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

DisposeAsync()

Asynchronously disposes the client: cancels background work, awaits the tracked receive/send loops (bounded by TeardownTimeout), then releases the WebSocket. Prefer this over Dispose() so teardown deterministically drains in-flight I/O instead of abandoning it.

public ValueTask DisposeAsync()

Returns

ValueTask

DisposeCoreAsync()

Cancels the instance token, awaits the tracked background loops (bounded), and releases resources. Override to extend async teardown in derived classes.

protected virtual ValueTask DisposeCoreAsync()

Returns

ValueTask

GetErrorMessage(AppMessage)

Retrieves the error message from the specified RustPlusContracts.AppMessage response.

protected static string GetErrorMessage(AppMessage response)

Parameters

response AppMessage

The RustPlusContracts.AppMessage containing the response data.

Returns

string

The server's error string when the response carries an error; otherwise "unknown-error" (a response without an error payload should never reach this method, so don't invent a specific reason for it).

IsError(AppMessage)

Determines whether the specified RustPlusContracts.AppMessage response contains an error. Returns false if the message is a broadcast without a response.

protected static bool IsError(AppMessage response)

Parameters

response AppMessage

The RustPlusContracts.AppMessage to check.

Returns

bool

True if the response contains an error; otherwise, false.

ParseNotification(AppBroadcast?)

Parses and handles a broadcast notification received from the Rust+ server. Intended to be overridden in derived classes to implement custom notification handling logic.

protected virtual void ParseNotification(AppBroadcast? broadcast)

Parameters

broadcast AppBroadcast

The RustPlusContracts.AppBroadcast received from the server.

SendRequestAsync(AppRequest, Func<AppBroadcast, bool>?, CancellationToken)

Asynchronously sends a request to the Rust+ server and awaits the response correlated by sequence number. Raises SendingRequest before sending and RequestSent after. The wait honors cancellationToken, the instance token, and a default timeout; on cancellation or timeout the pending entry is removed and the task faults with a clear exception.

public Task<AppMessage> SendRequestAsync(AppRequest request, Func<AppBroadcast, bool>? broadcastReplyMatcher = null, CancellationToken cancellationToken = default)

Parameters

request AppRequest

The RustPlusContracts.AppRequest to send.

broadcastReplyMatcher Func<AppBroadcast, bool>

When non-null, the success reply is delivered as a broadcast (no seq); the first incoming broadcast this predicate matches resolves the request. Unrelated broadcasts (other players' messages, other entities) are left to the notification pipeline.

cancellationToken CancellationToken

A token to cancel waiting for the response.

Returns

Task<AppMessage>

A task that represents the asynchronous operation and contains the RustPlusContracts.AppMessage response.

Exceptions

TimeoutException

Thrown when no response arrives within the request timeout.

Events

Connected

Occurs when the client has successfully connected to the Rust+ server.

public event EventHandler? Connected

Event Type

EventHandler
See Also

Connecting

Occurs when the client is about to connect to the Rust+ server.

public event EventHandler? Connecting

Event Type

EventHandler
See Also

Disconnected

Occurs when the client has disconnected from the Rust+ server.

public event EventHandler? Disconnected

Event Type

EventHandler
See Also

Disconnecting

Occurs when the client is about to disconnect from the Rust+ server.

public event EventHandler? Disconnecting

Event Type

EventHandler
See Also

ErrorOccurred

Occurs when an error is encountered during communication with the Rust+ server.

public event EventHandler<Exception>? ErrorOccurred

Event Type

EventHandler<Exception>
See Also

MessageReceived

Occurs when a message is received from the Rust+ server.

public event EventHandler<AppMessage>? MessageReceived

Event Type

EventHandler<AppMessage>
See Also
AppMessage

NotificationReceived

Occurs when a notification (broadcast) is received from the Rust+ server.

public event EventHandler<AppMessage>? NotificationReceived

Event Type

EventHandler<AppMessage>
See Also
AppMessage

RequestSent

Occurs after a request has been sent to the Rust+ server.

public event EventHandler<AppRequest>? RequestSent

Event Type

EventHandler<AppRequest>
See Also

ResponseReceived

Occurs when a response is received from the Rust+ server.

public event EventHandler<AppMessage>? ResponseReceived

Event Type

EventHandler<AppMessage>
See Also
AppMessage

SendingRequest

Occurs when a request is about to be sent to the Rust+ server.

public event EventHandler? SendingRequest

Event Type

EventHandler
See Also