From f46ac2ae73f5d39866d91b1766d1716d901f4f62 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Tue, 13 Aug 2019 17:22:56 +0800 Subject: [PATCH] websocket dev tool --- .gitignore | 2 +- Android/app/src/main/AndroidManifest.xml | 1 + .../penfeizhou/doricdemo/MainActivity.java | 8 ++- .../main/res/xml/network_security_config.xml | 4 ++ .../github/penfeizhou/doric/DoricContext.java | 13 +++++ .../github/penfeizhou/doric/dev/WSClient.java | 2 +- .../penfeizhou/doric/shader/GroupNode.java | 37 +++++++++++--- doric-cli/index.js | 1 - doric-cli/package-lock.json | 5 ++ doric-cli/package.json | 1 + doric-cli/scripts/watcher.js | 49 +++++++++++++++---- 11 files changed, 97 insertions(+), 26 deletions(-) create mode 100644 Android/app/src/main/res/xml/network_security_config.xml diff --git a/.gitignore b/.gitignore index 39b6783c..b71869f9 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,7 @@ bin/ gen/ out/ - +test/ # Gradle files .gradle/ build/ diff --git a/Android/app/src/main/AndroidManifest.xml b/Android/app/src/main/AndroidManifest.xml index e56409b3..0f292422 100644 --- a/Android/app/src/main/AndroidManifest.xml +++ b/Android/app/src/main/AndroidManifest.xml @@ -9,6 +9,7 @@ android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" + android:networkSecurityConfig="@xml/network_security_config" android:theme="@style/AppTheme"> diff --git a/Android/app/src/main/java/com/github/penfeizhou/doricdemo/MainActivity.java b/Android/app/src/main/java/com/github/penfeizhou/doricdemo/MainActivity.java index 08994ed5..0b930451 100644 --- a/Android/app/src/main/java/com/github/penfeizhou/doricdemo/MainActivity.java +++ b/Android/app/src/main/java/com/github/penfeizhou/doricdemo/MainActivity.java @@ -20,13 +20,11 @@ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - DoricContext doricContext = DoricContext.create(this, DoricUtils.readAssetFile("demo/Snake.js"), "demo for test"); - doricContext.callEntity("__init__", new JSONBuilder() - .put("width", ViewGroup.LayoutParams.MATCH_PARENT) - .put("height", ViewGroup.LayoutParams.MATCH_PARENT)); + 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.getRootNode().setRootView((FrameLayout) findViewById(R.id.root)); - Doric.connectDevKit("wss://192.168.11.38:7777"); + Doric.connectDevKit("ws://192.168.11.38:7777"); LocalServer localServer = new LocalServer(getApplicationContext(), 8910); try { localServer.start(); diff --git a/Android/app/src/main/res/xml/network_security_config.xml b/Android/app/src/main/res/xml/network_security_config.xml new file mode 100644 index 00000000..dca93c07 --- /dev/null +++ b/Android/app/src/main/res/xml/network_security_config.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file 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 471fe994..f397efe1 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 @@ -1,6 +1,7 @@ package com.github.penfeizhou.doric; import android.content.Context; +import android.view.ViewGroup; import com.github.penfeizhou.doric.async.AsyncResult; import com.github.penfeizhou.doric.plugin.DoricJavaPlugin; @@ -8,6 +9,9 @@ import com.github.penfeizhou.doric.utils.DoricConstant; import com.github.penfeizhou.doric.utils.DoricMetaInfo; import com.github.penfeizhou.doric.shader.RootNode; import com.github.pengfeizhou.jscore.JSDecoder; +import com.github.pengfeizhou.jscore.JSONBuilder; + +import org.json.JSONObject; import java.util.HashMap; import java.util.Map; @@ -24,6 +28,7 @@ public class DoricContext { private RootNode mRootNode = new RootNode(this); private final String source; private String script; + private JSONObject initParams; DoricContext(Context context, String contextId, String source) { this.mContext = context; @@ -46,6 +51,13 @@ public class DoricContext { return doricContext; } + public void init(int width, int height) { + this.initParams = new JSONBuilder() + .put("width", width) + .put("height", height).toJSONObject(); + callEntity(DoricConstant.DORIC_ENTITY_INIT, this.initParams); + } + public AsyncResult callEntity(String methodName, Object... args) { return getDriver().invokeContextEntityMethod(mContextId, methodName, args); } @@ -97,6 +109,7 @@ public class DoricContext { public void reload(String script) { getDriver().createContext(mContextId, script, source); + callEntity(DoricConstant.DORIC_ENTITY_INIT, this.initParams); } public void onShow() { 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 index 79b58b23..ef507245 100644 --- 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 @@ -51,7 +51,7 @@ public class WSClient extends WebSocketListener { String source = jsonObject.optString("source"); String script = jsonObject.optString("script"); for (DoricContext context : DoricContextManager.aliveContexts()) { - if (source.equals(context.getSource())) { + if (source.contains(context.getSource())) { context.reload(script); } } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/shader/GroupNode.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/shader/GroupNode.java index 173b68a5..b0655ea4 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/shader/GroupNode.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/shader/GroupNode.java @@ -9,7 +9,9 @@ import com.github.pengfeizhou.jscore.JSArray; import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSValue; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -32,6 +34,7 @@ public abstract class GroupNode extends ViewNode { case "children": JSArray jsArray = prop.asArray(); int i; + List tobeRemoved = new ArrayList<>(); for (i = 0; i < jsArray.size(); i++) { JSValue jsValue = jsArray.get(i); if (!jsValue.isObject()) { @@ -47,11 +50,23 @@ public abstract class GroupNode extends ViewNode { child.mParent = this; child.mId = id; mChildrenNode.put(id, child); - } else if (i != child.index) { - mIndexInfo.remove(i); - child.index = i; - mView.removeView(child.getView()); + } else { + if (i != child.index) { + mIndexInfo.remove(i); + child.index = i; + mView.removeView(child.getView()); + } + tobeRemoved.remove(child); } + + ViewNode node = mIndexInfo.get(i); + + if (node != null && node != child) { + mView.removeViewAt(i); + mIndexInfo.remove(i); + tobeRemoved.add(node); + } + ViewGroup.LayoutParams params = child.getLayoutParams(); if (params == null) { params = generateDefaultLayoutParams(); @@ -62,14 +77,20 @@ public abstract class GroupNode extends ViewNode { mIndexInfo.put(i, child); } } - while (i < mView.getChildCount()) { - mView.removeViewAt(mView.getChildCount() - 1); - if (mIndexInfo.get(i) != null) { - mChildrenNode.remove(mIndexInfo.get(i).getId()); + + while (i < mIndexInfo.size()) { + ViewNode node = mIndexInfo.get(i); + if (node != null) { + mChildrenNode.remove(node.getId()); mIndexInfo.remove(i); + tobeRemoved.remove(node); } i++; } + + for (ViewNode node : tobeRemoved) { + mChildrenNode.remove(node.getId()); + } break; default: super.blend(view, layoutParams, name, prop); diff --git a/doric-cli/index.js b/doric-cli/index.js index 2aa124e4..86d6b91d 100755 --- a/doric-cli/index.js +++ b/doric-cli/index.js @@ -15,4 +15,3 @@ program require('./scripts/watcher') }) program.parse(process.argv) -console.log(process.cwd()) \ No newline at end of file diff --git a/doric-cli/package-lock.json b/doric-cli/package-lock.json index 0e0ddefb..9bfe5d0b 100644 --- a/doric-cli/package-lock.json +++ b/doric-cli/package-lock.json @@ -211,6 +211,11 @@ "resolved": "https://registry.npm.taobao.org/picomatch/download/picomatch-2.0.7.tgz", "integrity": "sha1-UUFp2MfNC9vuzIomCeNKcWPeafY=" }, + "qrcode-terminal": { + "version": "0.12.0", + "resolved": "https://registry.npmjs.org/qrcode-terminal/-/qrcode-terminal-0.12.0.tgz", + "integrity": "sha512-EXtzRZmC+YGmGlDFbXKxQiMZNwCLEO6BANKXG4iCtSIM0yqc/pappSx3RIKr4r0uh5JsBckOXeKrB3Iz7mdQpQ==" + }, "readdirp": { "version": "3.1.1", "resolved": "https://registry.npm.taobao.org/readdirp/download/readdirp-3.1.1.tgz", diff --git a/doric-cli/package.json b/doric-cli/package.json index a1fffbcf..b852d370 100644 --- a/doric-cli/package.json +++ b/doric-cli/package.json @@ -24,6 +24,7 @@ "chokidar": "^3.0.2", "commander": "^2.20.0", "nodejs-websocket": "^1.7.2", + "qrcode-terminal": "^0.12.0", "rollup": "^1.18.0", "shelljs": "^0.8.3", "typescript": "^3.5.3" diff --git a/doric-cli/scripts/watcher.js b/doric-cli/scripts/watcher.js index 4b9df594..041dcda7 100644 --- a/doric-cli/scripts/watcher.js +++ b/doric-cli/scripts/watcher.js @@ -4,19 +4,48 @@ const fs = require("fs") require('shelljs/global') -exec('npm run dev', () => { -}) -ws.listen(7777) -chokidar.watch(process.cwd() + "/bundle", { - ignored: /(^|[\/\\])\../, -}).on('all', (event, path) => { - console.log('path is ', path) - if (event === 'add' || event === 'change') { +exec('npm run dev >/dev/null 2>&1', { async: true }) +console.warn('Waiting ...') +setTimeout(() => { + console.warn('Start watching') + ws.listen(7777) + chokidar.watch(process.cwd() + "/bundle", { + ignored: /(^|[\/\\])\../, + }).on('change', (path) => { + console.log('path is ', path) fs.readFile(path, 'utf-8', (error, data) => { console.log('send data ', data) ws.connections.forEach(e => { - e.sendText(data) + e.sendText(JSON.stringify({ + script: data, + source: path.match(/[^/\\]*$/)[0], + })) }) }) + }); +}, 3000); +const os = require('os'); + +function getIPAdress() { + const ret = [] + var interfaces = os.networkInterfaces(); + for (var devName in interfaces) { + var iface = interfaces[devName]; + for (var i = 0; i < iface.length; i++) { + var alias = iface[i]; + if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) { + ret.push(alias.address); + } + } } -}); + return ret +} + + +const qrcode = require('qrcode-terminal'); +const ips = getIPAdress() +console.log(`本机IP是${ips}`) +ips.forEach(e => { + console.log(`IP:${e}`) + qrcode.generate(e, { small: true }); +})