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