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.
 
 
 
 

42 lines
1.2 KiB

  1. using System;
  2. using System.Net.WebSockets;
  3. using System.Threading.Tasks;
  4. using MQTTnet.Adapter;
  5. using MQTTnet.Diagnostics;
  6. using MQTTnet.Implementations;
  7. using MQTTnet.Serializer;
  8. using MQTTnet.Server;
  9. namespace MQTTnet.AspNetCore
  10. {
  11. public class MqttWebSocketServerAdapter : IMqttServerAdapter
  12. {
  13. public event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted;
  14. public Task StartAsync(IMqttServerOptions options)
  15. {
  16. return Task.CompletedTask;
  17. }
  18. public Task StopAsync()
  19. {
  20. return Task.CompletedTask;
  21. }
  22. public Task AcceptWebSocketAsync(WebSocket webSocket)
  23. {
  24. if (webSocket == null) throw new ArgumentNullException(nameof(webSocket));
  25. var clientAdapter = new MqttChannelAdapter(new MqttWebSocketChannel(webSocket), new MqttPacketSerializer(), new MqttNetLogger().CreateChildLogger(nameof(MqttWebSocketServerAdapter)));
  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. }