From 659affc85d303a037cb11cc8f3e4a28b761b788e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Fri, 25 Oct 2019 10:42:18 +0800 Subject: [PATCH] debug protocol almost there (bugged) --- .../java/pub/doric/demo/MainActivity.java | 2 +- .../doric/engine/remote/RemoteJSExecutor.java | 56 +++++++++++++++++-- .../pub/doric/engine/remote/ValueBuilder.java | 2 +- js-framework/index.debug.ts | 39 ++++++++++++- 4 files changed, 90 insertions(+), 9 deletions(-) diff --git a/Android/app/src/main/java/pub/doric/demo/MainActivity.java b/Android/app/src/main/java/pub/doric/demo/MainActivity.java index 86b86dba..9a9a6b8a 100644 --- a/Android/app/src/main/java/pub/doric/demo/MainActivity.java +++ b/Android/app/src/main/java/pub/doric/demo/MainActivity.java @@ -37,7 +37,7 @@ public class MainActivity extends AppCompatActivity { setContentView(R.layout.activity_main); DoricContext doricContext = DoricContext.create(this, DoricUtils.readAssetFile("demo/Snake.js"), "test"); doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); - doricContext.callEntity("log"); +// doricContext.callEntity("log"); doricContext.getRootNode().setRootView((FrameLayout) findViewById(R.id.root)); Doric.connectDevKit("ws://192.168.11.38:7777"); LocalServer localServer = new LocalServer(getApplicationContext(), 8910); diff --git a/Android/doric/src/main/java/pub/doric/engine/remote/RemoteJSExecutor.java b/Android/doric/src/main/java/pub/doric/engine/remote/RemoteJSExecutor.java index 65e5f7d5..385c644a 100644 --- a/Android/doric/src/main/java/pub/doric/engine/remote/RemoteJSExecutor.java +++ b/Android/doric/src/main/java/pub/doric/engine/remote/RemoteJSExecutor.java @@ -10,6 +10,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; import org.jetbrains.annotations.NotNull; +import org.json.JSONObject; import java.io.EOFException; import java.net.ConnectException; @@ -31,13 +32,15 @@ public class RemoteJSExecutor { private final Map globalFunctions = new HashMap<>(); + private JSDecoder temp; + public RemoteJSExecutor() { OkHttpClient okHttpClient = new OkHttpClient .Builder() .readTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) .build(); - Request request = new Request.Builder().url("ws://192.168.24.166:2080").build(); + final Request request = new Request.Builder().url("ws://192.168.24.166:2080").build(); final Thread current = Thread.currentThread(); webSocket = okHttpClient.newWebSocket(request, new WebSocketListener() { @@ -66,7 +69,7 @@ public class RemoteJSExecutor { JsonObject jo = ((JsonObject) je); String cmd = jo.get("cmd").getAsString(); switch (cmd) { - case "injectGlobalJSFunction": + case "injectGlobalJSFunction": { String name = jo.get("name").getAsString(); JsonArray arguments = jo.getAsJsonArray("arguments"); JSDecoder[] decoders = new JSDecoder[arguments.size()]; @@ -87,13 +90,36 @@ public class RemoteJSExecutor { decoders[i] = decoder; } } else { - ValueBuilder vb = new ValueBuilder(arguments.get(i)); - JSDecoder decoder = new JSDecoder(vb.build()); - decoders[i] = decoder; + try { + ValueBuilder vb = new ValueBuilder(new JSONObject(gson.toJson(arguments.get(i)))); + JSDecoder decoder = new JSDecoder(vb.build()); + decoders[i] = decoder; + } catch (Exception ex) { + ex.printStackTrace(); + } + } } + + globalFunctions.get(name).exec(decoders); - break; + } + + break; + case "invokeMethod": { + try { + Object result = jo.get("result"); + ValueBuilder vb = new ValueBuilder(result); + JSDecoder decoder = new JSDecoder(vb.build()); + temp = decoder; + System.out.println(result); + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + //LockSupport.unpark(current); + } + } + break; } } } @@ -122,6 +148,24 @@ public class RemoteJSExecutor { } public JSDecoder invokeMethod(String objectName, String functionName, JavaValue[] javaValues, boolean hashKey) { + JsonObject jo = new JsonObject(); + jo.addProperty("cmd", "invokeMethod"); + jo.addProperty("objectName", objectName); + jo.addProperty("functionName", functionName); + + JsonArray ja = new JsonArray(); + for (JavaValue javaValue : javaValues) { + JsonObject argument = new JsonObject(); + argument.addProperty("type", javaValue.getType()); + argument.addProperty("value", javaValue.getValue()); + ja.add(argument); + } + jo.add("javaValues", ja); + + jo.addProperty("hashKey", hashKey); + webSocket.send(gson.toJson(jo)); + + // LockSupport.park(Thread.currentThread()); return null; } diff --git a/Android/doric/src/main/java/pub/doric/engine/remote/ValueBuilder.java b/Android/doric/src/main/java/pub/doric/engine/remote/ValueBuilder.java index a4a2c060..d369e055 100644 --- a/Android/doric/src/main/java/pub/doric/engine/remote/ValueBuilder.java +++ b/Android/doric/src/main/java/pub/doric/engine/remote/ValueBuilder.java @@ -64,7 +64,7 @@ public class ValueBuilder { Iterator iterator = ((JSONObject) O).keys(); while (iterator.hasNext()) { String key = iterator.next(); - writeInt(output, key.hashCode()); + write(output, key); write(output, ((JSONObject) O).opt(key)); } writeInt(output, 0); diff --git a/js-framework/index.debug.ts b/js-framework/index.debug.ts index d602cd8b..79f33b81 100644 --- a/js-framework/index.debug.ts +++ b/js-framework/index.debug.ts @@ -18,7 +18,7 @@ import * as WebSocket from 'ws' let global = new Function('return this')() global.doric = doric -const contextId = "DoricDebug" +const contextId = "1" global.context = doric.jsObtainContext(contextId) global.Entry = doric.jsObtainEntry(contextId) @@ -31,6 +31,9 @@ wss.on('connection', function connection(ws) { console.log(messageObject.name) Reflect.set(global, messageObject.name, function() { let args = [].slice.call(arguments) + console.log("===============================") + console.log(args) + console.log("===============================") ws.send(JSON.stringify({ cmd: 'injectGlobalJSFunction', name: messageObject.name, @@ -38,6 +41,40 @@ wss.on('connection', function connection(ws) { })) }) break + case "invokeMethod": + console.log(messageObject.objectName) + console.log(messageObject.functionName) + + let args = [] + for (let i = 0;i < messageObject.javaValues.length;i++) { + let javaValue = messageObject.javaValues[i] + if (javaValue.type === 0) { + args.push(null) + } else if (javaValue.type === 1) { + args.push(parseFloat(javaValue.value)) + } else if (javaValue.type === 2) { + args.push((javaValue.value == 'true')) + } else if (javaValue.type === 3) { + args.push(javaValue.value.toString()) + } else if (javaValue.type === 4) { + args.push(JSON.parse(javaValue.value)) + } else if (javaValue.type === 5) { + args.push(JSON.parse(javaValue.value)) + } + } + console.log(args) + console.log(messageObject.hashKey) + + let object = Reflect.get(global, messageObject.objectName) + let method = Reflect.get(object, messageObject.functionName) + let result = Reflect.apply(method, undefined, args) + + console.log(result) + ws.send(JSON.stringify({ + cmd: 'invokeMethod', + result: result + })) + break } }) })