add doric ws client
This commit is contained in:
parent
e367093a6a
commit
9a13cdb3ad
@ -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")
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,14 +21,20 @@ public class DoricContext {
|
||||
private final Map<String, DoricJavaPlugin> 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<JSDecoder> callEntity(String methodName, Object... args) {
|
||||
@ -78,4 +84,8 @@ public class DoricContext {
|
||||
}
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public void reload(String script) {
|
||||
getDriver().createContext(mContextId, script, source);
|
||||
}
|
||||
}
|
||||
|
@ -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<String> getKeySet() {
|
||||
return getInstance().doricContextMap.keySet();
|
||||
}
|
||||
|
||||
public static Collection<DoricContext> aliveContexts() {
|
||||
return getInstance().doricContextMap.values();
|
||||
}
|
||||
}
|
||||
|
@ -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<JSDecoder> 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;
|
||||
}
|
||||
}
|
||||
|
@ -24,4 +24,8 @@ public interface IDoricDriver {
|
||||
AsyncResult<Boolean> destroyContext(final String contextId);
|
||||
|
||||
DoricRegistry getRegistry();
|
||||
|
||||
void connectDevKit(String url);
|
||||
|
||||
void disconnectDevKit();
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user