|
|
@@ -2,8 +2,9 @@ class Conn { |
|
|
|
constructor(id) { |
|
|
|
this.id = id; |
|
|
|
this.conn = null; |
|
|
|
this.pongTimeout = null; |
|
|
|
this.pongTime = 7500; |
|
|
|
this.gotPong = false; |
|
|
|
this.pongTime = 4000; |
|
|
|
this.state = "idle"; |
|
|
|
this.q = []; |
|
|
|
|
|
|
|
this.onstatechange = () => {} |
|
|
@@ -11,24 +12,36 @@ class Conn { |
|
|
|
|
|
|
|
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() { |
|
|
|
this.conn = new WebSocket(`${location.protocol.replace("http", "ws")}//${location.host}`); |
|
|
|
this.onstatechange("Connecting"); |
|
|
|
this.setState("connecting"); |
|
|
|
this.connected = false; |
|
|
|
this.ready = false; |
|
|
|
|
|
|
|
this.conn.onclose = evt => { |
|
|
|
this.conn = null; |
|
|
|
this.onstatechange(`Closed: ${evt.code}`); |
|
|
|
this.setState("closed", `closed: ${evt.code}`); |
|
|
|
setTimeout(this.createConn.bind(this), 2000); |
|
|
|
} |
|
|
|
|
|
|
|
this.conn.onopen = () => { |
|
|
|
this.onstatechange("Identifying"); |
|
|
|
this.setState("identifying"); |
|
|
|
this.conn.send(JSON.stringify({ type: "identify-master", id: this.id })); |
|
|
|
|
|
|
|
this.q.forEach(el => this.conn.send(el)); |
|
|
@@ -42,20 +55,18 @@ class Conn { |
|
|
|
case "identify-response": |
|
|
|
if (obj.err == null) { |
|
|
|
this.connected = true; |
|
|
|
this.onstatechange("Waiting for target"); |
|
|
|
this.setState("waiting-for-target"); |
|
|
|
} else { |
|
|
|
this.onstatechange(`Error: ${obj.err}`); |
|
|
|
this.setState("error", `error: ${obj.err}`); |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
case "target-connect": |
|
|
|
this.ready = true; |
|
|
|
this.onstatechange("Ready"); |
|
|
|
this.setState("ready"); |
|
|
|
break; |
|
|
|
|
|
|
|
case "target-disconnect": |
|
|
|
this.ready = false; |
|
|
|
this.onstatechange("Waiting for target"); |
|
|
|
this.setState("waiting-for-target") |
|
|
|
break; |
|
|
|
|
|
|
|
case "js-result": |
|
|
@@ -67,7 +78,7 @@ class Conn { |
|
|
|
break; |
|
|
|
|
|
|
|
case "pong": |
|
|
|
this.gotPong(); |
|
|
|
this.onPong(); |
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
@@ -76,16 +87,10 @@ class Conn { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
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) { |