Skip to content

KAW0/RxSocket

 
 

Repository files navigation

RxSocket   Build status NuGet License

Minimal Reactive Socket Implementation

  • asynchronous connect
  • observable accept and receive
  • supports .NET Standard 2.0
  • dependencies: Reactive Extensions 4
  • simple and intuitive API
  • tested
  • fast

server

interface IRxSocketServer: IDisposable
{
    IObservable<IRxSocketClient> AcceptObservable { get; }
}
// Create a socket server on the Endpoint.
IRxSocketServer server = RxSocketServer.Create(IPEndPoint);

// Start accepting connections from clients.
server.AcceptObservable.Subscribe(onNext: acceptClient =>
{
    acceptClient.ReceiveObservable.ToStrings().Subscribe(onNext: message =>
    {
        // Echo received messages back to the client.
        acceptClient.Send(message.ToByteArray());
    });
});

client

interface IRxSocketClient: IDisposable
{
    bool Connected { get; }
    void Send(byte[] buffer, int offset = 0, int length = 0);
    IObservable<byte> ReceiveObservable { get; }
}
// Create a socket client by connecting to the server at EndPoint.
IRxSocketClient client = await RxSocketClient.ConnectAsync(IPEndPoint);

client.ReceiveObservable.ToStrings().Subscribe(onNext: message =>
{
    // Receive message from the server.
    Assert.Equal("Hello!", message);
});

// Send a message to the server.
client.Send("Hello!".ToByteArray());

// Wait for the message to be received by the server and sent back to the client.
await Task.Delay(100);

client.Dispose();
server.Dispose();

About

A minimal reactive socket implementation.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%