JsStation.html 3.8 KB

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