From 305578e7e6955eaf6492d1d736ad0a153a08ed7b Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Tue, 19 Sep 2017 23:05:41 +0200 Subject: [PATCH] Refactor code --- .../Implementations/MqttTcpChannel.cs | 2 +- .../Implementations/MqttWebSocketChannel.cs | 3 ++- .../Implementations/WebSocketStream.cs | 11 ++++------- Tests/MQTTnet.TestApp.NetFramework/Program.cs | 11 ++--------- 4 files changed, 9 insertions(+), 18 deletions(-) diff --git a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs index 47062da..3c9623f 100644 --- a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs +++ b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs @@ -54,8 +54,8 @@ namespace MQTTnet.Implementations await _socket.ConnectAsync(new HostName(options.Server), options.GetPort().ToString(), SocketProtectionLevel.Tls12); } - ReceiveStream = _socket.InputStream.AsStreamForRead(); SendStream = _socket.OutputStream.AsStreamForWrite(); + ReceiveStream = _socket.InputStream.AsStreamForRead(); RawReceiveStream = ReceiveStream; } diff --git a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttWebSocketChannel.cs b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttWebSocketChannel.cs index 942105d..0051322 100644 --- a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttWebSocketChannel.cs +++ b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttWebSocketChannel.cs @@ -28,10 +28,11 @@ namespace MQTTnet.Implementations { uri += ":" + options.Port; } - + _webSocket = new ClientWebSocket(); _webSocket.Options.KeepAliveInterval = options.KeepAlivePeriod; await _webSocket.ConnectAsync(new Uri(uri), CancellationToken.None).ConfigureAwait(false); + RawReceiveStream = new WebSocketStream(_webSocket); } diff --git a/Frameworks/MQTTnet.UniversalWindows/Implementations/WebSocketStream.cs b/Frameworks/MQTTnet.UniversalWindows/Implementations/WebSocketStream.cs index d912148..69225b9 100644 --- a/Frameworks/MQTTnet.UniversalWindows/Implementations/WebSocketStream.cs +++ b/Frameworks/MQTTnet.UniversalWindows/Implementations/WebSocketStream.cs @@ -27,7 +27,7 @@ namespace MQTTnet.Implementations { var response = await _webSocket.ReceiveAsync(new ArraySegment(buffer, currentOffset, count), cancellationToken).ConfigureAwait(false); currentOffset += response.Count; - + if (response.MessageType == WebSocketMessageType.Close) { await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, cancellationToken).ConfigureAwait(false); @@ -56,15 +56,12 @@ namespace MQTTnet.Implementations public override bool CanSeek => false; public override bool CanWrite => true; - public override long Length - { - get { throw new NotSupportedException(); } - } + public override long Length => throw new NotSupportedException(); public override long Position { - get { throw new NotSupportedException(); } - set { throw new NotSupportedException(); } + get => throw new NotSupportedException(); + set => throw new NotSupportedException(); } public override long Seek(long offset, SeekOrigin origin) diff --git a/Tests/MQTTnet.TestApp.NetFramework/Program.cs b/Tests/MQTTnet.TestApp.NetFramework/Program.cs index c8f82e0..59647b2 100644 --- a/Tests/MQTTnet.TestApp.NetFramework/Program.cs +++ b/Tests/MQTTnet.TestApp.NetFramework/Program.cs @@ -78,8 +78,6 @@ namespace MQTTnet.TestApp.NetFramework { new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce) }); - - Console.WriteLine("### SUBSCRIBED ###"); }; client.Disconnected += async (s, e) => @@ -108,17 +106,12 @@ namespace MQTTnet.TestApp.NetFramework Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###"); + var messageFactory = new MqttApplicationMessageFactory(); while (true) { Console.ReadLine(); - var applicationMessage = new MqttApplicationMessage( - "A/B/C", - Encoding.UTF8.GetBytes("Hello World"), - MqttQualityOfServiceLevel.AtLeastOnce, - false - ); - + var applicationMessage = messageFactory.CreateApplicationMessage("myTopic", "Hello World", MqttQualityOfServiceLevel.AtLeastOnce); await client.PublishAsync(applicationMessage); } }