JsStation.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Sers-JsStation</title>
  6. <script src="sers.ServiceStation.min.js"></script>
  7. <script>
  8. //var clConfig = { host: 'ws://0.0.0.0:4503', secretKey: 'SersCL' };
  9. var clConfig = { host: 'ws://' + location.hostname + ':4503', secretKey: 'SersCL' };
  10. var serviceStation = new sers.ServiceStation();
  11. /*
  12. user_ApiNode demo:
  13. //onInvoke: function(requestData_bytes,rpcData_object,reply_rpcData_object){}
  14. {
  15. route: '/JsStation/api1', httpMethod: 'GET', apiName: 'js作为服务站点',
  16. onInvoke: function (requestData_bytes, rpcData, reply_rpcData) {
  17. var request_string = vit.bytesToString(requestData_bytes);
  18. var replyData = {
  19. success: true,
  20. data:
  21. {
  22. request_string: request_string,
  23. _: Math.random()
  24. }
  25. };
  26. return vit.objectSerializeToBytes(replyData);
  27. }
  28. }
  29. */
  30. vit.logger.onmessage = function (msg) {
  31. txt_log.value = txt_log.value + '\n' + msg;
  32. txt_log.scrollTop = txt_log.scrollHeight;
  33. };
  34. function reloadApiNode() {
  35. try {
  36. vit.logger.info('加载api...');
  37. var str_apiNodes = txt_apiNodes.value;
  38. var user_apiNodes = eval('(' + str_apiNodes + ')');
  39. serviceStation.localApiService.clearApiNodes();
  40. for (var t in user_apiNodes) {
  41. var item = user_apiNodes[t];
  42. serviceStation.localApiService.addSimpleApiNode(item.route, item.httpMethod.toUpperCase(), item.apiName, item.onInvoke);
  43. }
  44. vit.logger.info('api已加载,数量:' + user_apiNodes.length);
  45. } catch (e) {
  46. vit.logger.error(e);
  47. }
  48. }
  49. function startService() {
  50. try {
  51. //设置websocket host 地址 demo: "ws://127.0.0.1:4503"
  52. //serviceStation.org.setHost("ws://127.0.0.1:4503");
  53. serviceStation.org.setHost(clConfig.host);
  54. //连接秘钥,用以验证连接安全性。服务端和客户端必须一致
  55. serviceStation.org.secretKey = clConfig.secretKey;
  56. serviceStation.start();
  57. } catch (e) {
  58. vit.logger.error(e);
  59. }
  60. }
  61. function stopService() {
  62. try {
  63. serviceStation.stop();
  64. } catch (e) {
  65. vit.logger.error(e);
  66. }
  67. }
  68. </script>
  69. </head>
  70. <body>
  71. <h1>Sers-JsStation</h1>
  72. <div>
  73. <textarea type="text" id="txt_log" rows="10" cols="200"></textarea>
  74. <br /><input type="button" onclick="reloadApiNode()" value="加载api" />
  75. <input type="button" onclick="startService()" value="启动服务" />
  76. <input type="button" onclick="stopService()" value="关闭服务" /><br />
  77. <textarea type="text" id="txt_apiNodes" rows="40" cols="200">
  78. [
  79. {
  80. route: '/JsStation/api1', httpMethod: 'GET', apiName: 'js作为服务站点',
  81. onInvoke: function (requestData_bytes, rpcData, reply_rpcData) {
  82. var request_string = vit.bytesToString(requestData_bytes);
  83. vit.logger.info('[api调用] request:' + request_string );
  84. var replyData = {
  85. success: true,
  86. data:
  87. {
  88. request_string: request_string,
  89. _: Math.random()
  90. }
  91. };
  92. return vit.objectSerializeToBytes(replyData);
  93. }
  94. }
  95. ]
  96. </textarea>
  97. </div>
  98. </body>
  99. </html>