feat: 优化API 修复http模块错误

This commit is contained in:
coding 2018-01-09 12:19:02 +00:00
parent a6be26206e
commit 96f93754e6
3 changed files with 30 additions and 35 deletions

View File

@ -111,8 +111,8 @@ function EventHandlerDefault() {
* @param ignoreCancel * @param ignoreCancel
*/ */
this.listen = function listen(jsp, event, exec, priority, ignoreCancel) { this.listen = function listen(jsp, event, exec, priority, ignoreCancel) {
if (!jsp || !jsp.description || !jsp.description.name) throw new TypeError('插件名称为空 请检查传入参数!');
var name = jsp.description.name; var name = jsp.description.name;
if (ext.isNull(name)) throw new TypeError('插件名称为空 请检查传入参数!');
var eventCls = this.name2Class(name, event); var eventCls = this.name2Class(name, event);
if (!eventCls) { return; } if (!eventCls) { return; }
if (typeof priority === 'boolean') { if (typeof priority === 'boolean') {
@ -128,7 +128,7 @@ function EventHandlerDefault() {
if (!listenerMap[name]) listenerMap[name] = []; if (!listenerMap[name]) listenerMap[name] = [];
var offExec = function () { var offExec = function () {
this.unregister(eventCls, listener); this.unregister(eventCls, listener);
console.debug('插件 %s 注销事件 %s'.format(name, this.class2Name(eventCls))); console.debug('插件 %s 注销事件 %s'.format(name, eventCls.name.substring(eventCls.name.lastIndexOf(".") + 1)));
}.bind(this); }.bind(this);
var off = { var off = {
event: eventCls, event: eventCls,

View File

@ -2,7 +2,7 @@
/** /**
* MiaoScript脚本插件加载类 * MiaoScript脚本插件加载类
*/ */
/*global Java, base, module, exports, require, __FILE__*/ /*global Java, module, exports, require, __FILE__*/
// var zip = require("core/zip"); // var zip = require("core/zip");
var fs = require('core/fs'); var fs = require('core/fs');
var yaml = require('modules/yaml'); var yaml = require('modules/yaml');
@ -87,7 +87,7 @@ function loadPlugin(file) {
hook: function (origin) { hook: function (origin) {
return beforeLoadHook(origin); return beforeLoadHook(origin);
} }
}); })
console.debug("插件编译结果: %s".format(JSON.stringify(plugin))); console.debug("插件编译结果: %s".format(JSON.stringify(plugin)));
var desc = plugin.description; var desc = plugin.description;
if (!desc || !desc.name) { if (!desc || !desc.name) {
@ -127,16 +127,15 @@ function initPlugin(file, plugin) {
// 初始化 __FILE__ // 初始化 __FILE__
plugin.__FILE__ = file; plugin.__FILE__ = file;
// 初始化 __DATA__ // 初始化 __DATA__
plugin.__DATA__ = fs.file(file.parentFile, plugin.description.name); plugin.__DATA__ = plugin.dataFolder = fs.file(file.parentFile, plugin.description.name);
// 初始化 getDataFolder() // 初始化 getDataFolder()
plugin.getDataFolder = function () { plugin.getDataFolder = function getDataFolder() {
return plugin.__DATA__; return plugin.__DATA__;
}; }
// 初始化 getFile() // 初始化 getFile()
plugin.getFile = function (name) { plugin.getFile = plugin.file = function getFile(name) {
return fs.file(plugin.getDataFolder(), name); return fs.file(plugin.getDataFolder(), name);
}; };
// 初始化插件配置相关方法 // 初始化插件配置相关方法
initPluginConfig(plugin); initPluginConfig(plugin);
@ -164,9 +163,9 @@ function initPluginConfig(plugin) {
if (!file.isFile) { if (!file.isFile) {
file = plugin.getFile(file); file = plugin.getFile(file);
} }
return yaml.safeLoad(base.read(file)); return yaml.safeLoad(fs.read(file), { json: true });
} }
}; }
/** /**
* 重载配置文件 * 重载配置文件
* @constructor * @constructor
@ -174,7 +173,7 @@ function initPluginConfig(plugin) {
*/ */
plugin.reloadConfig = function () { plugin.reloadConfig = function () {
plugin.config = plugin.getConfig(plugin.configFile); plugin.config = plugin.getConfig(plugin.configFile);
}; }
/** /**
* 保存配置文件 * 保存配置文件
* @constructor * @constructor
@ -184,13 +183,13 @@ function initPluginConfig(plugin) {
switch (arguments.length) { switch (arguments.length) {
case 0: case 0:
plugin.configFile.parentFile.mkdirs(); plugin.configFile.parentFile.mkdirs();
base.save(plugin.configFile, yaml.safeDump(plugin.config)); fs.save(plugin.configFile, yaml.safeDump(plugin.config));
break; break;
case 2: case 2:
base.save(arguments[0], yaml.safeDump(arguments[1])); fs.save(fs.file(plugin.__DATA__, arguments[0]), yaml.safeDump(arguments[1]));
break; break;
} }
}; }
if (plugin.configFile.isFile()) { if (plugin.configFile.isFile()) {
plugin.config = plugin.getConfig('config.yml'); plugin.config = plugin.getConfig('config.yml');
} else if (plugin.description.config) { } else if (plugin.description.config) {
@ -199,10 +198,6 @@ function initPluginConfig(plugin) {
} }
} }
function runAndCatch(jsp, name, ext) {
}
function checkAndGet(args) { function checkAndGet(args) {
if (args.length === 0) { if (args.length === 0) {
return plugins; return plugins;
@ -223,15 +218,13 @@ function checkAndRun(args, name, ext) {
for (var i in pls) { for (var i in pls) {
var jsp = pls[i]; var jsp = pls[i];
var exec = jsp[name]; var exec = jsp[name];
if (exec) { try {
try { // 绑定方法的this到插件自身
// 绑定方法的this到插件自身 if (typeof exec === "function") exec.call(jsp);
exec.bind(jsp)(); if (ext) ext.call(jsp);
if (ext) ext.bind(jsp)(); } catch (ex) {
} catch (ex) { console.console('§6插件 §b%s §6执行 §d%s §6方法时发生错误 §4%s'.format(jsp.description.name, name, ex.message));
console.console('§6插件 §b%s §6执行 §d%s §6方法时发生错误 §4%s'.format(jsp.description.name, name, ex.message)); console.ex(ex);
console.ex(ex);
}
} }
} }
} }

View File

@ -47,7 +47,7 @@ function open(url, method, header) {
var conn = new URL(url).openConnection(); var conn = new URL(url).openConnection();
if (conn instanceof HttpsURLConnection) { if (conn instanceof HttpsURLConnection) {
conn.setHostnameVerifier(TrustAnyHostnameVerifier); conn.setHostnameVerifier(TrustAnyHostnameVerifier);
conn.setSSLSocketFactory(TrustAnyHostnameVerifier); conn.setSSLSocketFactory(SSLSocketFactory);
} }
conn.setRequestMethod(method); conn.setRequestMethod(method);
conn.setDoOutput(true); conn.setDoOutput(true);
@ -81,10 +81,12 @@ function request(url, method, header, params, body) {
var conn = open(buildUrl(url, params), method, header); var conn = open(buildUrl(url, params), method, header);
try { try {
conn.connect(); conn.connect();
var out = conn.getOutputStream(); if (body) {
out.write(new String(body).getBytes(config.Charset)); var out = conn.getOutputStream();
out.flush(); out.write(new String(body).getBytes(config.Charset));
out.close(); out.flush();
out.close();
}
return response(conn); return response(conn);
} finally { } finally {
conn.disconnect(); conn.disconnect();