MESSAGE_DISPATCH.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. console.log("运行 MESSAGE_DISPATCH")
  9. CMD.updateFilterConfig = async function(){
  10. const postData = {
  11. cmd:"db_filter_config",
  12. fun:"getConfig",
  13. data:{}
  14. };
  15. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  16. .then(response => {
  17. redis_help.setKeyValue("FilterConfig",JSON.stringify(response.data.data))
  18. })
  19. .catch(error => {
  20. console.error('updateFilterConfig error!', error);
  21. });
  22. }
  23. CMD.updateMainConfig = async function(){
  24. const postData = {
  25. cmd:"tg_main",
  26. fun:"getOpenMainList",
  27. data:{}
  28. };
  29. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  30. .then(response => {
  31. redis_help.setKeyValue("MainConfig",JSON.stringify(response.data.data))
  32. })
  33. .catch(error => {
  34. console.error('updateFilterConfig error!', error);
  35. });
  36. }
  37. CMD.updateAppConfig = async function(){
  38. const postData = {
  39. cmd:"tg_app",
  40. fun:"getAppList",
  41. data:{}
  42. };
  43. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  44. .then(response => {
  45. redis_help.setKeyValue("AppConfig",JSON.stringify(response.data.data))
  46. })
  47. .catch(error => {
  48. console.error('updateFilterConfig error!', error);
  49. });
  50. }
  51. CMD.updatePlatformConfig = async function(){
  52. const postData = {
  53. cmd:"tg_platform",
  54. fun:"getPlatformList",
  55. data:{}
  56. };
  57. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  58. .then(response => {
  59. redis_help.setKeyValue("PlatformConfig",JSON.stringify(response.data.data))
  60. })
  61. .catch(error => {
  62. console.error('updateFilterConfig error!', error);
  63. });
  64. }
  65. var ws_isOpen = false
  66. CMD.myListener = {
  67. onOpen: function (webSocket, response) {
  68. ws_isOpen = true
  69. //打开链接后,想服务器端发送一条消息
  70. sendMessage("status_task",{work:1})
  71. },
  72. onMessage: function (webSocket, msg) { //msg可能是字符串,也可能是byte数组,取决于服务器送的内容
  73. recvMessage(msg)
  74. },
  75. onClosing: function (webSocket, code, response) {
  76. ws_isOpen = false
  77. },
  78. onClosed: function (webSocket, code, response) {
  79. ws_isOpen = false
  80. },
  81. onFailure: function (webSocket, t, response) {
  82. }
  83. }
  84. function sendMessage(cmd,data){
  85. if(ws_isOpen==true){
  86. var json = {};
  87. json.cmd=cmd; //方法
  88. json.data= data; //参数
  89. var msg=JSON.stringify(json);
  90. if(CMD.wsClient!=undefined&&CMD.wsClient!=null){
  91. CMD.wsClient.sendMsg(msg);
  92. }
  93. }
  94. }
  95. async function recvMessage(data){
  96. try {
  97. // 单个任务
  98. let json_msg = JSON.parse(data)
  99. console.log("recvMessage:",json_msg.cmd)
  100. switch (json_msg.cmd) {
  101. case "updateFilterConfig":
  102. CMD.updateFilterConfig()
  103. break;
  104. case "updateMainConfig":
  105. CMD.updateMainConfig()
  106. break;
  107. case "updateAppConfig":
  108. CMD.updateAppConfig()
  109. break;
  110. case "updatePlatformConfig":
  111. CMD.updatePlatformConfig()
  112. break;
  113. default:
  114. const postData = {
  115. cmd:"filter_task",
  116. fun:json_msg.cmd,
  117. data:json_msg
  118. };
  119. const client = new HttpClient({
  120. timeout: 5000,
  121. // headers: {
  122. // 'User-Agent': 'Custom User Agent'
  123. // }
  124. });
  125. const response = await client.post('http://127.0.0.1:9111',postData);
  126. break;
  127. }
  128. }catch(e){
  129. console.error("recvMessage error1!:",e)
  130. }
  131. }
  132. CMD.init = function(){
  133. redis_help.connect((results)=>{
  134. if(results){
  135. CMD.wsClient = new WebSocketClient(config.ws_config.host, {
  136. reconnectDelay: 5000, // 5 seconds
  137. heartbeatInterval: 30000, // 30 seconds
  138. heartbeatTimeout: 5000 // 5 seconds
  139. },CMD.myListener);
  140. }
  141. })
  142. CMD.updateConfig()
  143. CMD.updateTimeConfig()
  144. }
  145. CMD.updateTimeConfig = function(){
  146. let create_heiyan_book_link = require('../src/api/hy/create_heiyan_book_link')
  147. create_heiyan_book_link.login((data)=>{
  148. console.log("hei_yan_token:",data['token'])
  149. redis_help.setKeyValue("hei_yan_token",data['token'])
  150. })
  151. }
  152. //定时更新配置
  153. CMD.updateConfig = function(){
  154. setInterval(()=>{
  155. CMD.updateTimeConfig()
  156. },time_count)
  157. }
  158. CMD.init()