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.
 
 
 
 

48 rivejä
1.4 KiB

  1. using Microsoft.AspNetCore.Connections;
  2. using Microsoft.AspNetCore.Connections.Features;
  3. using MQTTnet.Adapter;
  4. using MQTTnet.Server;
  5. using System;
  6. using System.Threading.Tasks;
  7. using MQTTnet.Formatter;
  8. namespace MQTTnet.AspNetCore
  9. {
  10. public class MqttConnectionHandler : ConnectionHandler, IMqttServerAdapter
  11. {
  12. public Action<MqttServerAdapterClientAcceptedEventArgs> ClientAcceptedHandler { get; set; }
  13. public override async Task OnConnectedAsync(ConnectionContext connection)
  14. {
  15. // required for websocket transport to work
  16. var transferFormatFeature = connection.Features.Get<ITransferFormatFeature>();
  17. if (transferFormatFeature != null)
  18. {
  19. transferFormatFeature.ActiveFormat = TransferFormat.Binary;
  20. }
  21. using (var adapter = new MqttConnectionContext(new MqttPacketFormatterAdapter(), connection))
  22. {
  23. var args = new MqttServerAdapterClientAcceptedEventArgs(adapter);
  24. ClientAcceptedHandler?.Invoke(args);
  25. await args.SessionTask.ConfigureAwait(false);
  26. }
  27. }
  28. public Task StartAsync(IMqttServerOptions options)
  29. {
  30. return Task.CompletedTask;
  31. }
  32. public Task StopAsync()
  33. {
  34. return Task.CompletedTask;
  35. }
  36. public void Dispose()
  37. {
  38. }
  39. }
  40. }