JsEnv_Dev.js 3.5 KB

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