mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-30 18:56:30 +08:00
Refactorings
This commit is contained in:
@@ -19,14 +19,20 @@ namespace MQTTnet.Implementations
|
|||||||
public async Task ConnectAsync(MqttClientOptions options)
|
public async Task ConnectAsync(MqttClientOptions options)
|
||||||
{
|
{
|
||||||
_webSocket = new ClientWebSocket();
|
_webSocket = new ClientWebSocket();
|
||||||
await _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None);
|
await _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None).ConfigureAwait(false);
|
||||||
RawStream = new WebSocketStream(_webSocket);
|
RawStream = new WebSocketStream(_webSocket);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task DisconnectAsync()
|
public async Task DisconnectAsync()
|
||||||
{
|
{
|
||||||
RawStream = null;
|
RawStream = null;
|
||||||
return _webSocket?.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
|
||||||
|
if (_webSocket == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|||||||
@@ -23,10 +23,16 @@ namespace MQTTnet.Implementations
|
|||||||
RawStream = new WebSocketStream(_webSocket);
|
RawStream = new WebSocketStream(_webSocket);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task DisconnectAsync()
|
public async Task DisconnectAsync()
|
||||||
{
|
{
|
||||||
RawStream = null;
|
RawStream = null;
|
||||||
return _webSocket?.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
|
||||||
|
if (_webSocket == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|||||||
@@ -19,14 +19,20 @@ namespace MQTTnet.Implementations
|
|||||||
public async Task ConnectAsync(MqttClientOptions options)
|
public async Task ConnectAsync(MqttClientOptions options)
|
||||||
{
|
{
|
||||||
_webSocket = new ClientWebSocket();
|
_webSocket = new ClientWebSocket();
|
||||||
await _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None);
|
await _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None).ConfigureAwait(false);
|
||||||
RawStream = new WebSocketStream(_webSocket);
|
RawStream = new WebSocketStream(_webSocket);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task DisconnectAsync()
|
public async Task DisconnectAsync()
|
||||||
{
|
{
|
||||||
RawStream = null;
|
RawStream = null;
|
||||||
return _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
|
||||||
|
if (_webSocket == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ namespace MQTTnet.Core.Adapter
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _channel.ConnectAsync(options).TimeoutAfter(timeout);
|
await _channel.ConnectAsync(options).TimeoutAfter(timeout).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch (MqttCommunicationTimedOutException)
|
catch (MqttCommunicationTimedOutException)
|
||||||
{
|
{
|
||||||
@@ -50,7 +50,7 @@ namespace MQTTnet.Core.Adapter
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _channel.DisconnectAsync();
|
await _channel.DisconnectAsync().ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch (MqttCommunicationTimedOutException)
|
catch (MqttCommunicationTimedOutException)
|
||||||
{
|
{
|
||||||
@@ -82,8 +82,16 @@ namespace MQTTnet.Core.Adapter
|
|||||||
}
|
}
|
||||||
|
|
||||||
await _sendTask; // configure await false geneates stackoverflow
|
await _sendTask; // configure await false geneates stackoverflow
|
||||||
|
|
||||||
|
if (timeout > TimeSpan.Zero)
|
||||||
|
{
|
||||||
await _channel.SendStream.FlushAsync().TimeoutAfter(timeout).ConfigureAwait(false);
|
await _channel.SendStream.FlushAsync().TimeoutAfter(timeout).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await _channel.SendStream.FlushAsync().ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
catch (MqttCommunicationTimedOutException)
|
catch (MqttCommunicationTimedOutException)
|
||||||
{
|
{
|
||||||
throw;
|
throw;
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ namespace MQTTnet.TestApp.UniversalWindows
|
|||||||
var options = new MqttClientOptions
|
var options = new MqttClientOptions
|
||||||
{
|
{
|
||||||
Server = Server.Text,
|
Server = Server.Text,
|
||||||
Port = 8080,
|
|
||||||
UserName = User.Text,
|
UserName = User.Text,
|
||||||
Password = Password.Text,
|
Password = Password.Text,
|
||||||
ClientId = ClientId.Text,
|
ClientId = ClientId.Text,
|
||||||
|
|||||||
Reference in New Issue
Block a user