added aspnetcore package and adapted sample app

This commit is contained in:
Jan Eggers
2017-10-24 12:45:14 +02:00
parent 3529724bdc
commit 1ca9b0bd62
15 changed files with 260 additions and 41 deletions
@@ -0,0 +1,42 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using MQTTnet.AspNetCore;
using MQTTnet.Core.Server;
namespace MQTTnet.AspnetCore
{
public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UseMqttEndpoint(this IApplicationBuilder app, string path = "/mqtt")
{
app.UseWebSockets();
app.Use(async (context, next) =>
{
if (context.Request.Path == path && context.WebSockets.IsWebSocketRequest)
{
var adapter = app.ApplicationServices.GetRequiredService<MqttWebSocketServerAdapter>();
using (var webSocket = await context.WebSockets.AcceptWebSocketAsync("mqtt"))
{
await adapter.AcceptWebSocketAsync(webSocket);
}
}
else
{
await next();
}
});
return app;
}
public static IApplicationBuilder UseMqttServer(this IApplicationBuilder app, Action<IMqttServer> configure)
{
var server = app.ApplicationServices.GetRequiredService<IMqttServer>();
configure(server);
return app;
}
}
}