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

68 lines
2.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Grpc.Core;
  7. using JT808.Gateway.GrpcService;
  8. using Microsoft.Extensions.Hosting;
  9. using Microsoft.Extensions.Logging;
  10. using System.Text.Json;
  11. namespace JT808.Gateway.SimpleClient.Jobs
  12. {
  13. public class CallGrpcClientJob :IHostedService
  14. {
  15. private Channel channel;
  16. private readonly ILogger Logger;
  17. private Grpc.Core.Metadata AuthMetadata;
  18. public CallGrpcClientJob(
  19. ILoggerFactory loggerFactory)
  20. {
  21. Logger = loggerFactory.CreateLogger("CallGrpcClientJob");
  22. channel = new Channel("localhost:828",
  23. ChannelCredentials.Insecure);
  24. AuthMetadata = new Grpc.Core.Metadata();
  25. AuthMetadata.Add("token", "smallchi518");
  26. }
  27. public Task StartAsync(CancellationToken cancellationToken)
  28. {
  29. Task.Run(() =>
  30. {
  31. while (!cancellationToken.IsCancellationRequested)
  32. {
  33. JT808Gateway.JT808GatewayClient jT808GatewayClient = new JT808Gateway.JT808GatewayClient(channel);
  34. try
  35. {
  36. var result1 = jT808GatewayClient.GetTcpAtomicCounter(new Empty(), AuthMetadata);
  37. var result2 = jT808GatewayClient.GetTcpSessionAll(new Empty(), AuthMetadata);
  38. Logger.LogInformation($"[GetTcpAtomicCounter]:{JsonSerializer.Serialize(result1)}");
  39. Logger.LogInformation($"[GetTcpSessionAll]:{JsonSerializer.Serialize(result2)}");
  40. }
  41. catch (Exception ex)
  42. {
  43. Logger.LogError(ex, "Call Grpc Error");
  44. }
  45. try
  46. {
  47. var result1 = jT808GatewayClient.GetTcpAtomicCounter(new Empty());
  48. }
  49. catch (RpcException ex)
  50. {
  51. Logger.LogError($"{ex.StatusCode.ToString()}-{ex.Message}");
  52. }
  53. Thread.Sleep(3000);
  54. }
  55. }, cancellationToken);
  56. return Task.CompletedTask;
  57. }
  58. public Task StopAsync(CancellationToken cancellationToken)
  59. {
  60. channel.ShutdownAsync();
  61. return Task.CompletedTask;
  62. }
  63. }
  64. }