MESSAGE_DISPATCH.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. const CMD = {}
  2. const time_count = 60000*5 //60秒更新一次配置
  3. const config = require('../etc/config.json')
  4. const redis_help = require('../src/use_redis');
  5. var axios = require('axios');
  6. const HttpClient = require('../src/HttpClient');
  7. const WebSocketClient = require('../src/WebSocketClient');
  8. const rabbitMq = require('../src/mq/rabbit-mq');
  9. console.log("运行 MESSAGE_DISPATCH")
  10. CMD.updateFilterConfig = async function(){
  11. const postData = {
  12. cmd:"db_filter_config",
  13. fun:"getConfig",
  14. data:{}
  15. };
  16. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  17. .then(response => {
  18. redis_help.setKeyValue("FilterConfig",JSON.stringify(response.data.data))
  19. })
  20. .catch(error => {
  21. console.error('updateFilterConfig error!', error);
  22. });
  23. }
  24. CMD.updateMainConfig = async function(){
  25. const postData = {
  26. cmd:"tg_main",
  27. fun:"getOpenMainList",
  28. data:{}
  29. };
  30. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  31. .then(response => {
  32. redis_help.setKeyValue("MainConfig",JSON.stringify(response.data.data))
  33. })
  34. .catch(error => {
  35. console.error('updateFilterConfig error!', error);
  36. });
  37. }
  38. CMD.updateAppConfig = async function(){
  39. const postData = {
  40. cmd:"tg_app",
  41. fun:"getAppList",
  42. data:{}
  43. };
  44. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  45. .then(response => {
  46. redis_help.setKeyValue("AppConfig",JSON.stringify(response.data.data))
  47. })
  48. .catch(error => {
  49. console.error('updateFilterConfig error!', error);
  50. });
  51. }
  52. CMD.updatePlatformConfig = async function(){
  53. const postData = {
  54. cmd:"tg_platform",
  55. fun:"getPlatformList",
  56. data:{}
  57. };
  58. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  59. .then(response => {
  60. redis_help.setKeyValue("PlatformConfig",JSON.stringify(response.data.data))
  61. })
  62. .catch(error => {
  63. console.error('updateFilterConfig error!', error);
  64. });
  65. }
  66. CMD.updateFqKeyList = async function(){
  67. const postData = {
  68. cmd:"fq_book",
  69. fun:"get_all_fq_key",
  70. data:{}
  71. };
  72. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  73. .then(response => {
  74. redis_help.setKeyValue("all_fq_key",JSON.stringify(response.data.data))
  75. })
  76. .catch(error => {
  77. console.error('all_fq_key error!', error);
  78. });
  79. }
  80. CMD.updateBlackBooks = async function(){
  81. const postData = {
  82. cmd:"book_black_list",
  83. fun:"get_all_black_books",
  84. data:{}
  85. };
  86. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  87. .then(response => {
  88. redis_help.setKeyValue("all_black_books",JSON.stringify(response.data.data))
  89. })
  90. .catch(error => {
  91. console.error('all_black_books error!', error);
  92. });
  93. }
  94. var ws_isOpen = false
  95. CMD.myListener = {
  96. onOpen: function (webSocket, response) {
  97. ws_isOpen = true
  98. //打开链接后,想服务器端发送一条消息
  99. sendMessage("status_task",{work:1})
  100. },
  101. onMessage: function (webSocket, msg) { //msg可能是字符串,也可能是byte数组,取决于服务器送的内容
  102. recvMessage(msg)
  103. },
  104. onClosing: function (webSocket, code, response) {
  105. ws_isOpen = false
  106. },
  107. onClosed: function (webSocket, code, response) {
  108. ws_isOpen = false
  109. },
  110. onFailure: function (webSocket, t, response) {
  111. }
  112. }
  113. function sendMessage(cmd,data){
  114. if(ws_isOpen==true){
  115. var json = {};
  116. json.cmd=cmd; //方法
  117. json.data= data; //参数
  118. var msg=JSON.stringify(json);
  119. if(CMD.wsClient!=undefined&&CMD.wsClient!=null){
  120. CMD.wsClient.sendMsg(msg);
  121. }
  122. }
  123. }
  124. async function sendMQMessage(message,routingKey = null) {
  125. try {
  126. if(routingKey!=null){
  127. await rabbitMq.producerDirectMsg( message,"exchange_system",routingKey);
  128. }else{
  129. await rabbitMq.producerDirectMsg( message,"exchange_system");
  130. }
  131. console.log('消息发送成功');
  132. } catch (error) {
  133. console.error('发送消息失败:', error);
  134. }
  135. }
  136. async function sendPullMQMessage(message,routingKey = null) {
  137. try {
  138. if(routingKey!=null){
  139. await rabbitMq.producerDirectMsg( message,"exchange_update_pull_config",routingKey);
  140. }else{
  141. await rabbitMq.producerDirectMsg( message,"exchange_update_pull_config");
  142. }
  143. console.log('消息发送成功');
  144. } catch (error) {
  145. console.error('发送消息失败:', error);
  146. }
  147. }
  148. async function recvMessage(data){
  149. try {
  150. // 单个任务
  151. let json_msg = JSON.parse(data)
  152. console.log("recvMessage:",json_msg.cmd)
  153. switch (json_msg.cmd) {
  154. case "updateFilterConfig":
  155. CMD.updateFilterConfig()
  156. break;
  157. case "updateMainConfig":
  158. CMD.updateMainConfig()
  159. break;
  160. case "updateAppConfig":
  161. CMD.updateAppConfig()
  162. break;
  163. case "updatePlatformConfig":
  164. CMD.updatePlatformConfig()
  165. break;
  166. case "updateFqKeyList":
  167. CMD.updateFqKeyList()
  168. break;
  169. case "updateBlackBooks":
  170. CMD.updateBlackBooks()
  171. break;
  172. case "addMain":
  173. {
  174. const postData = {
  175. cmd:"filter_task",
  176. fun:json_msg.cmd,
  177. data:json_msg
  178. };
  179. sendMQMessage(postData,json_msg.cmd)
  180. }
  181. break;
  182. case "updatePullConig":
  183. {
  184. const postData = {
  185. cmd:"filter_task",
  186. fun:json_msg.cmd,
  187. data:json_msg
  188. };
  189. sendPullMQMessage(postData,json_msg.cmd)
  190. }
  191. break;
  192. default:
  193. // const client = new HttpClient({
  194. // timeout: 5000,
  195. // // headers: {
  196. // // 'User-Agent': 'Custom User Agent'
  197. // // }
  198. // });
  199. // const response = await client.post('http://127.0.0.1:9111',postData);
  200. break;
  201. }
  202. }catch(e){
  203. console.error("recvMessage error1!:",e)
  204. }
  205. }
  206. CMD.init = function(){
  207. redis_help.connect((results)=>{
  208. if(results){
  209. CMD.wsClient = new WebSocketClient(config.ws_config.host, {
  210. reconnectDelay: 5000, // 5 seconds
  211. heartbeatInterval: 30000, // 30 seconds
  212. heartbeatTimeout: 5000 // 5 seconds
  213. },CMD.myListener);
  214. }
  215. })
  216. CMD.updateConfig()
  217. CMD.updateTimeConfig()
  218. }
  219. CMD.updateTimeConfig = function(){
  220. let create_heiyan_book_link = require('../src/api/hy/create_heiyan_book_link')
  221. create_heiyan_book_link.login((data)=>{
  222. console.log("hei_yan_token:",data['token'])
  223. redis_help.setKeyValue("hei_yan_token",data['token'])
  224. })
  225. }
  226. //定时更新配置
  227. CMD.updateConfig = function(){
  228. setInterval(()=>{
  229. CMD.updateTimeConfig()
  230. },time_count)
  231. }
  232. CMD.init()