mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
62 lines
1.7 KiB
C#
62 lines
1.7 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.FileProviders;
|
|
using Microsoft.Extensions.Logging;
|
|
using MQTTnet.AspnetCore;
|
|
using MQTTnet.Core;
|
|
|
|
namespace MQTTnet.TestApp.AspNetCore2
|
|
{
|
|
public class Startup
|
|
{
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddHostedMqttServer();
|
|
}
|
|
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
|
{
|
|
loggerFactory.AddConsole(LogLevel.Debug);
|
|
loggerFactory.AddDebug();
|
|
|
|
app.UseMqttEndpoint();
|
|
app.UseMqttServer(async server =>
|
|
{
|
|
var msg = new MqttApplicationMessageBuilder()
|
|
.WithPayload("Mqtt is Awesome")
|
|
.WithTopic("message")
|
|
.Build();
|
|
|
|
while (true)
|
|
{
|
|
server.Publish(msg);
|
|
await Task.Delay(TimeSpan.FromSeconds(2));
|
|
}
|
|
});
|
|
|
|
app.Use((context, next) =>
|
|
{
|
|
if (context.Request.Path == "/")
|
|
{
|
|
context.Request.Path = "/Index.html";
|
|
}
|
|
|
|
return next();
|
|
});
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
app.UseStaticFiles( new StaticFileOptions()
|
|
{
|
|
RequestPath = "/node_modules",
|
|
FileProvider = new PhysicalFileProvider( Path.Combine(env.ContentRootPath, "node_modules" ) )
|
|
} );
|
|
}
|
|
}
|
|
}
|