|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using System;
- using System.IO;
- using System.Net.WebSockets;
- using System.Threading;
- using System.Threading.Tasks;
- using MQTTnet.Core.Channel;
- using MQTTnet.Implementations;
-
- namespace MQTTnet.AspNetCore
- {
- public class MqttWebSocketServerChannel : IMqttCommunicationChannel, IDisposable
- {
- private WebSocket _webSocket;
-
- public MqttWebSocketServerChannel(WebSocket webSocket)
- {
- _webSocket = webSocket ?? throw new ArgumentNullException(nameof(webSocket));
-
- SendStream = new WebSocketStream(_webSocket);
- ReceiveStream = SendStream;
- }
-
- public Stream SendStream { get; private set; }
- public Stream ReceiveStream { get; private set; }
-
- public Task ConnectAsync()
- {
- return Task.CompletedTask;
- }
-
- public async Task DisconnectAsync()
- {
- if (_webSocket == null)
- {
- return;
- }
-
- try
- {
- await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
- }
- finally
- {
- Dispose();
- }
- }
-
- public void Dispose()
- {
- SendStream?.Dispose();
- ReceiveStream?.Dispose();
-
- _webSocket?.Dispose();
-
- SendStream = null;
- ReceiveStream = null;
- _webSocket = null;
- }
- }
- }
|