25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

MqttClientDisconnectedHandler.cs 1.4 KiB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Threading.Tasks;
  3. using IronPython.Runtime;
  4. using Microsoft.Extensions.Logging;
  5. using MQTTnet.Server.Scripting;
  6. namespace MQTTnet.Server.Mqtt
  7. {
  8. public class MqttClientDisconnectedHandler : IMqttServerClientDisconnectedHandler
  9. {
  10. private readonly PythonScriptHostService _pythonScriptHostService;
  11. private readonly ILogger _logger;
  12. public MqttClientDisconnectedHandler(PythonScriptHostService pythonScriptHostService, ILogger<MqttClientDisconnectedHandler> logger)
  13. {
  14. _pythonScriptHostService = pythonScriptHostService ?? throw new ArgumentNullException(nameof(pythonScriptHostService));
  15. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  16. }
  17. public Task HandleClientDisconnectedAsync(MqttServerClientDisconnectedEventArgs eventArgs)
  18. {
  19. try
  20. {
  21. var pythonEventArgs = new PythonDictionary
  22. {
  23. { "client_id", eventArgs.ClientId },
  24. { "type", PythonConvert.Pythonfy(eventArgs.DisconnectType) }
  25. };
  26. _pythonScriptHostService.InvokeOptionalFunction("on_client_disconnected", pythonEventArgs);
  27. }
  28. catch (Exception exception)
  29. {
  30. _logger.LogError(exception, "Error while handling client disconnected event.");
  31. }
  32. return Task.CompletedTask;
  33. }
  34. }
  35. }