@@ -1,7 +1,6 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Threading.Tasks; | |||
using MQTTnet.Core.Client; | |||
namespace MQTTnet.Core.Server | |||
{ | |||
@@ -6,7 +6,6 @@ using MQTTnet.Core.Adapter; | |||
using Microsoft.Extensions.Logging; | |||
using Microsoft.Extensions.Options; | |||
using System.Linq; | |||
using Microsoft.Extensions.DependencyInjection; | |||
namespace MQTTnet.Core.Server | |||
{ | |||
@@ -8,17 +8,19 @@ using Microsoft.Extensions.FileProviders; | |||
using Microsoft.Extensions.Logging; | |||
using MQTTnet.AspNetCore; | |||
using MQTTnet.Core; | |||
using MQTTnet.Core.Client; | |||
namespace MQTTnet.TestApp.AspNetCore2 | |||
{ | |||
public class Startup | |||
{ | |||
// In class _Startup_ of the ASP.NET Core 2.0 project. | |||
public void ConfigureServices(IServiceCollection services) | |||
{ | |||
services.AddHostedMqttServer(); | |||
} | |||
// In class _Startup_ of the ASP.NET Core 2.0 project. | |||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | |||
{ | |||
app.UseMqttEndpoint(); | |||
@@ -49,11 +51,11 @@ namespace MQTTnet.TestApp.AspNetCore2 | |||
app.UseStaticFiles(); | |||
app.UseStaticFiles( new StaticFileOptions() | |||
app.UseStaticFiles(new StaticFileOptions | |||
{ | |||
RequestPath = "/node_modules", | |||
FileProvider = new PhysicalFileProvider( Path.Combine(env.ContentRootPath, "node_modules" ) ) | |||
} ); | |||
FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "node_modules")) | |||
}); | |||
} | |||
} | |||
} |
@@ -35,14 +35,29 @@ namespace MQTTnet.TestApp.NetCore | |||
options.Storage = new RetainedMessageHandler(); | |||
// Extend the timestamp for all messages from clients. | |||
options.ApplicationMessageInterceptor = context => | |||
{ | |||
if (MqttTopicFilterComparer.IsMatch(context.ApplicationMessage.Topic, "/myTopic/WithTimestamp/#")) | |||
{ | |||
// Replace the payload with the timestamp. But also extending a JSON | |||
// based payload with the timestamp is a suitable use case. | |||
context.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(DateTime.Now.ToString("O")); | |||
} | |||
// Replace the payload with the timestamp. But also extending a JSON | |||
// based payload with the timestamp is a suitable use case. | |||
context.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(DateTime.Now.ToString("O")); | |||
} | |||
}; | |||
// Protect several topics from being subscribed from every client. | |||
options.SubscriptionsInterceptor = context => | |||
{ | |||
if (context.TopicFilter.Topic.StartsWith("admin/foo/bar") && context.ClientId != "theAdmin") | |||
{ | |||
context.AcceptSubscription = false; | |||
} | |||
if (context.TopicFilter.Topic.StartsWith("the/secret/stuff") && context.ClientId != "Imperator") | |||
{ | |||
context.AcceptSubscription = false; | |||
context.CloseConnection = true; | |||
} | |||
}; | |||
}); | |||
@@ -264,7 +264,7 @@ namespace MQTTnet.TestApp.UniversalWindows | |||
var mqttClient = serviceProvider.GetRequiredService<IMqttClient>(); | |||
} | |||
{ | |||
// Create a new MQTT client. | |||
var factory = new MqttFactory(); | |||
@@ -396,6 +396,64 @@ namespace MQTTnet.TestApp.UniversalWindows | |||
return new ChainValidationResult[0]; | |||
}; | |||
{ | |||
// Start a MQTT server. | |||
var mqttServer = new MqttFactory().CreateMqttServer(); | |||
await mqttServer.StartAsync(); | |||
Console.WriteLine("Press any key to exit."); | |||
Console.ReadLine(); | |||
await mqttServer.StopAsync(); | |||
} | |||
{ | |||
// Configure MQTT server. | |||
var mqttServer = new MqttFactory().CreateMqttServer(options => | |||
{ | |||
options.ConnectionBacklog = 100; | |||
options.DefaultEndpointOptions.Port = 1884; | |||
options.ConnectionValidator = packet => | |||
{ | |||
if (packet.ClientId != "Highlander") | |||
{ | |||
return MqttConnectReturnCode.ConnectionRefusedIdentifierRejected; | |||
} | |||
return MqttConnectReturnCode.ConnectionAccepted; | |||
}; | |||
}); | |||
} | |||
{ | |||
// Setup client validator. | |||
var mqttServer = new MqttFactory().CreateMqttServer(options => | |||
{ | |||
options.ConnectionValidator = c => | |||
{ | |||
if (c.ClientId.Length < 10) | |||
{ | |||
return MqttConnectReturnCode.ConnectionRefusedIdentifierRejected; | |||
} | |||
if (c.Username != "mySecretUser") | |||
{ | |||
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; | |||
} | |||
if (c.Password != "mySecretPassword") | |||
{ | |||
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; | |||
} | |||
return MqttConnectReturnCode.ConnectionAccepted; | |||
}; | |||
}); | |||
} | |||
{ | |||
// Create a new MQTT server. | |||
var mqttServer = new MqttFactory().CreateMqttServer(); | |||
} | |||
} | |||
private async void StartServer(object sender, RoutedEventArgs e) | |||