PerHostManagerSample.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Threading.Tasks;
  3. using Best.HTTP.Examples.Helpers;
  4. using Best.HTTP.Hosts.Settings;
  5. using Best.HTTP.Shared;
  6. using UnityEngine;
  7. namespace Best.HTTP.Examples
  8. {
  9. /// <summary>
  10. /// Demonstrates the usage of HTTPManager.PerHostSettings to set rules and configurations on a per-host basis.
  11. /// </summary>
  12. class PerHostManagerSample : SampleBase
  13. {
  14. #pragma warning disable 0649, 0169
  15. [Header("Sample Fields")]
  16. /// <summary>
  17. /// GameObject that will be used as a root for new UI objects.
  18. /// </summary>
  19. [SerializeField]
  20. private RectTransform _contentRoot;
  21. /// <summary>
  22. /// Prefab of a UI object with two Text fields.
  23. /// </summary>
  24. [SerializeField]
  25. private MultiTextListItem _listItemPrefab;
  26. /// <summary>
  27. /// Prefab of a UI object with Text and (Raw)Image fields.
  28. /// </summary>
  29. [SerializeField]
  30. private TextWithImageListItem _listItemWithImagePrefab;
  31. #pragma warning restore
  32. readonly Uri host_1 = new Uri("https://besthttpwebgldemo.azurewebsites.net");
  33. readonly string[] host_1_image_paths = new string[] { "/images/Demo/Two.png", "/images/Demo/Three.png" };
  34. readonly Uri host_2 = new Uri("https://httpbingo.org");
  35. readonly string[] host_2_image_paths = new string[] { "/image/jpeg", "/image/png" };
  36. protected override async void Start()
  37. {
  38. base.Start();
  39. #if (!UNITY_WEBGL || UNITY_EDITOR) && !BESTHTTP_DISABLE_ALTERNATE_SSL
  40. CreateUIItem("Disabling HTTP/2 globally, all following request should done over HTTP/1.");
  41. // Get the global/fallback settings.
  42. var globalSettings = HTTPManager.PerHostSettings.Get("*");
  43. // Turn off connection pooling
  44. globalSettings.HTTP1ConnectionSettings.TryToReuseConnections = false;
  45. // Turn off HTTP/2 connections.
  46. globalSettings.HTTP2ConnectionSettings.EnableHTTP2Connections = false;
  47. await DownloadImages();
  48. CreateUIItem("Enabling HTTP/2 for only a couple hosts");
  49. // Add new settings. New connections to these hosts will use these settings instead of the global one.
  50. // By default both connection pooling and HTTP/2 are enabled, downloading the images again will be done over HTTP/2.
  51. HTTPManager.PerHostSettings.Add(host_1, new HostSettings());
  52. HTTPManager.PerHostSettings.Add(host_2, new HostSettings());
  53. #else
  54. CreateUIItem("Please note that this sample can't work under WebGL!");
  55. #endif
  56. await DownloadImages();
  57. }
  58. async Task DownloadImages()
  59. {
  60. // Images from the first host
  61. foreach (var path in host_1_image_paths)
  62. {
  63. var request = HTTPRequest.CreateGet(new Uri(host_1, path));
  64. request.DownloadSettings.DisableCache = true;
  65. var resp = await request.GetHTTPResponseAsync();
  66. CreateUIItemWithImage(host_1.Host)
  67. .SetStatusText($"http/{resp.HTTPVersion}")
  68. .SetImage(resp.DataAsTexture2D);
  69. }
  70. // Images from the second host
  71. foreach (var path in host_2_image_paths)
  72. {
  73. var request = HTTPRequest.CreateGet(new Uri(host_2, path));
  74. request.DownloadSettings.DisableCache = true;
  75. var resp = await request.GetHTTPResponseAsync();
  76. CreateUIItemWithImage(host_2.Host)
  77. .SetStatusText($"http/{resp.HTTPVersion}")
  78. .SetImage(resp.DataAsTexture2D);
  79. }
  80. }
  81. private void OnDisable()
  82. => HTTPManager.PerHostSettings.Clear();
  83. MultiTextListItem CreateUIItem(string str)
  84. => Instantiate<MultiTextListItem>(this._listItemPrefab, this._contentRoot)
  85. .SetText(str) as MultiTextListItem;
  86. TextWithImageListItem CreateUIItemWithImage(string str)
  87. => Instantiate<TextWithImageListItem>(this._listItemWithImagePrefab, this._contentRoot)
  88. .SetText(str) as TextWithImageListItem;
  89. }
  90. }