Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for hosting extensions #1653

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Apply code style
  • Loading branch information
chkr1011 committed Nov 18, 2023
commit db37cfbcb737fcc102fad789611eb4164f7296b5
33 changes: 15 additions & 18 deletions Samples/Server/Server_Hosting_Extensions_Samples.cs
Original file line number Diff line number Diff line change
@@ -9,38 +9,35 @@
// ReSharper disable MemberCanBeMadeStatic.Local

using Microsoft.Extensions.Hosting;
//using MQTTnet.AspNetCore;
using MQTTnet.Server;

namespace MQTTnet.Samples.Server;

public static class Server_Hosting_Extensions_Samples
{

// This could be called as a top-level statement in a Program.cs file
public static Task Start_Single_Line_Server()
=> new HostBuilder().UseMqttServer().Build().RunAsync();

public static Task Start_Simple_Server()
{
var host = new HostBuilder()
.UseMqttServer()
.Build();

return host.RunAsync();
}

public static Task Start_Server()
{
var builder = new HostBuilder();

builder
.UseMqttServer(mqtt =>
builder.UseMqttServer(
mqtt =>
{
mqtt.WithDefaultEndpoint();
});

var host = builder.Build();
return host.RunAsync();
}

public static Task Start_Simple_Server()
{
var host = new HostBuilder().UseMqttServer().Build();

return host.RunAsync();
}

// This could be called as a top-level statement in a Program.cs file
public static Task Start_Single_Line_Server()
{
return new HostBuilder().UseMqttServer().Build().RunAsync();
}
}
144 changes: 63 additions & 81 deletions Source/MQTTnet.Tests/Server/Hosting_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
#if NET5_0_OR_GREATER
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using MQTTnet.Server;
using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Server;

namespace MQTTnet.Tests.Server
{

[TestClass]
public class Hosting_Tests
public sealed class Hosting_Tests
{

[TestMethod]
public async Task Default_Host_Configuration()
public async Task Advanced_Host_Configuration()
{
var syncLock = new object();
var connectedClientCount = 0;
var host = new HostBuilder().UseMqttServer(
mqtt =>
{
mqtt.WithDefaultEndpoint().WithKeepAlive();

var host = new HostBuilder()
.UseMqttServer()
mqtt.ClientConnectedAsync += e =>
{
lock (syncLock)
{
connectedClientCount++;
}

return Task.CompletedTask;
};
})
.Build();

await host.StartAsync();

// Perform client connect test
@@ -30,28 +43,26 @@ public async Task Default_Host_Configuration()
var factory = new MqttFactory();
var client = factory.CreateMqttClient();
var options = factory.CreateClientOptionsBuilder();
options
.WithTcpServer("127.0.0.1");
options.WithTcpServer("127.0.0.1");

await client.ConnectAsync(options.Build());
}
finally
{
await host.StopAsync();
}

Assert.AreEqual(1, connectedClientCount);
}

[TestMethod]
public async Task Custom_Host_Configuration()
{

var host = new HostBuilder()
.UseMqttServer(mqtt =>
{
mqtt
.WithDefaultEndpoint()
.WithKeepAlive();
})
var host = new HostBuilder().UseMqttServer(
mqtt =>
{
mqtt.WithDefaultEndpoint().WithKeepAlive();
})
.Build();
await host.StartAsync();

@@ -61,8 +72,7 @@ public async Task Custom_Host_Configuration()
var factory = new MqttFactory();
var client = factory.CreateMqttClient();
var options = factory.CreateClientOptionsBuilder();
options
.WithTcpServer("127.0.0.1");
options.WithTcpServer("127.0.0.1");

await client.ConnectAsync(options.Build());
}
@@ -73,28 +83,9 @@ public async Task Custom_Host_Configuration()
}

[TestMethod]
public async Task Advanced_Host_Configuration()
public async Task Default_Host_Configuration()
{
var syncLock = new object();
var connectedClientCount = 0;
var host = new HostBuilder()
.UseMqttServer(mqtt =>
{
mqtt
.WithDefaultEndpoint()
.WithKeepAlive();

mqtt.ClientConnectedAsync += e =>
{
lock (syncLock)
{
connectedClientCount++;
}
return Task.CompletedTask;
};
})
.Build();

var host = new HostBuilder().UseMqttServer().Build();
await host.StartAsync();

// Perform client connect test
@@ -103,28 +94,24 @@ public async Task Advanced_Host_Configuration()
var factory = new MqttFactory();
var client = factory.CreateMqttClient();
var options = factory.CreateClientOptionsBuilder();
options
.WithTcpServer("127.0.0.1");
options.WithTcpServer("127.0.0.1");

await client.ConnectAsync(options.Build());
}
finally
{
await host.StopAsync();
}
Assert.AreEqual(1, connectedClientCount);
}

[TestMethod]
public async Task Default_WebSocket_Configuration_Connect()
{
var host = new HostBuilder()
.UseMqttServer(mqtt =>
{
mqtt
.WithDefaultWebSocketEndpoint()
.WithDefaultWebSocketEndpointPort(8080);
})
var host = new HostBuilder().UseMqttServer(
mqtt =>
{
mqtt.WithDefaultWebSocketEndpoint().WithDefaultWebSocketEndpointPort(8080);
})
.Build();
await host.StartAsync();

@@ -136,8 +123,7 @@ public async Task Default_WebSocket_Configuration_Connect()
var factory = new MqttFactory();
var client = factory.CreateMqttClient();
var options = factory.CreateClientOptionsBuilder();
options
.WithWebSocketServer("127.0.0.1:8080/mqtt");
options.WithWebSocketServer("127.0.0.1:8080/mqtt");

await client.ConnectAsync(options.Build());
}
@@ -152,41 +138,39 @@ public async Task External_HttpListener_WebSocket_Configuration_Connect()
{
using (var tcs = new CancellationTokenSource())
{
var host = new HostBuilder()
.UseMqttServer()
.Build();
var host = new HostBuilder().UseMqttServer().Build();
await host.StartAsync();

var httpListener = new HttpListener();
httpListener.Prefixes.Add("http://127.0.0.1:8080/");
httpListener.Start();

_ = Task.Factory.StartNew(async () =>
{
while (!tcs.IsCancellationRequested)
_ = Task.Factory.StartNew(
async () =>
{
try
while (!tcs.IsCancellationRequested)
{
var context = await httpListener.GetContextAsync();

if (context.Request.Url.AbsolutePath.Equals("/mqtt", StringComparison.OrdinalIgnoreCase) && context.Request.IsWebSocketRequest)
try
{
var mqttServer = host.Services.GetService<MqttServer>();
var webSocketContext = await context.AcceptWebSocketAsync("MQTT");
mqttServer.HandleWebSocketConnection(webSocketContext, context);
var context = await httpListener.GetContextAsync();

if (context.Request.Url.AbsolutePath.Equals("/mqtt", StringComparison.OrdinalIgnoreCase) && context.Request.IsWebSocketRequest)
{
var mqttServer = host.Services.GetService<MqttServer>();
var webSocketContext = await context.AcceptWebSocketAsync("MQTT");
mqttServer.HandleWebSocketConnection(webSocketContext, context);
}
else
{
context.Response.StatusCode = 404;
context.Response.Close();
}
}
else
catch
{
context.Response.StatusCode = 404;
context.Response.Close();
}
}
catch
{

}
}
});
});

await Task.Delay(5000);

@@ -196,8 +180,7 @@ public async Task External_HttpListener_WebSocket_Configuration_Connect()
var factory = new MqttFactory();
var client = factory.CreateMqttClient();
var options = factory.CreateClientOptionsBuilder();
options
.WithWebSocketServer("127.0.0.1:8080/mqtt");
options.WithWebSocketServer("127.0.0.1:8080/mqtt");

await client.ConnectAsync(options.Build());
}
@@ -208,7 +191,6 @@ public async Task External_HttpListener_WebSocket_Configuration_Connect()
}
}
}

}
}