MessageClient.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading;
  6. using Assets.Plugins.CommonTool;
  7. using Assets.Plugins.Log;
  8. using Assets.Plugins.WeMessageService;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. using UnityEngine.Video;
  12. public class MessageClient : MonoBehaviour
  13. {
  14. public Text txtHeartbeat;
  15. public Text txtSceneTitle;
  16. public Text txtSceneDesc;
  17. public GameObject VideoObj;
  18. public GameObject ImageObj;
  19. private VideoPlayer Video1;
  20. private Image Image1;
  21. private string heartbeat = "";
  22. private List<SceneDto> scenes;
  23. private List<CampAttachDto> _videos = new List<CampAttachDto>();
  24. private List<CampAttachDto> _images = new List<CampAttachDto>();
  25. private SceneDto scene;
  26. void Awake()
  27. {
  28. MessageClientManager.Instance.ReceiveHeartbeat +=R_Heartbeat;
  29. MessageClientManager.Instance.ReceiveScene += R_NewScene;
  30. //MessageClientManager.Instance.RegisterClient();
  31. }
  32. private string _baseUrl = "http://shvber.com:8888";
  33. private string _baseUrl2 = "http://shvber.com:5028";
  34. private bool IsPlaying => VideoObj.activeSelf ||
  35. ImageObj.activeSelf;
  36. // Start is called before the first frame update
  37. void Start()
  38. {
  39. scenes = new List<SceneDto>();
  40. Video1 = VideoObj.GetComponent<VideoPlayer>();
  41. Video1.source = VideoSource.Url;
  42. Image1 = ImageObj.GetComponent<Image>();
  43. VideoObj.SetActive(false);
  44. ImageObj.SetActive(false);
  45. LogHelper.LogInfo("¿ªÊ¼");
  46. StartCoroutine(VideoStop());
  47. StartCoroutine(GetScene());
  48. StartCoroutine(VideoPlay());
  49. }
  50. // Update is called once per frame
  51. void Update()
  52. {
  53. if (txtHeartbeat != null)
  54. {
  55. txtHeartbeat.text = heartbeat;
  56. }
  57. if (scene == null)
  58. {
  59. return;
  60. }
  61. if ( txtSceneTitle != null )
  62. {
  63. txtSceneTitle.text = scene.Name;
  64. txtSceneDesc.text = scene.Description;
  65. }
  66. if (scene.Attaches != null && !IsPlaying)
  67. {
  68. _images = scene.Attaches.Where(a => a.FileType == "image").ToList();
  69. _videos = scene.Attaches.Where(a => a.FileType == "video").ToList();
  70. }
  71. scene = null;
  72. }
  73. void R_Heartbeat(string text)
  74. {
  75. heartbeat = text;
  76. }
  77. void R_NewScene(WeMessage msg)
  78. {
  79. LogHelper.LogInfo(msg.Content);
  80. var sceneDto = msg.Content.Str2Obj<SceneDto>();
  81. var url = _baseUrl + "/api/services/app/query/GetSceneInfo";
  82. url += "?no=" + msg.RunningId;
  83. url += "&sceneNo=" + sceneDto.No;
  84. var result= url.RequestPost("");
  85. var newScene = result.Str2Obj<SceneDto>();
  86. if (newScene != null)
  87. {
  88. scenes.Add(newScene);
  89. }
  90. }
  91. private int total = 60 * 30;
  92. IEnumerator GetScene()
  93. {
  94. while (true)
  95. {
  96. if (scenes.Any())
  97. {
  98. if (IsPlaying)
  99. {
  100. while (IsPlaying)
  101. {
  102. yield return new WaitForSeconds(0.5f);
  103. }
  104. }
  105. scene = scenes.FirstOrDefault();
  106. scenes.Remove(scene);
  107. }
  108. else
  109. {
  110. scene = null;
  111. }
  112. yield return new WaitForSeconds(5f);
  113. }
  114. // ReSharper disable once IteratorNeverReturns
  115. }
  116. IEnumerator VideoPlay()
  117. {
  118. while (true)
  119. {
  120. if (!VideoObj.activeSelf && _videos.Any())
  121. {
  122. var v = _videos.FirstOrDefault();
  123. if (v != null)
  124. {
  125. var url = _baseUrl2 + v.FilePath;
  126. _videos.Remove(v);
  127. VideoObj.SetActive(true);
  128. Video1.url = url;
  129. }
  130. }
  131. if (!ImageObj.activeSelf && _images.Any())
  132. {
  133. var v = _images.FirstOrDefault();
  134. if (v != null)
  135. {
  136. var url = _baseUrl2 + v.FilePath;
  137. _images.Remove(v);
  138. AsyncImageDownload.Instance.SetAsyncImage(url, ImageObj);
  139. ImageObj.SetActive(true);
  140. }
  141. }
  142. yield return new WaitForSeconds(0.5f);
  143. }
  144. }
  145. IEnumerator VideoStop()
  146. {
  147. int i1 = 0;
  148. //int i2 = 0;
  149. while (true)
  150. {
  151. if (Video1.frame>0 && Video1.frame + 1 == (long) Video1.frameCount)
  152. {
  153. Video1.Stop();
  154. //Video1.url = "";
  155. VideoObj.SetActive(false);
  156. }
  157. if (ImageObj.activeSelf)
  158. {
  159. i1++;
  160. if (i1 >= total)
  161. {
  162. i1 = 0;
  163. ImageObj.SetActive(false);
  164. }
  165. }
  166. yield return 0;
  167. }
  168. }
  169. private void OnApplicationQuit()
  170. {
  171. StopAllCoroutines();
  172. }
  173. }
  174. public class SceneDto
  175. {
  176. public string No { get; set; }
  177. public string Name { get; set; }
  178. public string Description { get; set; }
  179. public string Path { get; set; }
  180. public List<CampAttachDto> Attaches { get; set; }
  181. }
  182. public class CampAttachDto
  183. {
  184. public string AttachNo { get; set; }
  185. public string CampNo { get; set; }
  186. public string FileType { get; set; }
  187. public string FileTitle { get; set; }
  188. public string FileExt { get; set; }
  189. public string FileName { get; set; }
  190. public string FilePath { get; set; }
  191. }