WebSockets

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