From be95acd001efafacacdb4c34980bc426cb17fce2 Mon Sep 17 00:00:00 2001 From: coding Date: Thu, 11 Jan 2018 14:46:47 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0http=E7=B1=BB?= =?UTF-8?q?=E5=BA=93=20server=E9=80=9A=E8=BF=87orElse=E8=BF=94=E5=9B=9Eund?= =?UTF-8?q?efined?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/internal/sponge/server.js | 10 +-- src/main/resources/modules/http.js | 67 ++++++++++++++------ 2 files changed, 54 insertions(+), 23 deletions(-) diff --git a/src/main/resources/internal/sponge/server.js b/src/main/resources/internal/sponge/server.js index ff1fd6e..992501a 100644 --- a/src/main/resources/internal/sponge/server.js +++ b/src/main/resources/internal/sponge/server.js @@ -38,7 +38,7 @@ exports.plugin = { } return PluginManager.isPluginEnabled(name); }, - self: PluginManager.getPlugin('miaoscript').get() + self: PluginManager.getPlugin('miaoscript').orElse(undefined) }; /** * 获取玩家 @@ -48,9 +48,9 @@ exports.player = function () { case 0: return undefined; case 1: - return Server.getPlayer(arguments[0]).get(); + return Server.getPlayer(arguments[0]).orElse(undefined); default: - return Server.getPlayer(arguments[0]).get(); + return Server.getPlayer(arguments[0]).orElse(undefined); } }; /** @@ -59,8 +59,10 @@ exports.player = function () { exports.players = function () { switch (arguments.length) { case 1: + // 此处的forEach是Collection接口的 return Server.onlinePlayers.forEach(arguments[0]); default: - return Server.onlinePlayers; + // 此处会转换为JS原生的Array + return Java.from(Server.onlinePlayers.toArray()); } }; \ No newline at end of file diff --git a/src/main/resources/modules/http.js b/src/main/resources/modules/http.js index a90bc93..d71fae7 100644 --- a/src/main/resources/modules/http.js +++ b/src/main/resources/modules/http.js @@ -39,7 +39,8 @@ var SSLSocketFactory = function initSSLSocketFactory() { var config = { Charset: 'UTF-8', ConnectTimeout: 10000, - ReadTimeout: 10000 + ReadTimeout: 10000, + Debug: false } function open(url, method, header) { @@ -54,8 +55,10 @@ function open(url, method, header) { conn.setDoInput(true); conn.setConnectTimeout(config.ConnectTimeout); conn.setReadTimeout(config.ReadTimeout); - for (var key in header) { - conn.setRequestProperty(key, header[key]); + if (header) { + for (var key in header) { + conn.setRequestProperty(key, header[key]); + } } return conn; } @@ -66,24 +69,28 @@ function buildUrl(url, params) { if (queryStart == -1) { url += '?'; } - for (var key in params) { - url += key; - url += '='; - url += params[key]; - url += '&'; - } - return url.substr(0, url.length - 1); + return url += object2URLSearchParams(params); } return url; } -function request(url, method, header, params, body) { - var conn = open(buildUrl(url, params), method, header); +function request(config) { + var conn = open(buildUrl(config.url, config.query), config.method, config.header); try { conn.connect(); - if (body) { + var data = config.data; + if (data) { 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.close(); } @@ -102,14 +109,36 @@ function response (conn) { 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 = { - config: config + config: config, + request: request }; -['GET', 'POST', 'PUT', 'DELETE', 'HEADER'].forEach(function(method){ - http[method.toLowerCase()] = function (url, header, params, body) { - return request(url, method, header, params, body); +['GET', 'DELETE', 'HEAD', 'OPTIONS'].forEach(function (method) { + http[method.toLowerCase()] = function __likeGet__(url, config) { + 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;