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.

MqttConnectionHandler.cs 1.4 KiB

пре 6 година
пре 6 година
пре 6 година
пре 6 година
пре 6 година
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Microsoft.AspNetCore.Connections;
  2. using Microsoft.AspNetCore.Connections.Features;
  3. using MQTTnet.Adapter;
  4. using MQTTnet.Serializer;
  5. using MQTTnet.Server;
  6. using System;
  7. using System.Threading.Tasks;
  8. namespace MQTTnet.AspNetCore
  9. {
  10. public class MqttConnectionHandler : ConnectionHandler, IMqttServerAdapter
  11. {
  12. public event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted;
  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. var serializer = new MqttPacketSerializer();
  22. using (var adapter = new MqttConnectionContext(serializer, connection))
  23. {
  24. var args = new MqttServerAdapterClientAcceptedEventArgs(adapter);
  25. ClientAccepted?.Invoke(this, args);
  26. await args.SessionTask;
  27. }
  28. }
  29. public Task StartAsync(IMqttServerOptions options)
  30. {
  31. return Task.CompletedTask;
  32. }
  33. public Task StopAsync()
  34. {
  35. return Task.CompletedTask;
  36. }
  37. public void Dispose()
  38. {
  39. }
  40. }
  41. }