UNPKG

moac-api

Version:
244 lines (215 loc) 6.31 kB
var http = require('http'); /*** * 带超时设置的request * @param options * @param timeout * @param callback * @returns */ function requestWithTimeout(options,timeout,callback){ var timeoutEventId, req = http.request(options,function(res){ res.on('end',function(){ clearTimeout(timeoutEventId); //console.log('response end...'); }); res.on('close',function(){ clearTimeout(timeoutEventId); console.log('response close...'); }); res.on('abort',function(){ console.log('abort...'); }); callback(res); }); req.on('timeout',function(e){ if(req.res){ // req.res('abort'); } req.abort(); }); timeoutEventId=setTimeout(function(){ req.emit('timeout',{message:'have been timeout...'}); },timeout); return req; } /*** * 子链通用rpc request方法 * @param ip * @param port * @param method * @param params * @returns */ function monitorRpcRequest(ip, port, method, params) { return new Promise(function(resolve, reject){ var data = {"jsonrpc": "2.0", "id": 0, "method": method, "params": params}; data = JSON.stringify(data); // 请求参数对象先转换为json字符串格式 var opt = { host: ip, port: port, method: 'POST', path:'/rpc', headers:{ "Content-Type": 'application/json', "Accept": 'application/json', "Content-Length": data.length, "Connection": 'keep-alive' } } var request = requestWithTimeout(opt, 4000, function(result) { // 设置请求超时时间4s var datas = ''; result.on('data', function(chunk) { try { datas += chunk; // 注意:返回json数据量大时,会截取分批返回 } catch(e) { reject(e); return; } }).on('end', function(){ // 请求结束,返回请求结果 var datasObj ={}; try { var datasObj = JSON.parse(datas); } catch (e) { // TODO: handle exception console.log("解析错误!"); } var rpcResult = ""; if (datasObj == undefined || datasObj == null) { // rpcResult = "0"; // rpc连接失败 reject(new Error("rpc connect error!")); return; } else if (datasObj.result == undefined) { if (datasObj.error != undefined && datasObj.error != null) { reject(new Error(datasObj.error.message)); return; } else { reject(new Error("monitor rpc result error!")); return; } } else if (datasObj.result.Storage == undefined) { resolve(datasObj.result); } else { resolve(datasObj.result.Storage); } }); }).on('error', function(e) { // 异常处理 reject(e); // if (e.message == "socket hang up") { // console.log("!!--------socket hang up"); // resolve("4"); // } else { // console.log("!!--------" + e.message); // resolve("5"); // } }); request.write(data); // 发送请求 request.end(); }); }; /*** * 解析address * @param address * @returns */ function resolveAddress(address) { var resObj = {}; var prefix = ""; if (address.indexOf("http://") == 0) { prefix = "http://"; } else if (address.indexOf("https://") == 0) { prefix = "https://"; } if (prefix != "") { var newAddr = address.replace(prefix, ""); resObj.ip = newAddr.split(":")[0]; resObj.port = newAddr.split(":")[1]; } return resObj; } /*** * 通用explorer request方法 * @param ip * @param port * @param method * @param params * @returns */ function explorerRequest(ip, port, method, params) { return new Promise(function(resolve, reject){ method = '/'+method; data = JSON.stringify(params); // 请求参数对象先转换为json字符串格式 var opt = { host: ip, port: port, method: 'POST', path: method, headers:{ "Content-Type": 'application/json', "Accept": 'application/json', "Content-Length": data.length, "Connection": 'keep-alive' } } var request = requestWithTimeout(opt, 4000, function(result) { // 设置请求超时时间4s var datas = ''; result.on('data', function(chunk) { try { datas += chunk; // 注意:返回json数据量大时,会截取分批返回 } catch(e) { console.log(e); } }).on('end', function(){ // 请求结束,返回请求结果 var datasObj = JSON.parse(datas); var rpcResult = ""; if (datasObj == undefined || datasObj == null) { rpcResult = "0"; // 连接失败 }else if (datasObj.data == undefined) { rpcResult = "2"; }else{ rpcResult = datasObj.data; } resolve(rpcResult); }); }).on('error', function(e) { // 异常处理 if (e.message == "socket hang up") { console.log("!!--------socket hang up"); resolve("4"); } else { console.log("!!--------" + e.message); resolve("5"); } }); request.write(data); // 发送请求 request.end(); }); }; /*** * 获取一定范围内的随机数 * @param lower * @param upper * @returns */ function random(lower, upper) { return Math.floor(Math.random() * (upper - lower+1)) + lower; } /** * 检测当前vnode是否正常 * @param chain3 * @returns */ function checkChain3(chain3) { return chain3.isConnected(); } module.exports={ requestWithTimeout: requestWithTimeout, monitorRpcRequest: monitorRpcRequest, resolveAddress: resolveAddress, explorerRequest: explorerRequest, random: random, checkChain3: checkChain3 };