debug protocol almost there (bugged)

This commit is contained in:
王劲鹏 2019-10-25 10:42:18 +08:00 committed by pengfei.zhou
parent 086beeb2c2
commit 659affc85d
4 changed files with 90 additions and 9 deletions

View File

@ -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);

View File

@ -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<String, JavaFunction> 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;
}

View File

@ -64,7 +64,7 @@ public class ValueBuilder {
Iterator<String> 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);

View File

@ -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
}
})
})