| @@ -2,6 +2,8 @@ class Conn { | |||
| constructor(id) { | |||
| this.id = id; | |||
| this.conn = null; | |||
| this.pongTimeout = null; | |||
| this.pongTime = 7500; | |||
| this.q = []; | |||
| this.onstatechange = () => {} | |||
| @@ -9,7 +11,8 @@ class Conn { | |||
| this.createConn(); | |||
| setInterval(() => this.send({ type: "ping" }), 30000); | |||
| setInterval(() => this.send({ type: "ping" }), this.pongTime); | |||
| this.gotPong(); | |||
| } | |||
| createConn() { | |||
| @@ -64,6 +67,7 @@ class Conn { | |||
| break; | |||
| case "pong": | |||
| this.gotPong(); | |||
| break; | |||
| default: | |||
| @@ -72,6 +76,18 @@ 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); | |||
| } | |||
| runJavascript(js) { | |||
| this.send({ type: "run-js", js }); | |||
| } | |||