Giter Site home page Giter Site logo

reactivesockets's Introduction

reactivesockets

Implements a very easy to use sockets API based on IObservable. It allows very simple protocol implementations such as:

    var client = new ReactiveClient("127.0.0.1", 1055);
    
    // The parsing of messages is done in a simple Rx query over the receiver observable
    // Note this protocol has a fixed header part containing the payload message length
    // And the message payload itself. Bytes are consumed from the client.Receiver 
    // automatically so its behavior is intuitive.
    IObservable<string> messages = from header in client.Receiver.Buffer(4)
                                   let length = BitConverter.ToInt32(header.ToArray(), 0)
                                   let body = client.Receiver.Take(length)
                                   select Encoding.UTF8.GetString(body.ToEnumerable().ToArray());
    
    // Finally, we subscribe on a background thread to process messages when they are available
    messages.SubscribeOn(TaskPoolScheduler.Default).Subscribe(message => Console.WriteLine(message));
    client.ConnectAsync().Wait();

Creating the server implementation is equally straightforward (this is an echo server for the same message format):

        var server = new ReactiveListener(1055);
        server.Connections.Subscribe(socket =>
        {
            IObservable<string> messages = from header in socket.Receiver.Buffer(4)
                                           let length = BitConverter.ToInt32(header.ToArray(), 0)
                                           let body = socket.Receiver.Take(length)
                                           select Encoding.UTF8.GetString(body.ToEnumerable().ToArray());

            // Echo the incoming message with the same format.
            messages.Subscribe(message =>
            { 
              var body = encoding.GetBytes(message);
              var header = BitConverter.GetBytes(body.Length);
              var payload = header.Concat(body).ToArray();
              
              socket.SendAsync(payload).Wait();
            });
        });


        server.Start();

Install using: install-package reactivesockets

This library was inspired by this forum post and blog entry.

reactivesockets's People

Contributors

kzu avatar flagbug avatar gregmac avatar adalon avatar wldevries avatar

Watchers

Manabu Higashida avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.