websocket dev tool

This commit is contained in:
pengfei.zhou 2019-08-13 17:22:56 +08:00
parent 76fe0f9044
commit f46ac2ae73
11 changed files with 97 additions and 26 deletions

2
.gitignore vendored
View File

@ -12,7 +12,7 @@
bin/ bin/
gen/ gen/
out/ out/
test/
# Gradle files # Gradle files
.gradle/ .gradle/
build/ build/

View File

@ -9,6 +9,7 @@
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:networkSecurityConfig="@xml/network_security_config"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity android:name="com.github.penfeizhou.doricdemo.MainActivity"> <activity android:name="com.github.penfeizhou.doricdemo.MainActivity">
<intent-filter> <intent-filter>

View File

@ -20,13 +20,11 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
DoricContext doricContext = DoricContext.create(this, DoricUtils.readAssetFile("demo/Snake.js"), "demo for test"); DoricContext doricContext = DoricContext.create(this, DoricUtils.readAssetFile("demo/Snake.js"), "test");
doricContext.callEntity("__init__", new JSONBuilder() doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
.put("width", ViewGroup.LayoutParams.MATCH_PARENT)
.put("height", ViewGroup.LayoutParams.MATCH_PARENT));
doricContext.callEntity("log"); doricContext.callEntity("log");
doricContext.getRootNode().setRootView((FrameLayout) findViewById(R.id.root)); 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); LocalServer localServer = new LocalServer(getApplicationContext(), 8910);
try { try {
localServer.start(); localServer.start();

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>

View File

@ -1,6 +1,7 @@
package com.github.penfeizhou.doric; package com.github.penfeizhou.doric;
import android.content.Context; import android.content.Context;
import android.view.ViewGroup;
import com.github.penfeizhou.doric.async.AsyncResult; import com.github.penfeizhou.doric.async.AsyncResult;
import com.github.penfeizhou.doric.plugin.DoricJavaPlugin; 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.utils.DoricMetaInfo;
import com.github.penfeizhou.doric.shader.RootNode; import com.github.penfeizhou.doric.shader.RootNode;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSONBuilder;
import org.json.JSONObject;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -24,6 +28,7 @@ public class DoricContext {
private RootNode mRootNode = new RootNode(this); private RootNode mRootNode = new RootNode(this);
private final String source; private final String source;
private String script; private String script;
private JSONObject initParams;
DoricContext(Context context, String contextId, String source) { DoricContext(Context context, String contextId, String source) {
this.mContext = context; this.mContext = context;
@ -46,6 +51,13 @@ public class DoricContext {
return 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<JSDecoder> callEntity(String methodName, Object... args) { public AsyncResult<JSDecoder> callEntity(String methodName, Object... args) {
return getDriver().invokeContextEntityMethod(mContextId, methodName, args); return getDriver().invokeContextEntityMethod(mContextId, methodName, args);
} }
@ -97,6 +109,7 @@ public class DoricContext {
public void reload(String script) { public void reload(String script) {
getDriver().createContext(mContextId, script, source); getDriver().createContext(mContextId, script, source);
callEntity(DoricConstant.DORIC_ENTITY_INIT, this.initParams);
} }
public void onShow() { public void onShow() {

View File

@ -51,7 +51,7 @@ public class WSClient extends WebSocketListener {
String source = jsonObject.optString("source"); String source = jsonObject.optString("source");
String script = jsonObject.optString("script"); String script = jsonObject.optString("script");
for (DoricContext context : DoricContextManager.aliveContexts()) { for (DoricContext context : DoricContextManager.aliveContexts()) {
if (source.equals(context.getSource())) { if (source.contains(context.getSource())) {
context.reload(script); context.reload(script);
} }
} }

View File

@ -9,7 +9,9 @@ import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -32,6 +34,7 @@ public abstract class GroupNode<F extends ViewGroup> extends ViewNode<F> {
case "children": case "children":
JSArray jsArray = prop.asArray(); JSArray jsArray = prop.asArray();
int i; int i;
List<ViewNode> tobeRemoved = new ArrayList<>();
for (i = 0; i < jsArray.size(); i++) { for (i = 0; i < jsArray.size(); i++) {
JSValue jsValue = jsArray.get(i); JSValue jsValue = jsArray.get(i);
if (!jsValue.isObject()) { if (!jsValue.isObject()) {
@ -47,11 +50,23 @@ public abstract class GroupNode<F extends ViewGroup> extends ViewNode<F> {
child.mParent = this; child.mParent = this;
child.mId = id; child.mId = id;
mChildrenNode.put(id, child); mChildrenNode.put(id, child);
} else if (i != child.index) { } else {
if (i != child.index) {
mIndexInfo.remove(i); mIndexInfo.remove(i);
child.index = i; child.index = i;
mView.removeView(child.getView()); 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(); ViewGroup.LayoutParams params = child.getLayoutParams();
if (params == null) { if (params == null) {
params = generateDefaultLayoutParams(); params = generateDefaultLayoutParams();
@ -62,14 +77,20 @@ public abstract class GroupNode<F extends ViewGroup> extends ViewNode<F> {
mIndexInfo.put(i, child); mIndexInfo.put(i, child);
} }
} }
while (i < mView.getChildCount()) {
mView.removeViewAt(mView.getChildCount() - 1); while (i < mIndexInfo.size()) {
if (mIndexInfo.get(i) != null) { ViewNode node = mIndexInfo.get(i);
mChildrenNode.remove(mIndexInfo.get(i).getId()); if (node != null) {
mChildrenNode.remove(node.getId());
mIndexInfo.remove(i); mIndexInfo.remove(i);
tobeRemoved.remove(node);
} }
i++; i++;
} }
for (ViewNode node : tobeRemoved) {
mChildrenNode.remove(node.getId());
}
break; break;
default: default:
super.blend(view, layoutParams, name, prop); super.blend(view, layoutParams, name, prop);

View File

@ -15,4 +15,3 @@ program
require('./scripts/watcher') require('./scripts/watcher')
}) })
program.parse(process.argv) program.parse(process.argv)
console.log(process.cwd())

View File

@ -211,6 +211,11 @@
"resolved": "https://registry.npm.taobao.org/picomatch/download/picomatch-2.0.7.tgz", "resolved": "https://registry.npm.taobao.org/picomatch/download/picomatch-2.0.7.tgz",
"integrity": "sha1-UUFp2MfNC9vuzIomCeNKcWPeafY=" "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": { "readdirp": {
"version": "3.1.1", "version": "3.1.1",
"resolved": "https://registry.npm.taobao.org/readdirp/download/readdirp-3.1.1.tgz", "resolved": "https://registry.npm.taobao.org/readdirp/download/readdirp-3.1.1.tgz",

View File

@ -24,6 +24,7 @@
"chokidar": "^3.0.2", "chokidar": "^3.0.2",
"commander": "^2.20.0", "commander": "^2.20.0",
"nodejs-websocket": "^1.7.2", "nodejs-websocket": "^1.7.2",
"qrcode-terminal": "^0.12.0",
"rollup": "^1.18.0", "rollup": "^1.18.0",
"shelljs": "^0.8.3", "shelljs": "^0.8.3",
"typescript": "^3.5.3" "typescript": "^3.5.3"

View File

@ -4,19 +4,48 @@ const fs = require("fs")
require('shelljs/global') require('shelljs/global')
exec('npm run dev', () => { exec('npm run dev >/dev/null 2>&1', { async: true })
}) console.warn('Waiting ...')
setTimeout(() => {
console.warn('Start watching')
ws.listen(7777) ws.listen(7777)
chokidar.watch(process.cwd() + "/bundle", { chokidar.watch(process.cwd() + "/bundle", {
ignored: /(^|[\/\\])\../, ignored: /(^|[\/\\])\../,
}).on('all', (event, path) => { }).on('change', (path) => {
console.log('path is ', path) console.log('path is ', path)
if (event === 'add' || event === 'change') {
fs.readFile(path, 'utf-8', (error, data) => { fs.readFile(path, 'utf-8', (error, data) => {
console.log('send data ', data) console.log('send data ', data)
ws.connections.forEach(e => { 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 });
})