diff --git a/Android/doric/build.gradle b/Android/doric/build.gradle index 02d5bd7d..d527b9c6 100644 --- a/Android/doric/build.gradle +++ b/Android/doric/build.gradle @@ -45,4 +45,5 @@ dependencies { androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' api 'com.github.pengfeizhou:jsc4a:0.1.0' + implementation("com.squareup.okhttp3:okhttp:3.11.0") } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/Doric.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/Doric.java index 5ec14f8c..23fe117e 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/Doric.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/Doric.java @@ -18,4 +18,12 @@ public class Doric { return sApplication; } + public static void connectDevKit(String url) { + DoricDriver.getInstance().connectDevKit(url); + } + + public static void disconnectDevKit() { + DoricDriver.getInstance().disconnectDevKit(); + } + } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContext.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContext.java index 4467096a..add8ee4f 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContext.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContext.java @@ -21,14 +21,20 @@ public class DoricContext { private final Map mPluginMap = new HashMap<>(); private final Context mContext; private RootNode mRootNode = new RootNode(this); + private final String source; - DoricContext(Context context, String contextId) { + DoricContext(Context context, String contextId, String source) { this.mContext = context; this.mContextId = contextId; + this.source = source; } - public static DoricContext create(Context context, String script, String alias) { - return DoricContextManager.getInstance().createContext(context, script, alias); + public String getSource() { + return source; + } + + public static DoricContext create(Context context, String script, String source) { + return DoricContextManager.getInstance().createContext(context, script, source); } public AsyncResult callEntity(String methodName, Object... args) { @@ -78,4 +84,8 @@ public class DoricContext { } return plugin; } + + public void reload(String script) { + getDriver().createContext(mContextId, script, source); + } } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContextManager.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContextManager.java index a531397f..9f44a8c9 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContextManager.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContextManager.java @@ -4,7 +4,9 @@ import android.content.Context; import com.github.penfeizhou.doric.async.AsyncResult; +import java.util.Collection; import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; @@ -33,7 +35,7 @@ public class DoricContextManager { DoricContext createContext(Context context, final String script, final String source) { final String contextId = String.valueOf(counter.incrementAndGet()); - final DoricContext doricContext = new DoricContext(context, contextId); + final DoricContext doricContext = new DoricContext(context, contextId, source); doricContextMap.put(contextId, doricContext); doricContext.getDriver().createContext(contextId, script, source); return doricContext; @@ -63,4 +65,12 @@ public class DoricContextManager { public static DoricContext getContext(String contextId) { return getInstance().doricContextMap.get(contextId); } + + public static Set getKeySet() { + return getInstance().doricContextMap.keySet(); + } + + public static Collection aliveContexts() { + return getInstance().doricContextMap.values(); + } } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricDriver.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricDriver.java index 9e6053d5..66c05119 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricDriver.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricDriver.java @@ -5,6 +5,7 @@ import android.os.Looper; import com.github.penfeizhou.doric.async.AsyncCall; import com.github.penfeizhou.doric.async.AsyncResult; +import com.github.penfeizhou.doric.dev.WSClient; import com.github.penfeizhou.doric.engine.DoricJSEngine; import com.github.penfeizhou.doric.utils.DoricConstant; import com.github.penfeizhou.doric.utils.DoricLog; @@ -25,6 +26,7 @@ public class DoricDriver implements IDoricDriver { private final ExecutorService mBridgeExecutor; private final Handler mUIHandler; private final Handler mJSHandler; + private WSClient wsClient; @Override public AsyncResult invokeContextEntityMethod(final String contextId, final String method, final Object... args) { @@ -118,4 +120,15 @@ public class DoricDriver implements IDoricDriver { public DoricRegistry getRegistry() { return doricJSEngine.getRegistry(); } + + @Override + public void connectDevKit(String url) { + wsClient = new WSClient(url); + } + + @Override + public void disconnectDevKit() { + wsClient.close(); + wsClient = null; + } } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/IDoricDriver.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/IDoricDriver.java index d026fb57..0e1b7cf5 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/IDoricDriver.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/IDoricDriver.java @@ -24,4 +24,8 @@ public interface IDoricDriver { AsyncResult destroyContext(final String contextId); DoricRegistry getRegistry(); + + void connectDevKit(String url); + + void disconnectDevKit(); } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/dev/WSClient.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/dev/WSClient.java new file mode 100644 index 00000000..79b58b23 --- /dev/null +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/dev/WSClient.java @@ -0,0 +1,78 @@ +package com.github.penfeizhou.doric.dev; + +import com.github.penfeizhou.doric.DoricContext; +import com.github.penfeizhou.doric.DoricContextManager; +import com.github.penfeizhou.doric.DoricDriver; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.WebSocket; +import okhttp3.WebSocketListener; + +/** + * @Description: com.github.penfeizhou.doric.dev + * @Author: pengfei.zhou + * @CreateDate: 2019-08-03 + */ +public class WSClient extends WebSocketListener { + private final WebSocket webSocket; + + public WSClient(String url) { + OkHttpClient okHttpClient = new OkHttpClient + .Builder() + .readTimeout(10, TimeUnit.SECONDS) + .writeTimeout(10, TimeUnit.SECONDS) + .build(); + Request request = new Request.Builder().url(url).build(); + webSocket = okHttpClient.newWebSocket(request, this); + } + + public void close() { + webSocket.close(-1, "Close"); + } + + @Override + public void onOpen(WebSocket webSocket, Response response) { + super.onOpen(webSocket, response); + } + + @Override + public void onMessage(WebSocket webSocket, String text) { + super.onMessage(webSocket, text); + try { + JSONObject jsonObject = new JSONObject(text); + String source = jsonObject.optString("source"); + String script = jsonObject.optString("script"); + for (DoricContext context : DoricContextManager.aliveContexts()) { + if (source.equals(context.getSource())) { + context.reload(script); + } + } + } catch (JSONException e) { + e.printStackTrace(); + } + + } + + @Override + public void onClosing(WebSocket webSocket, int code, String reason) { + super.onClosing(webSocket, code, reason); + } + + @Override + public void onClosed(WebSocket webSocket, int code, String reason) { + super.onClosed(webSocket, code, reason); + } + + @Override + public void onFailure(WebSocket webSocket, Throwable t, Response response) { + super.onFailure(webSocket, t, response); + } +}