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:
@@ -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