Giter Club home page Giter Club logo

websocket-rpc's Introduction

WebSocketRPC logo

NuGet packages version NuGet packages version NuGet packages version

WebSokcetRPC - RPC over WebSocket for .NET
WebSocket RPC library for .NET with auto JavaScript client code generation, supporting ASP.NET Core.

Tutorial: CodeProject article

Why WebSocketRPC ?

  • Lightweight
    The only dependency is JSON.NET library used for serialization/deserialization.

  • Simple
    There are only two relevant methods: Bind for binding object/interface onto a connection, and CallAsync for making RPCs.

  • Use 3rdParty assemblies as API(s)
    Implemented API, if used only for RPC, does not use anything from the library.

  • Automatic JavaScript code generation
    The JavaScript WebSocket client code is automatically generated (with JsDoc comments) from an existing .NET interface (API contract).

Check the samples by following the link above. The snippets below demonstrate the base RPC functionality.

1) .NET <- .NET

The server implements a math API containing a single function.

Server (C#)

//server's API
class MathAPI //:IMathAPI
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

...
//run the server and bind the local and remote API to a connection
Server.ListenAsync(8000, CancellationToken.None, 
                   (c, wc) => c.Bind<MathAPI>(new MathAPI()))
      .Wait(0);

Client (C#)

//server's API contract
interface IMathAPI
{
    int Add(int a, int b);
}

...
//run the client and bind the APIs to the connection
Client.ConnectAsync("ws://localhost:8000/", CancellationToken.None, 
                    (c, ws) => c.Bind<IMathAPI>())
      .Wait(0);
      
...
//make an RPC (there is only one connection)
var r = await RPC.For<IMathAPI>().CallAsync(x => Add(5, 3)); 
Console.WriteLine("Result: " + r.First()); //Output: 'Result: 8'

2) .NET <- JavaScript

The server's code is the same, but the client is written in JavaScript. The support is given by the WebSocketRPC.JS package.

Server (C#)

//the server code is the same as in the previous sample

//generate JavaScript client (file)
var code = RPCJs.GenerateCallerWithDoc<MathAPI>();
File.WriteAllText("MathAPI.js", code);

Client (JavaScript)

//init API
var api = new MathAPI("ws://localhost:8000");

//connect and excecute (when connection is opened)
api.connect(async () => {
   var r = await api.add(5, 3);
   console.log("Result: " + r);
});

3) ASP.NET Core

To incorporate server's code into the ASP.NET Core use WebSocketRPC.AspCore package. The initialization is done in a startup class in the Configure method. Everything the rest is the same.

class Startup
{
    public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
    {
        //the MVC initialization, etc.

        //initialize web-sockets
        app.UseWebSockets();
        //define route for a new connection and bind the API
        app.MapWebSocketRPC("/mathAPI", (httpCtx, c) => c.Bind<MathAPI>(new MathAPI()));
    }
}  

Related Libraries

SimpleHTTP library - adds the HTTP listener functionality (see the article).

How to Engage, Contribute and Provide Feedback

Remember: Your opinion is important and will define the future roadmap.

  • questions, comments - Github
  • spread the word

Final word

If you like the project please star it in order to help to spread the word. That way you will make the framework more significant and in the same time you will motivate me to improve it, so the benefit is mutual.

websocket-rpc's People

Contributors

dajuric avatar vacamra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

websocket-rpc's Issues

Abnormal client termination not reported on server end (Using ASPNET middleware)

So I have been playing around with this library in dotnet core, works great so far, except one thing I can't seem to get to work is how to be notified when a client (console app in my case for now) terminates abnormally. I need to know when a client connection closes (for whatever reason) so that the server end can react to it.

I'm using WebSocketRPC.AspCore v1.0.6, and calling:
`
app.UseWebSockets();
app.MapWebSocketRPC(
new PathString("/webservice"),
(ctx, c) =>
{
try
{
var b = c.Bind<WebService, IWebCallback>(new WebService());
c.OnOpen += async () =>
{
// gets called
};
c.OnClose += async (status, s) =>
{
// does not get called (only when client actually calls Connection.CloseAsync())
};
c.OnError += async ex =>
{
// Also does not get called when client abnormally closes (ie when i kill it)
};
}
catch(Exception ex)
{
}

            });

`

Is it available for .Net 4.6?

I can not install this NuGet for standart .net project on .Net 4.6.1

It says, that my version is not available :\

Is this lib available only for .Net 4.7? (I'm using visual studio)

Standalone .Net Standard 2.0 NuGet Package

Hi Darko,
I realized before that the Standalone package is not directly supporting .NET Standard 2.0 VS projects. That's why I had to create my own project and add your source code manually. Would be a lot smoother if there was a .NET Standard 2 version too.

Thanks

/Anders

WSS Support

Asp.net 2.1 defaults to using https, so the library is not compatible. Plans to update?

Invoking Client Functions?

I did a quick look at the code and samples and didn't see anything about invoking client-side functions from the server. Is this supported?

Sending and receiving complex objects

When a client receives a complex object from the server, the properties of the complex object are all null. Any idea why? Sending plain strings works fine.

var responses = await RPC.For().CallAsync(x => x.LoadPlayer(1234))
var response = responses.First();
response.PlayerName == null?????

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.