diff --git a/Samples/Client/Client_Connection_Samples.cs b/Samples/Client/Client_Connection_Samples.cs index 75ef6e61..62feb648 100644 --- a/Samples/Client/Client_Connection_Samples.cs +++ b/Samples/Client/Client_Connection_Samples.cs @@ -8,6 +8,7 @@ using System.Security.Authentication; using MQTTnet.Client; +using MQTTnet.Extensions.WebSocket4Net; using MQTTnet.Formatter; using MQTTnet.Samples.Helpers; @@ -154,6 +155,30 @@ public static class Client_Connection_Samples response.DumpToConsole(); } } + + public static async Task Connect_Client_Using_WebSocket4Net() + { + /* + * This sample creates a simple MQTT client and connects to a public broker using a WebSocket connection. + * Instead of the .NET implementation of WebSockets the implementaion from WebSocket4Net is used. It provides more + * encryption algorithms and supports more platforms. + * + * This is a modified version of the sample _Connect_Client_! See other sample for more details. + */ + + var mqttFactory = new MqttFactory().UseWebSocket4Net(); + + using (var mqttClient = mqttFactory.CreateMqttClient()) + { + var mqttClientOptions = new MqttClientOptionsBuilder().WithWebSocketServer("broker.hivemq.com:8000/mqtt").Build(); + + var response = await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None); + + Console.WriteLine("The MQTT client is connected."); + + response.DumpToConsole(); + } + } public static async Task Connect_Client_With_TLS_Encryption() {