浏览代码

state management

master
Martin Dørum 6 年前
父节点
当前提交
2f864c9b12
共有 2 个文件被更改,包括 31 次插入26 次删除
  1. 30
    25
      web/console/conn.js
  2. 1
    1
      web/console/index.html

+ 30
- 25
web/console/conn.js 查看文件

constructor(id) { constructor(id) {
this.id = id; this.id = id;
this.conn = null; this.conn = null;
this.pongTimeout = null;
this.pongTime = 7500;
this.gotPong = false;
this.pongTime = 4000;
this.state = "idle";
this.q = []; this.q = [];


this.onstatechange = () => {} this.onstatechange = () => {}


this.createConn(); this.createConn();


setInterval(() => this.send({ type: "ping" }), this.pongTime);
this.gotPong();
setInterval(() => {
if (this.state == "pong-timeout")
return;

if (!this.gotPong)
this.setState("pong-timeout");

this.send({ type: "ping" });
this.gotPong = false;
}, this.pongTime);
}

setState(state, msg) {
this.state = state;
this.onstatechange(msg || state);
} }


createConn() { createConn() {
this.conn = new WebSocket(`${location.protocol.replace("http", "ws")}//${location.host}`); this.conn = new WebSocket(`${location.protocol.replace("http", "ws")}//${location.host}`);
this.onstatechange("Connecting");
this.setState("connecting");
this.connected = false; this.connected = false;
this.ready = false;


this.conn.onclose = evt => { this.conn.onclose = evt => {
this.conn = null; this.conn = null;
this.onstatechange(`Closed: ${evt.code}`);
this.setState("closed", `closed: ${evt.code}`);
setTimeout(this.createConn.bind(this), 2000); setTimeout(this.createConn.bind(this), 2000);
} }


this.conn.onopen = () => { this.conn.onopen = () => {
this.onstatechange("Identifying");
this.setState("identifying");
this.conn.send(JSON.stringify({ type: "identify-master", id: this.id })); this.conn.send(JSON.stringify({ type: "identify-master", id: this.id }));


this.q.forEach(el => this.conn.send(el)); this.q.forEach(el => this.conn.send(el));
case "identify-response": case "identify-response":
if (obj.err == null) { if (obj.err == null) {
this.connected = true; this.connected = true;
this.onstatechange("Waiting for target");
this.setState("waiting-for-target");
} else { } else {
this.onstatechange(`Error: ${obj.err}`);
this.setState("error", `error: ${obj.err}`);
} }
break; break;


case "target-connect": case "target-connect":
this.ready = true;
this.onstatechange("Ready");
this.setState("ready");
break; break;


case "target-disconnect": case "target-disconnect":
this.ready = false;
this.onstatechange("Waiting for target");
this.setState("waiting-for-target")
break; break;


case "js-result": case "js-result":
break; break;


case "pong": case "pong":
this.gotPong();
this.onPong();
break; break;


default: default:
} }
} }


gotPong() {
this.ready = true;
this.onstatechange("Ready");
if (this.pongTimeout)
clearTimeout(this.pongTimeout);

this.pongTimeout = setTimeout(() => {
this.ready = false;
this.onstatechange("Pong timeout");
}, this.pongTime / 2);
onPong() {
this.gotPong = true;
if (this.state == "pong-timeout")
this.setState("ready");
} }


runJavascript(js) { runJavascript(js) {

+ 1
- 1
web/console/index.html 查看文件

<link rel="stylesheet" href="style.css"> <link rel="stylesheet" href="style.css">
</head> </head>
<body> <body>
<div>State: <span id="state">Idle</span></div>
<div>State: <span id="state">idle</span></div>
<br> <br>
<div>Insert this into your target site: <pre id="target-script"></pre></div> <div>Insert this into your target site: <pre id="target-script"></pre></div>



正在加载...
取消
保存