MESSAGE_DISPATCH.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 recvMessage(data){
  137. try {
  138. // 单个任务
  139. let json_msg = JSON.parse(data)
  140. console.log("recvMessage:",json_msg.cmd)
  141. switch (json_msg.cmd) {
  142. case "updateFilterConfig":
  143. CMD.updateFilterConfig()
  144. break;
  145. case "updateMainConfig":
  146. CMD.updateMainConfig()
  147. break;
  148. case "updateAppConfig":
  149. CMD.updateAppConfig()
  150. break;
  151. case "updatePlatformConfig":
  152. CMD.updatePlatformConfig()
  153. break;
  154. case "updateFqKeyList":
  155. CMD.updateFqKeyList()
  156. break;
  157. case "updateBlackBooks":
  158. CMD.updateBlackBooks()
  159. break;
  160. default:
  161. const postData = {
  162. cmd:"filter_task",
  163. fun:json_msg.cmd,
  164. data:json_msg
  165. };
  166. sendMQMessage(postData,json_msg.cmd)
  167. // const client = new HttpClient({
  168. // timeout: 5000,
  169. // // headers: {
  170. // // 'User-Agent': 'Custom User Agent'
  171. // // }
  172. // });
  173. // const response = await client.post('http://127.0.0.1:9111',postData);
  174. break;
  175. }
  176. }catch(e){
  177. console.error("recvMessage error1!:",e)
  178. }
  179. }
  180. CMD.init = function(){
  181. redis_help.connect((results)=>{
  182. if(results){
  183. CMD.wsClient = new WebSocketClient(config.ws_config.host, {
  184. reconnectDelay: 5000, // 5 seconds
  185. heartbeatInterval: 30000, // 30 seconds
  186. heartbeatTimeout: 5000 // 5 seconds
  187. },CMD.myListener);
  188. }
  189. })
  190. CMD.updateConfig()
  191. CMD.updateTimeConfig()
  192. }
  193. CMD.updateTimeConfig = function(){
  194. let create_heiyan_book_link = require('../src/api/hy/create_heiyan_book_link')
  195. create_heiyan_book_link.login((data)=>{
  196. console.log("hei_yan_token:",data['token'])
  197. redis_help.setKeyValue("hei_yan_token",data['token'])
  198. })
  199. }
  200. //定时更新配置
  201. CMD.updateConfig = function(){
  202. setInterval(()=>{
  203. CMD.updateTimeConfig()
  204. },time_count)
  205. }
  206. CMD.init()