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.1 KiB

6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940
  1. using Microsoft.AspNetCore.Connections;
  2. using MQTTnet.Adapter;
  3. using MQTTnet.Serializer;
  4. using MQTTnet.Server;
  5. using System;
  6. using System.Threading.Tasks;
  7. namespace MQTTnet.AspNetCore
  8. {
  9. public class MqttConnectionHandler : ConnectionHandler, IMqttServerAdapter
  10. {
  11. public event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted;
  12. public override async Task OnConnectedAsync(ConnectionContext connection)
  13. {
  14. var serializer = new MqttPacketSerializer();
  15. using (var adapter = new MqttConnectionContext(serializer, connection))
  16. {
  17. var args = new MqttServerAdapterClientAcceptedEventArgs(adapter);
  18. ClientAccepted?.Invoke(this, args);
  19. await args.SessionTask;
  20. }
  21. }
  22. public Task StartAsync(IMqttServerOptions options)
  23. {
  24. return Task.CompletedTask;
  25. }
  26. public Task StopAsync()
  27. {
  28. return Task.CompletedTask;
  29. }
  30. public void Dispose()
  31. {
  32. }
  33. }
  34. }