A C# library for reliable WebSocket communication with the Message Broker server. Features automatic reconnection, heartbeat monitoring, request/response handling, and event-based communication.
- 🔄 Automatic reconnection with configurable delay
- 💓 Heartbeat monitoring for connection health
- 🤝
Task<T>-based request/response pattern - 📡 Event-based message handling using
SingleEmitter - 📊 Connection state monitoring and events
- ⚡ Efficient message serialization
- 📝 Fully documented with XML comments
This library is intended to be used as a NuGet package. Once published, you can install it using the .NET CLI:
dotnet add package MBClient.CSusing MBClient.CS.Core;
using MBClient.CS.types;
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// Create a client instance
var client = new WebSocketClient(new ClientConfig
{
Url = "ws://localhost:8000",
Reconnect = new ReconnectConfig { Enabled = true }
});
// Listen for connection events
client.Connected.On(() => Console.WriteLine("Connected to server!"));
client.Disconnected.On(() => Console.WriteLine("Disconnected from server."));
client.Error.On((error) => Console.WriteLine($"An error occurred: {error.Message}"));
// Connect to the server
await client.Connect();
// Make a request
try
{
var response = await client.Request("my.topic", new { some = "data" });
Console.WriteLine($"Response payload: {response.Payload}");
}
catch (Exception error)
{
Console.WriteLine($"Request failed: {error.Message}");
}
// Publish a message
await client.Publish("my.topic", new { hello = "world" });
// Keep the application running to listen for messages
await Task.Delay(-1);
}
}The main client class for WebSocket communication.
new WebSocketClient(ClientConfig config)ClientConfig options:
Url(requiredstring): WebSocket server URL.Reconnect(optionalReconnectConfig): Configuration for reconnection behavior.Enabled(bool): Whether to automatically reconnect on disconnect.MaxAttempts(optionalint): Maximum number of reconnection attempts.Delay(optionalint): Delay between reconnection attempts in milliseconds.
Task Connect(): Connects to the server.Task Disconnect(): Disconnects from the server.Task<Message<BrokerHeader, object>> Request(string topic, object payload, RequestOptions? options = null): Makes a request and waits for a response.Task Publish(string topic, object payload, PublishOptions? options = null): Publishes a message.Task Subscribe<P>(string action, string topic, int priority, Action<Message<BrokerHeader, P>>? callback): Subscribes to a topic.Task Unsubscribe(string topic, string action): Unsubscribes from a topic.
Connected:SingleEmitter<Action>- Fires when a connection is established.Disconnected:SingleEmitter<Action>- Fires when the connection is lost.Error:SingleEmitter<Action<Exception>>- Fires when a connection or message error occurs.Reconnecting:SingleEmitter<Action<(int attempt, int maxAttempts)>>- Fires when a reconnection attempt is made.Message:SingleEmitter<Action<Message<BrokerHeader, object>>>- Fires for every message received from the server.
Messages are serialized in the following format:
action:topic:version[:requestId[:parentRequestId[:timeout]]]
{"key": "value"}
Where:
- First line: Header with action, topic, version, and optional requestId.
- Second line: JSON payload.
CONNECTED- Successfully connected to the server.CONNECTING- Initial connection attempt.RECONNECTING- Attempting to reconnect.DISCONNECTED- Not connected to the server.
# Restore dependencies
dotnet restore
# Build the library
dotnet build
# Run tests (once a test project is set up)
dotnet test- Fork the repository.
- Create your feature branch (
git checkout -b feature/amazing-feature). - Commit your changes (
git commit -m 'Add some amazing feature'). - Push to the branch (
git push origin feature/amazing-feature). - Open a Pull Request.
This project is licensed under the Apache-2.0 License - see the LICENSE file for details.