js call native over ws completed

This commit is contained in:
王劲鹏 2019-10-24 10:39:06 +08:00 committed by pengfei.zhou
parent 9864b57b21
commit 086beeb2c2
2 changed files with 28 additions and 4 deletions

View File

@ -4,8 +4,10 @@ import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JavaFunction; import com.github.pengfeizhou.jscore.JavaFunction;
import com.github.pengfeizhou.jscore.JavaValue; import com.github.pengfeizhou.jscore.JavaValue;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -66,10 +68,31 @@ public class RemoteJSExecutor {
switch (cmd) { switch (cmd) {
case "injectGlobalJSFunction": case "injectGlobalJSFunction":
String name = jo.get("name").getAsString(); String name = jo.get("name").getAsString();
JsonObject arguments = jo.getAsJsonObject("arguments"); JsonArray arguments = jo.getAsJsonArray("arguments");
for (String key : arguments.keySet()) { JSDecoder[] decoders = new JSDecoder[arguments.size()];
System.out.println(key + " " + arguments.get(key)); for (int i = 0; i < arguments.size(); i++) {
if (arguments.get(i).isJsonPrimitive()) {
JsonPrimitive jp = ((JsonPrimitive) arguments.get(i));
if (jp.isNumber()) {
ValueBuilder vb = new ValueBuilder(jp.getAsNumber());
JSDecoder decoder = new JSDecoder(vb.build());
decoders[i] = decoder;
} else if (jp.isBoolean()) {
ValueBuilder vb = new ValueBuilder(jp.getAsBoolean());
JSDecoder decoder = new JSDecoder(vb.build());
decoders[i] = decoder;
} else if (jp.isString()) {
ValueBuilder vb = new ValueBuilder(jp.getAsString());
JSDecoder decoder = new JSDecoder(vb.build());
decoders[i] = decoder;
} }
} else {
ValueBuilder vb = new ValueBuilder(arguments.get(i));
JSDecoder decoder = new JSDecoder(vb.build());
decoders[i] = decoder;
}
}
globalFunctions.get(name).exec(decoders);
break; break;
} }
} }

View File

@ -30,10 +30,11 @@ wss.on('connection', function connection(ws) {
case "injectGlobalJSFunction": case "injectGlobalJSFunction":
console.log(messageObject.name) console.log(messageObject.name)
Reflect.set(global, messageObject.name, function() { Reflect.set(global, messageObject.name, function() {
let args = [].slice.call(arguments)
ws.send(JSON.stringify({ ws.send(JSON.stringify({
cmd: 'injectGlobalJSFunction', cmd: 'injectGlobalJSFunction',
name: messageObject.name, name: messageObject.name,
arguments: arguments arguments: args
})) }))
}) })
break break