public async Task <IMqttConnectedClient> CreateClientAsync() { if (_disposed) { throw new ObjectDisposedException(nameof(MqttServerImpl)); } if (!_started) { throw new InvalidOperationException(ServerProperties.Server_NotStartedError); } MqttConnectedClientFactory factory = new MqttConnectedClientFactory(_privateStreamListener); IMqttConnectedClient client = await factory .CreateClientAsync(_configuration); string clientId = GetPrivateClientId(); await client .ConnectAsync(new MqttClientCredentials( clientId )); _connectionProvider.RegisterPrivateClient(clientId); return(client); }
public async void Start() { var configuration = new MqttConfiguration { BufferSize = 128 * 1024, Port = 55555, KeepAliveSecs = 10, WaitTimeoutSecs = 2, MaximumQualityOfService = MqttQualityOfService.AtMostOnce, AllowWildcardsInTopicFilters = true, }; //var = await MqttClient.CreateAsync("192.168.1.10", configuration); IMqttServer mqttServer = MqttServer.Create(configuration); mqttServer.ClientConnected += MqttServer_ClientConnected; mqttServer.ClientDisconnected += MqttServer_ClientDisconnected; mqttServer.MessageUndelivered += MqttServer_MessageUndelivered; mqttServer.Start(); client = await mqttServer.CreateClientAsync(); // client.PublishAsync(); Console.WriteLine("Hello World!"); }
public async Task when_in_process_client_communicate_with_tcp_client_then_succeeds() { IMqttConnectedClient inProcessClient = await Server.CreateClientAsync(); IMqttClient remoteClient = await GetClientAsync(); await remoteClient.ConnectAsync(new MqttClientCredentials( MqttTestHelper.GetClientId())); string fooTopic = "foo/message"; string barTopic = "bar/message"; await inProcessClient.SubscribeAsync(fooTopic, MqttQualityOfService.ExactlyOnce); await remoteClient.SubscribeAsync(barTopic, MqttQualityOfService.AtLeastOnce); int fooMessagesReceived = 0; int barMessagesReceived = 0; inProcessClient.MessageStream.Subscribe(message => { if (message.Topic == fooTopic) { fooMessagesReceived++; } }); remoteClient.MessageStream.Subscribe(message => { if (message.Topic == barTopic) { barMessagesReceived++; } }); await remoteClient.PublishAsync(new MqttApplicationMessage( fooTopic, new byte[255] ), MqttQualityOfService.AtMostOnce); await remoteClient.PublishAsync(new MqttApplicationMessage( fooTopic, new byte[10] ), MqttQualityOfService.AtLeastOnce); await remoteClient.PublishAsync(new MqttApplicationMessage( "other/topic", new byte[500] ), MqttQualityOfService.ExactlyOnce); await remoteClient.PublishAsync(new MqttApplicationMessage( fooTopic, new byte[50] ), MqttQualityOfService.ExactlyOnce); await inProcessClient.PublishAsync(new MqttApplicationMessage( barTopic, new byte[255] ), MqttQualityOfService.AtMostOnce); await inProcessClient.PublishAsync(new MqttApplicationMessage( barTopic, new byte[10] ), MqttQualityOfService.AtLeastOnce); await inProcessClient.PublishAsync(new MqttApplicationMessage( "other/topic", new byte[500] ), MqttQualityOfService.ExactlyOnce); await inProcessClient.PublishAsync(new MqttApplicationMessage( barTopic, new byte[50] ), MqttQualityOfService.ExactlyOnce); await Task.Delay(TimeSpan.FromMilliseconds(1000)); Assert.True(inProcessClient.IsConnected); Assert.True(remoteClient.IsConnected); fooMessagesReceived.Should().Be(3); barMessagesReceived.Should().Be(3); inProcessClient.Dispose(); remoteClient.Dispose(); }
public async Task when_creating_in_process_client_then_it_is_already_connected() { IMqttConnectedClient client = await Server.CreateClientAsync(); Assert.NotNull(client); Assert.True(client.IsConnected); Assert.False(string.IsNullOrEmpty(client.Id)); Assert.True(client.Id.StartsWith("private")); client.Dispose(); }
public async Task when_in_process_client_subscribe_to_system_topic_then_succeeds() { IMqttConnectedClient client = await Server.CreateClientAsync(); string topicFilter = "$SYS/" + Guid.NewGuid().ToString() + "/#"; await client.SubscribeAsync(topicFilter, MqttQualityOfService.AtMostOnce); Assert.True(client.IsConnected); await client.UnsubscribeAsync(topicFilter); client.Dispose(); }
public async Task when_in_process_client_disconnect_then_succeeds() { IMqttConnectedClient client = await Server.CreateClientAsync(); string clientId = client.Id; await client.DisconnectAsync(); Assert.False(Server.ActiveClients.Any(c => c == clientId)); Assert.False(client.IsConnected); Assert.True(string.IsNullOrEmpty(client.Id)); client.Dispose(); }
public async Task when_in_process_client_publish_system_messages_then_succeeds() { IMqttConnectedClient client = await Server.CreateClientAsync(); string topic = "$SYS/" + Guid.NewGuid().ToString(); TestMessage testMessage = new TestMessage { Name = string.Concat("Message ", Guid.NewGuid().ToString().Substring(0, 4)), Value = new Random().Next() }; MqttApplicationMessage message = new MqttApplicationMessage(topic, Serializer.Serialize(testMessage)); await client.PublishAsync(message, MqttQualityOfService.AtMostOnce); await client.PublishAsync(message, MqttQualityOfService.AtLeastOnce); await client.PublishAsync(message, MqttQualityOfService.ExactlyOnce); Assert.True(client.IsConnected); client.Dispose(); }
public async Task when_in_process_clients_communicate_each_other_then_succeeds() { IMqttConnectedClient fooClient = await Server.CreateClientAsync(); IMqttConnectedClient barClient = await Server.CreateClientAsync(); string fooTopic = "foo/message"; await fooClient.SubscribeAsync(fooTopic, MqttQualityOfService.ExactlyOnce); int messagesReceived = 0; fooClient.MessageStream.Subscribe(message => { if (message.Topic == fooTopic) { messagesReceived++; } }); await barClient.PublishAsync(new MqttApplicationMessage( fooTopic, new byte[255] ), MqttQualityOfService.AtMostOnce); await barClient.PublishAsync(new MqttApplicationMessage( fooTopic, new byte[10] ), MqttQualityOfService.AtLeastOnce); await barClient.PublishAsync(new MqttApplicationMessage( "other/topic", new byte[500] ), MqttQualityOfService.ExactlyOnce); await barClient.PublishAsync(new MqttApplicationMessage( fooTopic, new byte[50] ), MqttQualityOfService.ExactlyOnce); await Task.Delay(TimeSpan.FromMilliseconds(1000)); Assert.True(fooClient.IsConnected); Assert.True(barClient.IsConnected); messagesReceived.Should().Be(3); fooClient.Dispose(); barClient.Dispose(); }
static void PublishAsync(IMqttConnectedClient client, string clientId, string message) { Console.WriteLine($"Sending message to {clientId}: {message}"); client.PublishAsync(new MqttApplicationMessage(topic, Encoding.UTF8.GetBytes($"{clientId}:{message}")), MqttQualityOfService.AtLeastOnce).Wait(); }