private static async Task RunServerAsync() { try { var options = new MqttServerOptions { ConnectionValidator = p => { if (p.ClientId == "SpecialClient") { if (p.Username != "USER" || p.Password != "PASS") { return(MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword); } } return(MqttConnectReturnCode.ConnectionAccepted); }, DefaultCommunicationTimeout = TimeSpan.FromMinutes(10) }; var mqttServer = new MqttServerFactory().CreateMqttServer(options); var msgs = 0; var stopwatch = Stopwatch.StartNew(); mqttServer.ApplicationMessageReceived += (sender, args) => { msgs++; if (stopwatch.ElapsedMilliseconds > 1000) { Console.WriteLine($"received {msgs}"); msgs = 0; stopwatch.Restart(); } }; await mqttServer.StartAsync(); Console.WriteLine("Press any key to exit."); Console.ReadLine(); await mqttServer.StopAsync(); } catch (Exception e) { Console.WriteLine(e); } Console.ReadLine(); }
private static async Task RunServerAsync(string[] arguments) { MqttNetTrace.TraceMessagePublished += (s, e) => { Console.WriteLine($">> [{e.ThreadId}] [{e.Source}] [{e.Level}]: {e.Message}"); if (e.Exception != null) { Console.WriteLine(e.Exception); } }; try { var options = new MqttServerOptions { ConnectionValidator = p => { if (p.ClientId == "SpecialClient") { if (p.Username != "USER" || p.Password != "PASS") { return(MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword); } } return(MqttConnectReturnCode.ConnectionAccepted); } }; var mqttServer = new MqttServerFactory().CreateMqttServer(options); await mqttServer.StartAsync(); Console.WriteLine("Press any key to exit."); Console.ReadLine(); await mqttServer.StopAsync(); } catch (Exception e) { Console.WriteLine(e); } Console.ReadLine(); }
public void TestMethod1() { var options = new MqttServerOptions { ConnectionValidator = c => { if (c.ClientId.Length < 10) { return(MqttConnectReturnCode.ConnectionRefusedIdentifierRejected); } if (c.Username != "xxx" || c.Password != "xxx") { return(MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword); } return(MqttConnectReturnCode.ConnectionAccepted); } }; var mqttServer = new MqttServerFactory().CreateMqttServer(options); mqttServer.StartAsync(); }