This project is demonstrating bidirectional connection and data transfer via .NET Core WebSockets.
You can use it on your API - Service side to communicate among your trusted Backend API consumer Clients (for example MVC Web Application Hosting) and at the same time may client to be a Browser (User-Agent) then you can manage your connection and domain data transfer operation with same interface.
// Add NetCoreStack Native Socket Services.
services.AddNativeWebSockets<ServerWebSocketCommandInvocator>();
app.UseNativeWebSockets();
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();
}
// WebSockets for Browsers
services.AddNativeWebSockets(options => {
options.RegisterInvocator<AgentsWebSocketCommandInvocator>(WebSocketCommands.All);
});
// Client WebSocket - Proxy connections
services.AddProxyWebSockets()
.Register<CustomWebSocketCommandInvocator>(connectorname, "localhost:7803")
.Register<AnotherEndpointWebSocketCommandInvocator>(connectorname, "localhost:5000"); // Another endpoint registration, host address must be unique
// Add MVC framework services.
services.AddMvc();
// Client WebSocket - Proxy connections
app.UseProxyWebSockets();
// WebSockets for Browsers
app.UseNativeWebSockets();
// Use MVC
app.UseMvc();
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();
}
}