JsEnv_Dev.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. function Hlclient(wsURL) {
  2. this.wsURL = wsURL;
  3. this.handlers = {
  4. _execjs: function (resolve, param) {
  5. var res = eval(param)
  6. if (!res) {
  7. resolve("没有返回值")
  8. } else {
  9. resolve(res)
  10. }
  11. }
  12. };
  13. this.socket = {};
  14. if (!wsURL) {
  15. throw new Error('wsURL can not be empty!!')
  16. }
  17. this.connect()
  18. this.socket["ySocket"].addEventListener('close', (event) => {
  19. console.log('rpc已关闭');
  20. });
  21. }
  22. Hlclient.prototype.connect = function () {
  23. console.log('begin of connect to wsURL: ' + this.wsURL);
  24. var _this = this;
  25. try {
  26. this.socket["ySocket"] = new WebSocket(this.wsURL);
  27. this.socket["ySocket"].onmessage = function (e) {
  28. _this.handlerRequest(e.data)
  29. // let blob = e.data
  30. // try {
  31. // blob.text().then(data => {
  32. // _this.handlerRequest(data);
  33. // })
  34. // } catch {
  35. // console.log("not blob")
  36. // _this.handlerRequest(blob)
  37. // }
  38. }
  39. } catch (e) {
  40. console.log("connection failed,reconnect after 10s");
  41. setTimeout(function () {
  42. _this.connect()
  43. }, 10000)
  44. }
  45. this.socket["ySocket"].onclose = function () {
  46. console.log("connection failed,reconnect after 10s");
  47. setTimeout(function () {
  48. _this.connect()
  49. }, 10000)
  50. }
  51. this.socket["ySocket"].addEventListener('open', (event) => {
  52. console.log("rpc连接成功");
  53. });
  54. this.socket["ySocket"].addEventListener('error', (event) => {
  55. console.error('rpc连接出错,请检查是否打开服务端:', event.error);
  56. });
  57. };
  58. Hlclient.prototype.send = function (msg) {
  59. this.socket["ySocket"].send(msg)
  60. }
  61. Hlclient.prototype.regAction = function (func_name, func) {
  62. if (typeof func_name !== 'string') {
  63. throw new Error("an func_name must be string");
  64. }
  65. if (typeof func !== 'function') {
  66. throw new Error("must be function");
  67. }
  68. console.log("register func_name: " + func_name);
  69. this.handlers[func_name] = func;
  70. return true
  71. }
  72. //收到消息后这里处理,
  73. Hlclient.prototype.handlerRequest = function (requestJson) {
  74. var _this = this;
  75. try {
  76. var result = JSON.parse(requestJson)
  77. } catch (error) {
  78. console.log("catch error", requestJson);
  79. result = transjson(requestJson)
  80. }
  81. //console.log(result)
  82. if (!result['action']) {
  83. this.sendResult('', 'need request param {action}');
  84. return
  85. }
  86. var action = result["action"]
  87. var theHandler = this.handlers[action];
  88. if (!theHandler) {
  89. this.sendResult(action, 'action not found');
  90. return
  91. }
  92. try {
  93. if (!result["param"]) {
  94. theHandler(function (response) {
  95. _this.sendResult(action, response);
  96. })
  97. } else {
  98. var param = result["param"]
  99. try {
  100. param = JSON.parse(param)
  101. } catch (e) {
  102. console.log("")
  103. }
  104. theHandler(function (response) {
  105. _this.sendResult(action, response);
  106. }, param)
  107. }
  108. } catch (e) {
  109. console.log("error: " + e);
  110. _this.sendResult(action, e);
  111. }
  112. }
  113. Hlclient.prototype.sendResult = function (action, e) {
  114. this.send(action + atob("aGxeX14") + e);
  115. }
  116. function transjson(formdata) {
  117. var regex = /"action":(?<actionName>.*?),/g
  118. var actionName = regex.exec(formdata).groups.actionName
  119. stringfystring = formdata.match(/{..data..:.*..\w+..:\s...*?..}/g).pop()
  120. stringfystring = stringfystring.replace(/\\"/g, '"')
  121. paramstring = JSON.parse(stringfystring)
  122. tens = `{"action":` + actionName + `,"param":{}}`
  123. tjson = JSON.parse(tens)
  124. tjson.param = paramstring
  125. return tjson
  126. }