123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <!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/api1', httpMethod: 'GET', apiName: '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: {}
- }
- };
- txt_appsettings.value = JSON.stringify(appsettings, null, 2);
- var serviceStation = new sers.ServiceStation();
- /*
- user_ApiNode demo:
- //onInvoke: function(requestData_bytes,rpcData_object,reply_rpcData_object){}
- {
- route: '/JsStation/api1', httpMethod: 'GET', apiName: 'js作为服务站点',
- onInvoke: function (requestData_bytes, rpcData, reply_rpcData) {
- var request_string = vit.bytesToString(requestData_bytes);
- var replyData = {
- success: true,
- data:
- {
- request_string: request_string,
- _: Math.random()
- }
- };
- return vit.objectSerializeToBytes(replyData);
- }
- }
- */
- vit.logger.onmessage = function (msg) {
- txt_log.value = txt_log.value + '\n' + msg;
- txt_log.scrollTop = txt_log.scrollHeight;
- };
- function startService() {
- try {
- vit.logger.info('');
- vit.logger.info('--------------------------------------------');
- //(x.1)
- vit.logger.info('加载api...');
- var str_apiNodes = txt_apiNodes.value;
- var user_apiNodes = eval('(' + str_apiNodes + ')');
- serviceStation.localApiService.clearApiNodes();
- for (var t in user_apiNodes) {
- var item = user_apiNodes[t];
- serviceStation.localApiService.addSimpleApiNode(item.route, item.httpMethod.toUpperCase(), item.apiName, item.onInvoke);
- }
- vit.logger.info('api已加载,数量:' + user_apiNodes.length);
- //(x.2)appsettings
- 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)连接
- 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);
- }
- }
- </script>
- </body>
- </html>
|