JsStation.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <!DOCTYPE html>
  2. <html lang="zh-cn" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Sers-JsStation</title>
  6. </head>
  7. <body>
  8. <h2>Sers-JsStation</h2>
  9. <div>
  10. <textarea type="text" id="txt_log" rows="13" cols="200">
  11. 说明:
  12. 请在ServiceCenter配置文件中开启websocket通信,并修改对应的端口号和secretKey
  13. </textarea><br />
  14. <input type="button" onclick="startService()" value="启动服务" />
  15. <input type="button" onclick="stopService()" value="关闭服务" />
  16. <input type="button" onclick="txt_log.value = ''" value="清空" /> <br />
  17. <table>
  18. <tr><td>appsettings:</td><td>api:</td><td>调用接口:<input type="button" onclick="callApi()" value="执行" /> </td></tr>
  19. <tr>
  20. <td>
  21. <textarea type="text" id="txt_appsettings" rows="30" cols="40"></textarea>
  22. </td>
  23. <td>
  24. <textarea type="text" id="txt_apiNodes" rows="30" cols="80">
  25. [
  26. {
  27. route: '/JsStation/api1', httpMethod: 'GET', apiName: 'js作为服务站点',
  28. onInvoke: function (requestData_bytes, rpcData, reply_rpcData) {
  29. var request_string = vit.bytesToString(requestData_bytes);
  30. vit.logger.info('[api调用] request:' + request_string );
  31. var replyData = {
  32. success: true,
  33. data:
  34. {
  35. request_string: request_string,
  36. _: Math.random()
  37. }
  38. };
  39. return vit.objectSerializeToBytes(replyData);
  40. }
  41. }
  42. ]
  43. </textarea>
  44. </td>
  45. <td>
  46. <textarea type="text" id="txt_callApi" rows="30" cols="80">
  47. serviceStation.apiClient.callApi("/JsStation/api1", {name:'sers'}, 'GET',
  48. function (isSuccess, replyData_bytes, replyRpcData_object) {
  49. if (!isSuccess) {
  50. vit.logger.info("接口调用失败!");
  51. return;
  52. }
  53. //var apiRet = vit.bytesToObject(replyData_bytes);
  54. var str = vit.bytesToString(replyData_bytes);
  55. vit.logger.info("接口调用成功。 reply:" + vit.bytesToString(replyData_bytes));
  56. });
  57. </textarea>
  58. </td>
  59. </tr>
  60. </table>
  61. </div>
  62. <script src="sers.ServiceStation.min.js"></script>
  63. <script>
  64. var appsettings =
  65. {
  66. CL: {
  67. host: 'ws://' + (location.hostname || '127.0.0.1') + ':4503',
  68. secretKey: 'SersCL'
  69. },
  70. serviceStationInfo: {
  71. serviceStationName: 'JsStation',
  72. serviceStationKey: '',
  73. stationVersion: '',
  74. info: {}
  75. }
  76. };
  77. txt_appsettings.value = JSON.stringify(appsettings, null, 2);
  78. var serviceStation = new sers.ServiceStation();
  79. /*
  80. user_ApiNode demo:
  81. //onInvoke: function(requestData_bytes,rpcData_object,reply_rpcData_object){}
  82. {
  83. route: '/JsStation/api1', httpMethod: 'GET', apiName: 'js作为服务站点',
  84. onInvoke: function (requestData_bytes, rpcData, reply_rpcData) {
  85. var request_string = vit.bytesToString(requestData_bytes);
  86. var replyData = {
  87. success: true,
  88. data:
  89. {
  90. request_string: request_string,
  91. _: Math.random()
  92. }
  93. };
  94. return vit.objectSerializeToBytes(replyData);
  95. }
  96. }
  97. */
  98. vit.logger.onmessage = function (msg) {
  99. txt_log.value = txt_log.value + '\n' + msg;
  100. txt_log.scrollTop = txt_log.scrollHeight;
  101. };
  102. function startService() {
  103. try {
  104. vit.logger.info('');
  105. vit.logger.info('--------------------------------------------');
  106. //(x.1)
  107. vit.logger.info('加载api...');
  108. var str_apiNodes = txt_apiNodes.value;
  109. var user_apiNodes = eval('(' + str_apiNodes + ')');
  110. serviceStation.localApiService.clearApiNodes();
  111. for (var t in user_apiNodes) {
  112. var item = user_apiNodes[t];
  113. serviceStation.localApiService.addSimpleApiNode(item.route, item.httpMethod.toUpperCase(), item.apiName, item.onInvoke);
  114. }
  115. vit.logger.info('api已加载,数量:' + user_apiNodes.length);
  116. //(x.2)appsettings
  117. appsettings = eval('(' + txt_appsettings.value + ')');
  118. //设置websocket host 地址
  119. serviceStation.org.setHost(appsettings.CL.host);
  120. //连接秘钥,用以验证连接安全性。服务端和客户端必须一致
  121. serviceStation.org.secretKey = appsettings.CL.secretKey;
  122. serviceStation.serviceStationInfo = appsettings.serviceStationInfo;
  123. //(x.3)连接
  124. serviceStation.start();
  125. } catch (e) {
  126. vit.logger.error(e);
  127. }
  128. }
  129. function stopService() {
  130. try {
  131. vit.logger.info('');
  132. vit.logger.info('--------------------------------------------');
  133. vit.logger.info('断开连接...');
  134. serviceStation.stop();
  135. vit.logger.info('连接已断开');
  136. } catch (e) {
  137. vit.logger.error(e);
  138. }
  139. }
  140. function callApi() {
  141. try {
  142. vit.logger.info('');
  143. vit.logger.info('--------------------------------------------');
  144. vit.logger.info('调用接口...');
  145. eval(txt_callApi.value);
  146. } catch (e) {
  147. vit.logger.error(e);
  148. }
  149. }
  150. </script>
  151. </body>
  152. </html>