dev kit debug based on web socket

This commit is contained in:
王劲鹏 2019-11-12 10:44:43 +08:00
parent df0315183c
commit b1a2211dcf
8 changed files with 65 additions and 12 deletions

View File

@ -17,6 +17,8 @@ package pub.doric;
import android.app.Application; import android.app.Application;
import com.google.gson.JsonObject;
/** /**
* @Description: Doric * @Description: Doric
* @Author: pengfei.zhou * @Author: pengfei.zhou
@ -37,6 +39,10 @@ public class Doric {
DoricDriver.getInstance().connectDevKit(url); DoricDriver.getInstance().connectDevKit(url);
} }
public static void sendDevCommand(IDoricDriver.Command command, JsonObject jsonObject) {
DoricDriver.getInstance().sendDevCommand(command, jsonObject);
}
public static void disconnectDevKit() { public static void disconnectDevKit() {
DoricDriver.getInstance().disconnectDevKit(); DoricDriver.getInstance().disconnectDevKit();
} }

View File

@ -18,20 +18,22 @@ package pub.doric;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import com.github.pengfeizhou.jscore.JSDecoder;
import com.google.gson.JsonObject;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import pub.doric.async.AsyncCall; import pub.doric.async.AsyncCall;
import pub.doric.async.AsyncResult; import pub.doric.async.AsyncResult;
import pub.doric.dev.WSClient; import pub.doric.dev.WSClient;
import pub.doric.engine.DoricJSEngine; import pub.doric.engine.DoricJSEngine;
import pub.doric.utils.DoricConstant; import pub.doric.utils.DoricConstant;
import pub.doric.utils.DoricLog; import pub.doric.utils.DoricLog;
import pub.doric.utils.DoricUtils;
import pub.doric.utils.ThreadMode; import pub.doric.utils.ThreadMode;
import com.github.pengfeizhou.jscore.JSDecoder;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** /**
* @Description: Doric * @Description: Doric
* @Author: pengfei.zhou * @Author: pengfei.zhou
@ -142,6 +144,14 @@ public class DoricDriver implements IDoricDriver {
wsClient = new WSClient(url); wsClient = new WSClient(url);
} }
@Override
public void sendDevCommand(Command command, JsonObject jsonObject) {
JsonObject result = new JsonObject();
result.addProperty("cmd", command.toString());
result.add("data", jsonObject);
wsClient.send(DoricUtils.gson.toJson(result));
}
@Override @Override
public void disconnectDevKit() { public void disconnectDevKit() {
wsClient.close(); wsClient.close();

View File

@ -17,6 +17,7 @@ package pub.doric;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.google.gson.JsonObject;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
@ -29,6 +30,11 @@ import pub.doric.utils.ThreadMode;
* @CreateDate: 2019-07-19 * @CreateDate: 2019-07-19
*/ */
public interface IDoricDriver { public interface IDoricDriver {
enum Command {
DEBUG, HOT_RELOAD
}
AsyncResult<JSDecoder> invokeContextEntityMethod(final String contextId, final String method, final Object... args); AsyncResult<JSDecoder> invokeContextEntityMethod(final String contextId, final String method, final Object... args);
AsyncResult<JSDecoder> invokeDoricMethod(final String method, final Object... args); AsyncResult<JSDecoder> invokeDoricMethod(final String method, final Object... args);
@ -43,5 +49,7 @@ public interface IDoricDriver {
void connectDevKit(String url); void connectDevKit(String url);
void sendDevCommand(Command command, JsonObject jsonObject);
void disconnectDevKit(); void disconnectDevKit();
} }

View File

@ -12,6 +12,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment; import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.gson.JsonObject;
import com.tbruyelle.rxpermissions2.RxPermissions; import com.tbruyelle.rxpermissions2.RxPermissions;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
@ -20,6 +21,10 @@ import org.greenrobot.eventbus.ThreadMode;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer; import io.reactivex.functions.Consumer;
import pub.doric.Doric;
import pub.doric.DoricContext;
import pub.doric.DoricContextManager;
import pub.doric.IDoricDriver;
import pub.doric.R; import pub.doric.R;
import pub.doric.dev.event.EOFEvent; import pub.doric.dev.event.EOFEvent;
import pub.doric.dev.event.OpenEvent; import pub.doric.dev.event.OpenEvent;
@ -64,6 +69,17 @@ public class DevPanel extends BottomSheetDialogFragment {
} }
}); });
getView().findViewById(R.id.debug_text_view).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (DoricContext doricContext : DoricContextManager.aliveContexts()) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("contextId", doricContext.getContextId());
Doric.sendDevCommand(IDoricDriver.Command.DEBUG, jsonObject);
}
}
});
} }
@Override @Override

View File

@ -96,4 +96,8 @@ public class WSClient extends WebSocketListener {
EventBus.getDefault().post(new EOFEvent()); EventBus.getDefault().post(new EOFEvent());
} }
} }
public void send(String command) {
webSocket.send(command);
}
} }

View File

@ -3,7 +3,6 @@ package pub.doric.engine.remote;
import com.github.pengfeizhou.jscore.JSDecoder; 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.JsonArray; 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;
@ -24,11 +23,11 @@ import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import okhttp3.WebSocket; import okhttp3.WebSocket;
import okhttp3.WebSocketListener; import okhttp3.WebSocketListener;
import pub.doric.utils.DoricUtils;
public class RemoteJSExecutor { public class RemoteJSExecutor {
private final WebSocket webSocket; private final WebSocket webSocket;
private final Gson gson = new Gson();
private final Map<String, JavaFunction> globalFunctions = new HashMap<>(); private final Map<String, JavaFunction> globalFunctions = new HashMap<>();
@ -63,7 +62,7 @@ public class RemoteJSExecutor {
@Override @Override
public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) { public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {
JsonElement je = gson.fromJson(text, JsonElement.class); JsonElement je = DoricUtils.gson.fromJson(text, JsonElement.class);
if (je instanceof JsonObject) { if (je instanceof JsonObject) {
JsonObject jo = ((JsonObject) je); JsonObject jo = ((JsonObject) je);
@ -91,7 +90,7 @@ public class RemoteJSExecutor {
} }
} else { } else {
try { try {
ValueBuilder vb = new ValueBuilder(new JSONObject(gson.toJson(arguments.get(i)))); ValueBuilder vb = new ValueBuilder(new JSONObject(DoricUtils.gson.toJson(arguments.get(i))));
JSDecoder decoder = new JSDecoder(vb.build()); JSDecoder decoder = new JSDecoder(vb.build());
decoders[i] = decoder; decoders[i] = decoder;
} catch (Exception ex) { } catch (Exception ex) {
@ -141,7 +140,7 @@ public class RemoteJSExecutor {
JsonObject jo = new JsonObject(); JsonObject jo = new JsonObject();
jo.addProperty("cmd", "injectGlobalJSFunction"); jo.addProperty("cmd", "injectGlobalJSFunction");
jo.addProperty("name", name); jo.addProperty("name", name);
webSocket.send(gson.toJson(jo)); webSocket.send(DoricUtils.gson.toJson(jo));
} }
public void injectGlobalJSObject(String name, JavaValue javaValue) { public void injectGlobalJSObject(String name, JavaValue javaValue) {
@ -163,7 +162,7 @@ public class RemoteJSExecutor {
jo.add("javaValues", ja); jo.add("javaValues", ja);
jo.addProperty("hashKey", hashKey); jo.addProperty("hashKey", hashKey);
webSocket.send(gson.toJson(jo)); webSocket.send(DoricUtils.gson.toJson(jo));
LockSupport.park(Thread.currentThread()); LockSupport.park(Thread.currentThread());
return temp; return temp;

View File

@ -31,6 +31,7 @@ import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSONBuilder; import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import com.github.pengfeizhou.jscore.JavaValue; import com.github.pengfeizhou.jscore.JavaValue;
import com.google.gson.Gson;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -46,6 +47,8 @@ import java.lang.reflect.Field;
* @CreateDate: 2019-07-18 * @CreateDate: 2019-07-18
*/ */
public class DoricUtils { public class DoricUtils {
public static Gson gson = new Gson();
public static String readAssetFile(String assetFile) { public static String readAssetFile(String assetFile) {
InputStream inputStream = null; InputStream inputStream = null;
try { try {

View File

@ -5,6 +5,13 @@ const createServer = () => {
console.log('connected', connection.key) console.log('connected', connection.key)
connection.on('text', function (result) { connection.on('text', function (result) {
console.log('text', result) console.log('text', result)
let resultObject = JSON.parse(result)
switch(resultObject.cmd) {
case 'debug':
let contextId = resultObject.contextId
break
}
}) })
connection.on('connect', function (code) { connection.on('connect', function (code) {
console.log('connect', code) console.log('connect', code)