You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MqttWebSocketServerAdapter.cs 1.2 KiB

пре 7 година
пре 7 година
123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Net.WebSockets;
  3. using System.Threading.Tasks;
  4. using MQTTnet.Adapter;
  5. using MQTTnet.Diagnostics;
  6. using MQTTnet.Serializer;
  7. using MQTTnet.Server;
  8. namespace MQTTnet.AspNetCore
  9. {
  10. public sealed class MqttWebSocketServerAdapter : IMqttServerAdapter, IDisposable
  11. {
  12. public event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted;
  13. public Task StartAsync(IMqttServerOptions options)
  14. {
  15. return Task.CompletedTask;
  16. }
  17. public Task StopAsync()
  18. {
  19. return Task.CompletedTask;
  20. }
  21. public Task AcceptWebSocketAsync(WebSocket webSocket)
  22. {
  23. if (webSocket == null) throw new ArgumentNullException(nameof(webSocket));
  24. var channel = new MqttWebSocketServerChannel(webSocket);
  25. var clientAdapter = new MqttChannelAdapter(channel, new MqttPacketSerializer(), new MqttNetLogger());
  26. var eventArgs = new MqttServerAdapterClientAcceptedEventArgs(clientAdapter);
  27. ClientAccepted?.Invoke(this, eventArgs);
  28. return eventArgs.SessionTask;
  29. }
  30. public void Dispose()
  31. {
  32. StopAsync();
  33. }
  34. }
  35. }