123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <!DOCTYPE html>
- <html lang="zh-cn" xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta charset="utf-8" />
- <title>Sers-JsStation</title>
- </head>
- <body>
- <h2>Sers-JsStation</h2>
- <div>
- <textarea type="text" id="txt_log" rows="13" cols="200">
- 说明:
- 请在ServiceCenter配置文件中开启websocket通信,并修改对应的端口号和secretKey
- </textarea><br />
- <input type="button" onclick="startService()" value="启动服务" />
- <input type="button" onclick="stopService()" value="关闭服务" />
- <input type="button" onclick="txt_log.value = ''" value="清空" /> <br />
- <table>
- <tr><td>appsettings:</td><td>api:</td><td>调用接口:<input type="button" onclick="callApi()" value="执行" /> </td></tr>
- <tr>
- <td>
- <textarea type="text" id="txt_appsettings" rows="30" cols="40"></textarea>
- </td>
- <td>
- <textarea type="text" id="txt_apiNodes" rows="30" cols="80">
- [
- {
- route: '/JsStation/api', httpMethod: 'GET', name: 'call api in js server', description: 'js作为服务站点',
- onInvoke: function (requestData_bytes, rpcData, reply_rpcData) {
- var request_string = vit.bytesToString(requestData_bytes);
- vit.logger.info('[api调用] request:' + request_string);
- var replyData = {
- success: true,
- data:
- {
- request_string: request_string,
- _: Math.random()
- }
- };
- return vit.objectSerializeToBytes(replyData);
- }
- }
- ]
- </textarea>
- </td>
- <td>
- <textarea type="text" id="txt_callApi" rows="30" cols="80">
- serviceStation.apiClient.callApi("/JsStation/api1", {name:'sers'}, 'GET',
- function (isSuccess, replyData_bytes, replyRpcData_object) {
- if (!isSuccess) {
- vit.logger.info("接口调用失败!");
- return;
- }
- //var apiRet = vit.bytesToObject(replyData_bytes);
- var str = vit.bytesToString(replyData_bytes);
- vit.logger.info("接口调用成功。 reply:" + vit.bytesToString(replyData_bytes));
- });
- </textarea>
- </td>
- </tr>
- </table>
- </div>
- <script src="sers.ServiceStation.min.js"></script>
- <script>
- var appsettings =
- {
- CL: {
- host: 'ws://' + (location.hostname || '127.0.0.1') + ':4503',
- secretKey: 'SersCL'
- },
- serviceStationInfo: {
- serviceStationName: 'JsStation',
- serviceStationKey: '',
- stationVersion: '',
- info: {}
- }
- };
- //type: info/error
- //e: pass error when type is error
- //function(message,type,e){ }
- vit.logger.onmessage = function (message, type, e) {
- if (e) message = message + '\n' + e.stack;
- console.log(message);
- txt_log.value = txt_log.value + '\n' + message;
- txt_log.scrollTop = txt_log.scrollHeight;
- };
- var serviceStation = new sers.ServiceStation();
- //(Error e,requestData_bytes,rpcData,replyRpcDta)
- //localApiService.onError = function(e,requestData_bytes,rpcData,replyRpcDta){ return {success:false}; }
- serviceStation.localApiService.onError = function (e, requestData_bytes, rpcData, replyRpcDta) {
- vit.logger.error(e);
- var reply = {
- success: false,
- error: {
- errorMessage: e.message,
- errorDetail: { name: e.name, stack: e.stack }
- }
- };
- return reply;
- };
- /*
- apiNodes demo:
- //onInvoke: function(requestData_bytes,rpcData_object,reply_rpcData_object){}
- {
- route: '/JsStation/api', httpMethod: 'GET', name: 'call api in js server', description: 'js作为服务站点',
- onInvoke: function (requestData_bytes, rpcData, reply_rpcData) {
- var request_string = vit.bytesToString(requestData_bytes);
- vit.logger.info('[api调用] request:' + request_string);
- var replyData = {
- success: true,
- data:{
- request_string: request_string,
- _: Math.random()
- }
- };
- return vit.objectSerializeToBytes(replyData);
- }
- }
- */
- function startService() {
- try {
- vit.logger.info('');
- vit.logger.info('--------------------------------------------');
- //(x.1)load localApi
- vit.logger.info('[ApiLoader] load localApi...');
- var str_apiNodes = txt_apiNodes.value;
- var apiNodes = eval('(' + str_apiNodes + ')');
- serviceStation.localApiService.clearApiNodes();
- for (var item of apiNodes) {
- serviceStation.localApiService.addSimpleApiNode(item.route, item.httpMethod.toUpperCase(), item.name, item.description, item.onInvoke);
- }
- vit.logger.info('loaded localApi,count:' + apiNodes.length);
- //(x.2)load configuration
- vit.logger.info('load configuration...');
- appsettings = eval('(' + txt_appsettings.value + ')');
- //设置websocket host 地址
- serviceStation.org.setHost(appsettings.CL.host);
- //连接秘钥,用以验证连接安全性。服务端和客户端必须一致
- serviceStation.org.secretKey = appsettings.CL.secretKey;
- serviceStation.serviceStationInfo = appsettings.serviceStationInfo;
- //(x.3)event
- serviceStation.org.event_onDisconnected = function () {
- vit.logger.info('[sers.CL]org.event_onDisconnected');
- };
- //(x.4)connect
- serviceStation.start();
- } catch (e) {
- vit.logger.error(e);
- }
- }
- function stopService() {
- try {
- vit.logger.info('');
- vit.logger.info('--------------------------------------------');
- vit.logger.info('断开连接...');
- serviceStation.stop();
- vit.logger.info('连接已断开');
- } catch (e) {
- vit.logger.error(e);
- }
- }
- function callApi() {
- try {
- vit.logger.info('');
- vit.logger.info('--------------------------------------------');
- vit.logger.info('调用接口...');
- eval(txt_callApi.value);
- } catch (e) {
- vit.logger.error(e);
- }
- }
- txt_appsettings.value = JSON.stringify(appsettings, null, 2);
- </script>
- </body>
- </html>
|