MapDouyinLiveCommentComponentSystem.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using Newtonsoft.Json.Linq;
  4. namespace ET.Server
  5. {
  6. [FriendOf(typeof (MapDouyinLiveCommentComponent))]
  7. public static class MapDouyinLiveCommentComponentSystem
  8. {
  9. public class MapDouyinLiveCommentEventComponentAwakeSystem: AwakeSystem<MapDouyinLiveCommentComponent>
  10. {
  11. protected override void Awake(MapDouyinLiveCommentComponent self)
  12. {
  13. Log.Info($"创建抖音直播评论任务组件...");
  14. }
  15. }
  16. public class MapDouyinLiveCommentEventComponentDestroySystem: DestroySystem<MapDouyinLiveCommentComponent>
  17. {
  18. protected override void Destroy(MapDouyinLiveCommentComponent self)
  19. {
  20. Log.Info($"销毁抖音直播评论任务组件");
  21. bool tokenIsNull = self.GetParent<Map>().TokenIsNull();
  22. if (tokenIsNull)
  23. {
  24. return;
  25. }
  26. self.StopTask();
  27. }
  28. }
  29. public class MapDouyinLiveCommentEventComponentU: UpdateSystem<MapDouyinLiveCommentComponent>
  30. {
  31. protected override void Update(MapDouyinLiveCommentComponent self)
  32. {
  33. bool tokenIsNull = self.GetParent<Map>().TokenIsNull();
  34. if (tokenIsNull)
  35. {
  36. return;
  37. }
  38. // 运行中状态
  39. if (self.Status == 3)
  40. {
  41. return;
  42. }
  43. Log.Debug($"检查抖音直播评论任务状态...Status={self.Status}");
  44. self.Status = self.CheckTaskStatus();
  45. switch (self.Status)
  46. {
  47. case -1:
  48. {
  49. // 接口请求出错
  50. Log.Debug($"检查抖音直播评论任务状态 - 抖音接口请求出错");
  51. break;
  52. }
  53. case 1:
  54. {
  55. // 数据推送回调任务不存在
  56. Log.Debug($"检查抖音直播评论任务状态 - 任务已经被删除,可能是因为主播取消挂载/已关播");
  57. break;
  58. }
  59. case 2:
  60. {
  61. // 数据推送回调任务未启动则启动
  62. Log.Debug($"检查抖音直播点赞任务状态 - 任务启动...");
  63. self.StartTask();
  64. break;
  65. }
  66. case 3:
  67. // 数据推送回调任务运行中,不处理
  68. break;
  69. }
  70. }
  71. }
  72. /// <summary>
  73. /// 启动评论推送任务
  74. /// </summary>
  75. /// <param name="self"></param>
  76. private static void StartTask(this MapDouyinLiveCommentComponent self)
  77. {
  78. // 请求头
  79. Dictionary<string, string> head = new Dictionary<string, string>();
  80. head.Add("access-token", self.GetParent<Map>().GetDouyinAccessToken());
  81. // 参数
  82. JObject param = new JObject();
  83. param.Add("roomid", self.GetParent<Map>().RoomId.ToString());
  84. param.Add("appid", DouyinConst.Appid);
  85. param.Add("msg_type", "live_comment");
  86. string str = HttpHelper.PostRequestByDouyin(DouyinConst.StartTaskUrl, head, param);
  87. if (string.IsNullOrEmpty(str))
  88. {
  89. Log.Error($"启动评论推送任务 - StartTaskUrl 请求失败...返回为null");
  90. return;
  91. }
  92. JObject jObject = JObject.Parse(str);
  93. int errNo = Convert.ToInt32(jObject.SelectToken("err_no"));
  94. if (errNo != 0)
  95. {
  96. Log.Error($"启动评论推送任务 - StartTaskUrl 请求成功...返回错误:{errNo}");
  97. return;
  98. }
  99. self.LogId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("logid"));
  100. self.TaskId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("task_id"));
  101. }
  102. /// <summary>
  103. /// 停止评论推送任务
  104. /// </summary>
  105. /// <param name="self"></param>
  106. private static void StopTask(this MapDouyinLiveCommentComponent self)
  107. {
  108. // 请求头
  109. Dictionary<string, string> head = new Dictionary<string, string>();
  110. head.Add("access-token", self.GetParent<Map>().GetDouyinAccessToken());
  111. // 参数
  112. JObject param = new JObject();
  113. param.Add("roomid", self.GetParent<Map>().RoomId.ToString());
  114. param.Add("appid", DouyinConst.Appid);
  115. param.Add("msg_type", "live_comment");
  116. string str = HttpHelper.PostRequestByDouyin(DouyinConst.StopTaskUrl, head, param);
  117. if (string.IsNullOrEmpty(str))
  118. {
  119. Log.Error($"停止评论推送任务 - StopTaskUrl 请求失败...返回为null");
  120. return;
  121. }
  122. JObject jObject = JObject.Parse(str);
  123. int errNo = Convert.ToInt32(jObject.SelectToken("err_no"));
  124. if (errNo != 0)
  125. {
  126. Log.Error($"停止评论推送任务 - StopTaskUrl 请求成功...返回错误:{errNo}");
  127. return;
  128. }
  129. self.LogId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("logid"));
  130. }
  131. /// <summary>
  132. /// 检查点赞推送任务状态 (1-任务不存在, 2-任务未启动, 3-任务运行中)
  133. /// </summary>
  134. /// <param name="self"></param>
  135. /// <returns></returns>
  136. private static int CheckTaskStatus(this MapDouyinLiveCommentComponent self)
  137. {
  138. Dictionary<string, string> head = new Dictionary<string, string>();
  139. head.Add("access-token", self.GetParent<Map>().GetDouyinAccessToken());
  140. // 参数
  141. Dictionary<string, string> param = new Dictionary<string, string>();
  142. param.Add("roomid", self.GetParent<Map>().RoomId.ToString());
  143. param.Add("appid", DouyinConst.Appid);
  144. param.Add("msg_type", "live_comment");
  145. string str = HttpHelper.GetRequestByDouyin(DouyinConst.CheckTaskUrl, head, param);
  146. if (string.IsNullOrEmpty(str))
  147. {
  148. Log.Error($"检查评论推送任务状态 - CheckTaskUrl 请求失败...返回为null");
  149. return -1;
  150. }
  151. JObject jObject = JObject.Parse(str);
  152. int errNo = Convert.ToInt32(jObject.SelectToken("err_no"));
  153. if (errNo != 0)
  154. {
  155. Log.Error($"检查评论推送任务状态 - CheckTaskUrl 请求成功...返回错误:{errNo}");
  156. return -1;
  157. }
  158. self.LogId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("logid"));
  159. return Convert.ToInt32(jObject.SelectToken("data").SelectToken("status"));
  160. }
  161. }
  162. }