SimpleSample.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #if !BESTHTTP_DISABLE_SERVERSENT_EVENTS
  2. using System;
  3. using BestHTTP.Examples.Helpers;
  4. using BestHTTP.ServerSentEvents;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. namespace BestHTTP.Examples.ServerSentEvents
  8. {
  9. public class SimpleSample : BestHTTP.Examples.Helpers.SampleBase
  10. {
  11. #pragma warning disable 0649
  12. [Tooltip("The url of the resource to use.")]
  13. [SerializeField]
  14. private string _path = "/sse";
  15. [SerializeField]
  16. private ScrollRect _scrollRect;
  17. [SerializeField]
  18. private RectTransform _contentRoot;
  19. [SerializeField]
  20. private TextListItem _listItemPrefab;
  21. [SerializeField]
  22. private int _maxListItemEntries = 100;
  23. [SerializeField]
  24. private Button _startButton;
  25. [SerializeField]
  26. private Button _closeButton;
  27. #pragma warning restore
  28. private EventSource eventSource;
  29. protected override void Start()
  30. {
  31. base.Start();
  32. SetButtons(true, false);
  33. }
  34. void OnDestroy()
  35. {
  36. if (this.eventSource != null)
  37. {
  38. this.eventSource.Close();
  39. this.eventSource = null;
  40. }
  41. }
  42. public void OnStartButton()
  43. {
  44. GUIHelper.RemoveChildren(this._contentRoot, 0);
  45. // Create the EventSource instance
  46. this.eventSource = new EventSource(new Uri(base.sampleSelector.BaseURL + this._path));
  47. // Subscribe to generic events
  48. this.eventSource.OnOpen += OnOpen;
  49. this.eventSource.OnClosed += OnClosed;
  50. this.eventSource.OnError += OnError;
  51. this.eventSource.OnStateChanged += this.OnStateChanged;
  52. this.eventSource.OnMessage += OnMessage;
  53. // Subscribe to an application specific event
  54. this.eventSource.On("datetime", OnDateTime);
  55. // Start to connect to the server
  56. this.eventSource.Open();
  57. AddText("Opening Server-Sent Events...");
  58. SetButtons(false, true);
  59. }
  60. public void OnCloseButton()
  61. {
  62. SetButtons(false, false);
  63. this.eventSource.Close();
  64. }
  65. private void OnOpen(EventSource eventSource)
  66. {
  67. AddText("Open");
  68. }
  69. private void OnClosed(EventSource eventSource)
  70. {
  71. AddText("Closed");
  72. this.eventSource = null;
  73. SetButtons(true, false);
  74. }
  75. private void OnError(EventSource eventSource, string error)
  76. {
  77. AddText(string.Format("Error: <color=red>{0}</color>", error));
  78. }
  79. private void OnStateChanged(EventSource eventSource, States oldState, States newState)
  80. {
  81. AddText(string.Format("State Changed {0} => {1}", oldState, newState));
  82. }
  83. private void OnMessage(EventSource eventSource, Message message)
  84. {
  85. AddText(string.Format("Message: <color=yellow>{0}</color>", message));
  86. }
  87. private void OnDateTime(EventSource eventSource, Message message)
  88. {
  89. DateTimeData dtData = BestHTTP.JSON.LitJson.JsonMapper.ToObject<DateTimeData>(message.Data);
  90. AddText(string.Format("OnDateTime: <color=yellow>{0}</color>", dtData.ToString()));
  91. }
  92. private void SetButtons(bool start, bool close)
  93. {
  94. if (this._startButton != null)
  95. this._startButton.interactable = start;
  96. if (this._closeButton != null)
  97. this._closeButton.interactable = close;
  98. }
  99. private void AddText(string text)
  100. {
  101. GUIHelper.AddText(this._listItemPrefab, this._contentRoot, text, this._maxListItemEntries, this._scrollRect);
  102. }
  103. }
  104. [PlatformSupport.IL2CPP.Preserve]
  105. sealed class DateTimeData
  106. {
  107. #pragma warning disable 0649
  108. [PlatformSupport.IL2CPP.Preserve]
  109. public int eventid;
  110. [PlatformSupport.IL2CPP.Preserve]
  111. public string datetime;
  112. #pragma warning restore
  113. public override string ToString()
  114. {
  115. return string.Format("[DateTimeData EventId: {0}, DateTime: {1}]", this.eventid, this.datetime);
  116. }
  117. }
  118. }
  119. #endif