| constructor(id) { | constructor(id) { | ||||
| this.id = id; | this.id = id; | ||||
| this.conn = null; | this.conn = null; | ||||
| this.pongTimeout = null; | |||||
| this.pongTime = 7500; | |||||
| this.q = []; | this.q = []; | ||||
| this.onstatechange = () => {} | this.onstatechange = () => {} | ||||
| this.createConn(); | this.createConn(); | ||||
| setInterval(() => this.send({ type: "ping" }), 30000); | |||||
| setInterval(() => this.send({ type: "ping" }), this.pongTime); | |||||
| this.gotPong(); | |||||
| } | } | ||||
| createConn() { | createConn() { | ||||
| break; | break; | ||||
| case "pong": | case "pong": | ||||
| this.gotPong(); | |||||
| 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); | |||||
| } | |||||
| runJavascript(js) { | runJavascript(js) { | ||||
| this.send({ type: "run-js", js }); | this.send({ type: "run-js", js }); | ||||
| } | } |