25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MqttClientConnectedHandler.cs 1.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839
  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 MqttClientConnectedHandler : IMqttServerClientConnectedHandler
  9. {
  10. private readonly PythonScriptHostService _pythonScriptHostService;
  11. private readonly ILogger _logger;
  12. public MqttClientConnectedHandler(PythonScriptHostService pythonScriptHostService, ILogger<MqttClientConnectedHandler> logger)
  13. {
  14. _pythonScriptHostService = pythonScriptHostService ?? throw new ArgumentNullException(nameof(pythonScriptHostService));
  15. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  16. }
  17. public Task HandleClientConnectedAsync(MqttServerClientConnectedEventArgs eventArgs)
  18. {
  19. try
  20. {
  21. var pythonEventArgs = new PythonDictionary
  22. {
  23. { "client_id", eventArgs.ClientId }
  24. };
  25. _pythonScriptHostService.InvokeOptionalFunction("on_client_connected", pythonEventArgs);
  26. }
  27. catch (Exception exception)
  28. {
  29. _logger.LogError(exception, "Error while handling client connected event.");
  30. }
  31. return Task.CompletedTask;
  32. }
  33. }
  34. }