mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 17:23:04 +08:00
Refactor web socket usage
This commit is contained in:
@@ -26,7 +26,7 @@ namespace MQTTnet.Implementations
|
||||
|
||||
public Stream SendStream { get; private set; }
|
||||
public Stream ReceiveStream { get; private set; }
|
||||
public Stream RawStream { get; private set; }
|
||||
public Stream RawReceiveStream { get; private set; }
|
||||
|
||||
public async Task ConnectAsync(MqttClientOptions options)
|
||||
{
|
||||
@@ -56,7 +56,7 @@ namespace MQTTnet.Implementations
|
||||
|
||||
ReceiveStream = _socket.InputStream.AsStreamForRead();
|
||||
SendStream = _socket.OutputStream.AsStreamForWrite();
|
||||
RawStream = ReceiveStream;
|
||||
RawReceiveStream = ReceiveStream;
|
||||
}
|
||||
|
||||
public Task DisconnectAsync()
|
||||
@@ -67,8 +67,8 @@ namespace MQTTnet.Implementations
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
RawStream?.Dispose();
|
||||
RawStream = null;
|
||||
RawReceiveStream?.Dispose();
|
||||
RawReceiveStream = null;
|
||||
|
||||
SendStream?.Dispose();
|
||||
SendStream = null;
|
||||
|
||||
@@ -12,20 +12,32 @@ namespace MQTTnet.Implementations
|
||||
{
|
||||
private ClientWebSocket _webSocket = new ClientWebSocket();
|
||||
|
||||
public Stream RawStream { get; private set; }
|
||||
public Stream SendStream => RawStream;
|
||||
public Stream ReceiveStream => RawStream;
|
||||
public Stream RawReceiveStream { get; private set; }
|
||||
public Stream SendStream => RawReceiveStream;
|
||||
public Stream ReceiveStream => RawReceiveStream;
|
||||
|
||||
public async Task ConnectAsync(MqttClientOptions options)
|
||||
{
|
||||
var uri = options.Server;
|
||||
if (!uri.StartsWith("ws://", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
uri = "ws://" + uri;
|
||||
}
|
||||
|
||||
if (options.Port.HasValue)
|
||||
{
|
||||
uri += ":" + options.Port;
|
||||
}
|
||||
|
||||
_webSocket = new ClientWebSocket();
|
||||
await _webSocket.ConnectAsync(new Uri(options.Server), CancellationToken.None).ConfigureAwait(false);
|
||||
RawStream = new WebSocketStream(_webSocket);
|
||||
_webSocket.Options.KeepAliveInterval = options.KeepAlivePeriod;
|
||||
await _webSocket.ConnectAsync(new Uri(uri), CancellationToken.None).ConfigureAwait(false);
|
||||
RawReceiveStream = new WebSocketStream(_webSocket);
|
||||
}
|
||||
|
||||
public async Task DisconnectAsync()
|
||||
{
|
||||
RawStream = null;
|
||||
RawReceiveStream = null;
|
||||
|
||||
if (_webSocket == null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user