feat: 更新http类库 server通过orElse返回undefined

merge/2/HEAD
coding 2018-01-11 14:46:47 +00:00
parent 75340fd824
commit be95acd001
2 changed files with 54 additions and 23 deletions

View File

@ -38,7 +38,7 @@ exports.plugin = {
} }
return PluginManager.isPluginEnabled(name); return PluginManager.isPluginEnabled(name);
}, },
self: PluginManager.getPlugin('miaoscript').get() self: PluginManager.getPlugin('miaoscript').orElse(undefined)
}; };
/** /**
* 获取玩家 * 获取玩家
@ -48,9 +48,9 @@ exports.player = function () {
case 0: case 0:
return undefined; return undefined;
case 1: case 1:
return Server.getPlayer(arguments[0]).get(); return Server.getPlayer(arguments[0]).orElse(undefined);
default: default:
return Server.getPlayer(arguments[0]).get(); return Server.getPlayer(arguments[0]).orElse(undefined);
} }
}; };
/** /**
@ -59,8 +59,10 @@ exports.player = function () {
exports.players = function () { exports.players = function () {
switch (arguments.length) { switch (arguments.length) {
case 1: case 1:
// 此处的forEach是Collection接口的
return Server.onlinePlayers.forEach(arguments[0]); return Server.onlinePlayers.forEach(arguments[0]);
default: default:
return Server.onlinePlayers; // 此处会转换为JS原生的Array
return Java.from(Server.onlinePlayers.toArray());
} }
}; };

View File

@ -39,7 +39,8 @@ var SSLSocketFactory = function initSSLSocketFactory() {
var config = { var config = {
Charset: 'UTF-8', Charset: 'UTF-8',
ConnectTimeout: 10000, ConnectTimeout: 10000,
ReadTimeout: 10000 ReadTimeout: 10000,
Debug: false
} }
function open(url, method, header) { function open(url, method, header) {
@ -54,8 +55,10 @@ function open(url, method, header) {
conn.setDoInput(true); conn.setDoInput(true);
conn.setConnectTimeout(config.ConnectTimeout); conn.setConnectTimeout(config.ConnectTimeout);
conn.setReadTimeout(config.ReadTimeout); conn.setReadTimeout(config.ReadTimeout);
for (var key in header) { if (header) {
conn.setRequestProperty(key, header[key]); for (var key in header) {
conn.setRequestProperty(key, header[key]);
}
} }
return conn; return conn;
} }
@ -66,24 +69,28 @@ function buildUrl(url, params) {
if (queryStart == -1) { if (queryStart == -1) {
url += '?'; url += '?';
} }
for (var key in params) { return url += object2URLSearchParams(params);
url += key;
url += '=';
url += params[key];
url += '&';
}
return url.substr(0, url.length - 1);
} }
return url; return url;
} }
function request(url, method, header, params, body) { function request(config) {
var conn = open(buildUrl(url, params), method, header); var conn = open(buildUrl(config.url, config.query), config.method, config.header);
try { try {
conn.connect(); conn.connect();
if (body) { var data = config.data;
if (data) {
var out = conn.getOutputStream(); var out = conn.getOutputStream();
out.write(new String(body).getBytes(config.Charset)); if (typeof data === "object") {
var type = config.header['Content-Type'];
switch (type) {
case "application/x-www-form-urlencoded":
data = object2URLSearchParams(params);
default:
data = JSON.stringify(data)
}
}
out.write(new String(data).getBytes(config.Charset));
out.flush(); out.flush();
out.close(); out.close();
} }
@ -102,14 +109,36 @@ function response (conn) {
return result; return result;
} }
function object2URLSearchParams (params) {
var temp = []
for (var key in params) {
temp.push('%s=%s'.format(encodeURIComponent(key), encodeURIComponent(params[key])))
}
return temp.join('&')
}
var http = { var http = {
config: config config: config,
request: request
}; };
['GET', 'POST', 'PUT', 'DELETE', 'HEADER'].forEach(function(method){ ['GET', 'DELETE', 'HEAD', 'OPTIONS'].forEach(function (method) {
http[method.toLowerCase()] = function (url, header, params, body) { http[method.toLowerCase()] = function __likeGet__(url, config) {
return request(url, method, header, params, body); return this.request(Object.assign(config || {}, {
url: url,
method: method
}));
} }
}) });
['POST', 'PUT', 'PATCH'].forEach(function (method) {
http[method.toLowerCase()] = function __likePost__(url, data, config) {
return this.request(Object.assign(config || {}, {
url: url,
method: method,
data: data
}));
}
});
exports = module.exports = http; exports = module.exports = http;