JsEnv_Dev.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var rpc_client_id, Hlclient = function (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. if (this.wsURL.indexOf("clientId=") === -1 && rpc_client_id) {
  21. this.wsURL += "&clientId=" + rpc_client_id
  22. }
  23. console.log('begin of connect to wsURL: ' + this.wsURL);
  24. var _this = this;
  25. try {
  26. this.socket = new WebSocket(this.wsURL);
  27. this.socket.onmessage = function (e) {
  28. _this.handlerRequest(e.data)
  29. }
  30. } catch (e) {
  31. console.log("connection failed,reconnect after 10s");
  32. setTimeout(function () {
  33. _this.connect()
  34. }, 10000)
  35. }
  36. this.socket.onclose = function () {
  37. console.log('rpc已关闭');
  38. setTimeout(function () {
  39. _this.connect()
  40. }, 10000)
  41. }
  42. this.socket.addEventListener('open', (event) => {
  43. console.log("rpc连接成功");
  44. });
  45. this.socket.addEventListener('error', (event) => {
  46. console.error('rpc连接出错,请检查是否打开服务端:', event.error);
  47. })
  48. };
  49. Hlclient.prototype.send = function (msg) {
  50. this.socket.send(msg)
  51. }
  52. Hlclient.prototype.regAction = function (func_name, func) {
  53. if (typeof func_name !== 'string') {
  54. throw new Error("an func_name must be string");
  55. }
  56. if (typeof func !== 'function') {
  57. throw new Error("must be function");
  58. }
  59. console.log("register func_name: " + func_name);
  60. this.handlers[func_name] = func;
  61. return true
  62. }
  63. Hlclient.prototype.handlerRequest = function (requestJson) {
  64. var _this = this;
  65. try {
  66. var result = JSON.parse(requestJson)
  67. } catch (error) {
  68. console.log("请求信息解析错误", requestJson);
  69. return
  70. }
  71. if (result["registerId"]) {
  72. rpc_client_id = result['registerId']
  73. return
  74. }
  75. if (!result['action'] || !result["message_id"]) {
  76. console.warn('没有方法或者消息id,不处理');
  77. return
  78. }
  79. var action = result["action"], message_id = result["message_id"]
  80. var theHandler = this.handlers[action];
  81. if (!theHandler) {
  82. this.sendResult(action, message_id, 'action没找到');
  83. return
  84. }
  85. try {
  86. if (!result["param"]) {
  87. theHandler(function (response) {
  88. _this.sendResult(action, message_id, response);
  89. })
  90. return
  91. }
  92. var param = result["param"]
  93. try {
  94. param = JSON.parse(param)
  95. } catch (e) {
  96. }
  97. theHandler(function (response) {
  98. _this.sendResult(action, message_id, response);
  99. }, param)
  100. } catch (e) {
  101. console.log("error: " + e);
  102. _this.sendResult(action, message_id, e);
  103. }
  104. }
  105. Hlclient.prototype.sendResult = function (action, message_id, e) {
  106. if (typeof e === 'object' && e !== null) {
  107. try {
  108. e = JSON.stringify(e)
  109. } catch (v) {
  110. console.log(v)//不是json无需操作
  111. }
  112. }
  113. this.send(JSON.stringify({"action": action, "message_id": message_id, "response_data": e}));
  114. }