• Stars
    star
    126
  • Rank 284,543 (Top 6 %)
  • Language
    C#
  • License
    Other
  • Created almost 8 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

WebSockets Inception

Cross-Platform WebSockets Proxy

Minimalist websocket framework for .NET Core. You can use it on your APIs to communicate among your various client types.

NuGet NuGet

Latest release on Nuget

Usage for API - Service Layer

Startup ConfigureServices

// Add NetCoreStack Native Socket Services.
services.AddNativeWebSockets<ServerWebSocketCommandInvocator>();

Startup Configure

 app.UseNativeWebSockets();

Controller with dependency injection

public MyController(IConnectionManager connectionManager)
{
    _connectionManager = connectionManager;
}

[HttpPost(nameof(SendAsync))]
public async Task<IActionResult> SendAsync([FromBody]SimpleModel model)
{
    var echo = $"Echo from server '{model.Message}' - {DateTime.Now}";
    var obj = new { message = echo };
    var webSocketContext = new WebSocketMessageContext { Command = WebSocketCommands.DataSend, Value = obj };
    await _connectionManager.BroadcastAsync(webSocketContext);
    return Ok();
}

Clients

Startup ConfigureServices

// WebSockets for Browsers
services.AddNativeWebSockets<AgentsWebSocketCommandInvocator>();

// Client WebSocket - Proxy connections
var builder = services.AddProxyWebSockets();

var connectorname = $"TestWebApp-{Environment.MachineName}";
builder.Register<CustomWebSocketCommandInvocator>(connectorname, "localhost:7803");

// Runtime context factory
builder.Register<AnotherEndpointWebSocketCommandInvocator, CustomInvocatorContextFactory>();

Startup Configure

// Client WebSocket - Proxy connections
app.UseProxyWebSockets();

// WebSockets for Browsers
app.UseNativeWebSockets();

// Use MVC
app.UseMvc();

Invocator With Dependency Injection on Clients

public class CustomWebSocketCommandInvocator : IClientWebSocketCommandInvocator
{
    private readonly IConnectionManager _connectionManager;
    public CustomWebSocketCommandInvocator(IConnectionManager connectionManager)
    {
        _connectionManager = connectionManager;
    }

    public Task InvokeAsync(WebSocketMessageContext context)
    {
        // Sending incoming data from backend to the clients (Browsers)
        _connectionManager.BroadcastAsync(context);
        return Task.CompletedTask;
    }
}
public class ClientDiscoveryController : Controller
{
    private readonly IWebSocketConnector _connector;
    public ClientDiscoveryController(IWebSocketConnector<CustomWebSocketCommandInvocator> connector)
    {
        _connector = connector;
    }

    [HttpGet]
    public async Task<IActionResult> KeepAlive()
    {
        await _connector.SendAsync(new WebSocketMessageContext
        {
            Command = WebSocketCommands.DataSend,
            Value = new { Id = 1, Name = "Hello World!", DateTime = DateTime.Now }
        });

        return Ok();
    }
}

Prerequisites

ASP.NET Core