https://github.com/dajuric/websocket-rpc
WebSocket RPC library for .NET with auto JavaScript client code generation, supporting ASP.NET Core
https://github.com/dajuric/websocket-rpc
asp-net-core dotnet javascript-client rpc websocket
Last synced: about 1 month ago
JSON representation
WebSocket RPC library for .NET with auto JavaScript client code generation, supporting ASP.NET Core
- Host: GitHub
- URL: https://github.com/dajuric/websocket-rpc
- Owner: dajuric
- License: other
- Archived: true
- Created: 2017-10-11T00:11:32.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-10-15T13:08:58.000Z (about 7 years ago)
- Last Synced: 2025-10-26T05:34:52.521Z (about 2 months ago)
- Topics: asp-net-core, dotnet, javascript-client, rpc, websocket
- Language: C#
- Homepage:
- Size: 1.48 MB
- Stars: 142
- Watchers: 14
- Forks: 22
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-websockets - websocket-rpc - WebSocket RPC library for .NET with auto JavaScript client code generation, supporting ASP.NET Core. (Tools per Language / C\#)
README
**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).
## Samples
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#)
``` csharp
//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(new MathAPI()))
.Wait(0);
```
**Client** (C#)
``` csharp
//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())
.Wait(0);
...
//make an RPC (there is only one connection)
var r = await RPC.For().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#)
``` csharp
//the server code is the same as in the previous sample
//generate JavaScript client (file)
var code = RPCJs.GenerateCallerWithDoc();
File.WriteAllText("MathAPI.js", code);
```
**Client** (JavaScript)
``` 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.
``` csharp
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(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.