/// <summary> /// Constructor /// </summary> public Broker() { _factory = new MqttFactory(); _mqttServer = _factory.CreateMqttServer(); BrokerIsLaunched = false; }
public PublishingSpec() : base(keepAliveSecs: 1) { server = GetServerAsync().Result; }
private async void MqttServer() { if (null != _mqttServer) { return; } var optionBuilder = new MqttServerOptionsBuilder().WithConnectionBacklog(1000).WithDefaultEndpointPort(Convert.ToInt32(TxbPort.Text)); if (!String.IsNullOrEmpty(TxbServer.Text)) { optionBuilder.WithDefaultEndpointBoundIPAddress(IPAddress.Parse(TxbServer.Text)); } var options = optionBuilder.Build(); (options as MqttServerOptions).ConnectionValidator += context => { if (context.ClientId.Length < 1) { context.ReturnCode = MqttConnectReturnCode.ConnectionRefusedIdentifierRejected; return; } if (!context.Username.Equals("admin")) { context.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; return; } if (!context.Password.Equals("123456")) { context.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; return; } context.ReturnCode = MqttConnectReturnCode.ConnectionAccepted; }; _mqttServer = new MqttFactory().CreateMqttServer(); _mqttServer.ClientConnected += (sender, args) => { listBox.BeginInvoke(_updateListBoxAction, $">Client Connected:ClientId:{args.ClientId},ProtocalVersion:"); var s = _mqttServer.GetClientSessionsStatusAsync(); label3.BeginInvoke(new Action(() => { label3.Text = $"连接总数:{s.Result.Count}"; })); }; _mqttServer.ClientDisconnected += (sender, args) => { listBox.BeginInvoke(_updateListBoxAction, $"<Client DisConnected:ClientId:{args.ClientId}"); var s = _mqttServer.GetClientSessionsStatusAsync(); label3.BeginInvoke(new Action(() => { label3.Text = $"连接总数:{s.Result.Count}"; })); }; _mqttServer.ApplicationMessageReceived += (sender, args) => { listBox.BeginInvoke(_updateListBoxAction, $"ClientId:{args.ClientId} Topic:{args.ApplicationMessage.Topic} Payload:{Encoding.UTF8.GetString(args.ApplicationMessage.Payload)} QualityOfServiceLevel:{args.ApplicationMessage.QualityOfServiceLevel}"); }; _mqttServer.ClientSubscribedTopic += (sender, args) => { listBox.BeginInvoke(_updateListBoxAction, $"@ClientSubscribedTopic ClientId:{args.ClientId} Topic:{args.TopicFilter.Topic} QualityOfServiceLevel:{args.TopicFilter.QualityOfServiceLevel}"); }; _mqttServer.ClientUnsubscribedTopic += (sender, args) => { listBox.BeginInvoke(_updateListBoxAction, $"%ClientUnsubscribedTopic ClientId:{args.ClientId} Topic:{args.TopicFilter.Length}"); }; _mqttServer.Started += (sender, args) => { listBox.BeginInvoke(_updateListBoxAction, "Mqtt Server Start..."); }; _mqttServer.Stopped += (sender, args) => { listBox.BeginInvoke(_updateListBoxAction, "Mqtt Server Stop..."); }; await _mqttServer.StartAsync(options); }
public void ConfigureMqttServer(IMqttServer mqttServer) { _mqttServer = mqttServer; }
public async Task StartServer() { _mqttServer = new MqttFactory().CreateMqttServer(); await _mqttServer.StartAsync(_serverOptions); }
public PrivateClientSpec() { server = GetServerAsync().Result; }
public void Dispose() { serverStarted = false; _mqttServer.StopAsync().Wait(); _mqttServer = null; }
internal ConnectionManager(IMqttServer mqttServer, IMeasurable[] measurables) { this.mqttServer = mqttServer; this.measurables = measurables; }
public ConnectionSpec() { server = GetServerAsync().Result; }
public ConnectionSpecWithKeepAlive() : base(keepAliveSecs: 1) { server = GetServerAsync().Result; }
public void ConfigureMqttServer(IMqttServer mqttServer) { mqttConnectionService.ConfigureMqttServer(mqttServer); mqttSubscriptionService.ConfigureMqttServer(mqttServer); mqttPublishingService.ConfigureMqttServer(mqttServer); }
public DeviceConnectionHandler(IMqttServer mqttServer) { this.mqttServer = mqttServer; }
public MqttInternalService(IMqttServer mqttServer, BrokerCommandTopics commandTopics, BrokerEventTopics eventTopics) { this.mqttServer = mqttServer; this.commandTopics = commandTopics; this.eventTopics = eventTopics; }
public MqttHostedService() { _mqttServer = new MqttFactory().CreateMqttServer(); }
public void ConfigureMqttServer(IMqttServer mqttServer) { this.mqttServer = mqttServer; mqttServer.ClientSubscribedTopicHandler = this; mqttServer.ClientUnsubscribedTopicHandler = this; }
public TestServerWrapper(IMqttServer implementation, TestContext testContext, TestEnvironment testEnvironment) { Implementation = implementation; TestContext = testContext; TestEnvironment = testEnvironment; }
public static async Task StartMqttServerAsync() { if (mqttServer == null) { try { var options = new MqttServerOptionsBuilder().WithConnectionValidator(context => { //验证MQTT客户端 if (!string.IsNullOrEmpty(context.ClientId)) { //if (context.Username != "u001" || context.Password != "p001") //{ // context.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; //} context.ReturnCode = MqttConnectReturnCode.ConnectionAccepted; } else { context.ReturnCode = MqttConnectReturnCode.ConnectionRefusedIdentifierRejected; } }) .WithConnectionBacklog(2000) //设置连接数 .WithDefaultEndpointPort(1883) //默认监听端口是1883 这里设置成1884 .WithPersistentSessions() //使用持续会话 .WithEncryptionSslProtocol(SslProtocols.Tls12) .WithStorage(new RetainedMessageHandler()) //存储的实现,保留的应用程序消息 //拦截消息,扩展来自客户端的所有消息的时间戳 .WithApplicationMessageInterceptor(context => { if (MqttTopicFilterComparer.IsMatch(context.ApplicationMessage.Topic, "/myTopic/WithTimestamp/#")) { context.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(DateTime.Now.ToString("O")); Console.WriteLine("此消息被被处理"); } }) //拦截订阅 .WithSubscriptionInterceptor(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; } }) .Build(); //使用证书 //var certificate = new X509Certificate2(@"C:\certs\test\test.cer", "", X509KeyStorageFlags.Exportable); //options.TlsEndpointOptions.Certificate = certificate.Export(X509ContentType.Pfx); //options.TlsEndpointOptions.IsEnabled = true; mqttServer = new MqttFactory().CreateMqttServer(); mqttServer.UseClientConnectedHandler(e => { Console.WriteLine($"客户端[{e.ClientId}]已连接............"); return(Task.FromResult(1)); }); //mqttServer.ApplicationMessageReceived += MqttServer_ApplicationMessageReceived; ////mqttServer.ClientSubscribedTopic += MqttServer_ClientSubscribedTopic;//订阅事件 ////mqttServer.ClientUnsubscribedTopic += MqttServer_ClientUnsubscribedTopic;//取消订阅事件 //mqttServer..ClientConnected += MqttServer_ClientConnected; //mqttServer.ClientDisconnected += MqttServer_ClientDisconnected; //await Task.Run(async () => { await mqttServer.StartAsync(options); }); //mqttServer.StartAsync(options).Wait(); Console.WriteLine("MQTT服务启动成功!"); } catch (Exception ex) { Console.WriteLine(ex.Message); return; } } }
public SubscriptionSpec() { server = GetServerAsync().Result; }
public MqttServer() { mqttServer = new MqttFactory().CreateMqttServer(); }
public MqttClientDisconnectedHandler(IMqttServer mqttServer, ILoggerFactory loggerFactory) { _mqttServer = mqttServer; _logger = loggerFactory.CreateLogger <MqttClientDisconnectedHandler>(); }
public SonoffS20Channel(IMqttServer mqttServer, string topic) { _mqttServer = mqttServer; _name = topic; }
public async Task StartServer() { if (mqttServer == null) { try { #region 初始化MqttServer var options = new MqttServerOptionsBuilder().WithConnectionValidator(context => { //验证MQTT客户端 if (!string.IsNullOrEmpty(context.ClientId)) { Console.WriteLine(context.Username); Console.WriteLine(context.Password); if (context.Username != UserName || context.Password != Password) { context.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; } else { context.ReturnCode = MqttConnectReturnCode.ConnectionAccepted; } } else { context.ReturnCode = MqttConnectReturnCode.ConnectionRefusedIdentifierRejected; } }) .WithConnectionBacklog(ConnctionCount) //设置连接数 .WithDefaultEndpointPort(PortNum) //默认监听端口是1883 这里设置成1884 .WithPersistentSessions() //使用持续会话 .WithEncryptionSslProtocol(SslProtocols.Tls12) .WithStorage(new RetainedMessageHandler()) //存储的实现,保留的应用程序消息 //拦截消息,扩展来自客户端的所有消息的时间戳 .WithApplicationMessageInterceptor(context => { if (MqttTopicFilterComparer.IsMatch(context.ApplicationMessage.Topic, MessageInterceptorFilter)) { context.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(DateTime.Now.ToString("O")); Console.WriteLine("此消息被被处理"); } }) //拦截订阅 .WithSubscriptionInterceptor(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; } }) .Build(); //使用证书 if (IsUseCert) { var certificate = new X509Certificate2(@"C:\certs\test\test.cer", "", X509KeyStorageFlags.Exportable); options.TlsEndpointOptions.Certificate = certificate.Export(X509ContentType.Pfx); options.TlsEndpointOptions.IsEnabled = true; } mqttServer = new MqttFactory().CreateMqttServer(); #endregion #region 注册监听事件 //mqttServer.ApplicationMessageReceived += MqttServer_ApplicationMessageReceived;//收到消息事件 //mqttServer.ClientSubscribedTopic += MqttServer_ClientSubscribedTopic;//订阅事件 //mqttServer.ClientUnsubscribedTopic += MqttServer_ClientUnsubscribedTopic;//取消订阅事件 //mqttServer.ClientConnected += MqttServer_ClientConnected;//客户端连接事件 //mqttServer.ClientDisconnected += MqttServer_ClientDisconnected;//客户端断开连接事件 //mqttServer.Started += MqttServer_Started;//启动事件 //mqttServer.Stopped += MqttServer_Stopped;//停止事件 #endregion await Task.Run(async() => await mqttServer.StartAsync(options)); } catch (Exception ex) { throw ex; } } }