From 1f911779db4b570b615c465d9e95a0a98694bfaa Mon Sep 17 00:00:00 2001 From: Justin Gitlin Date: Wed, 31 Jan 2024 00:48:37 -0500 Subject: [PATCH] URL-encode ampersands from webrtc-peer.js, and cleanup disconnected clients --- demo/demo--base.js | 1 + demo/demo-webrtc-group-game.js | 9 +++++++++ src/webrtc-peer.js | 22 ++++++++++++---------- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/demo/demo--base.js b/demo/demo--base.js index 3ff0503..ed16e2e 100644 --- a/demo/demo--base.js +++ b/demo/demo--base.js @@ -22,6 +22,7 @@ class DemoBase { } static getDemoId() { + document.location.hash = document.location.hash.replace("%26", "&"); // replace url-encoded ampersand let id = document.location.hash.indexOf("&") == -1 ? document.location.hash diff --git a/demo/demo-webrtc-group-game.js b/demo/demo-webrtc-group-game.js index 62acdbe..3b7971b 100644 --- a/demo/demo-webrtc-group-game.js +++ b/demo/demo-webrtc-group-game.js @@ -173,6 +173,15 @@ class WebRtcGroupGameDemo extends DemoBase { `; }); + this.kiosk.addListener("peerConnected", (data) => { + console.log("peerConnected", data); + }); + this.kiosk.addListener("clientConnected", (data) => { + console.log("clientConnected", data); + }); + this.kiosk.addListener("peerClose", (data) => { + console.log("peerClose", data); + }); } buildClientUI() { diff --git a/src/webrtc-peer.js b/src/webrtc-peer.js index 3889edf..75662ac 100644 --- a/src/webrtc-peer.js +++ b/src/webrtc-peer.js @@ -343,7 +343,8 @@ class WebRtcKiosk extends WebRtcPeer { // build URL for client connection let url = window.location.href; if (replaceFunction) url = replaceFunction(url); - let separator = url.indexOf("#") == -1 ? "#" : "&"; + let separator = url.indexOf("#") == -1 ? "#" : "%26"; + urlParams = urlParams.replace("&", "%26"); let connectionURL = `${url}${separator}offer=${this.peerID}${urlParams}`; // add link container if it doesn't exist @@ -381,7 +382,9 @@ class WebRtcKiosk extends WebRtcPeer { connectionIsGood(conn) { return ( - conn.open && Date.now() - conn.connectTime < this.maxClientConnectionTime + conn.open && + conn.peerConnection.connectionState == "connected" && + Date.now() - conn.connectTime < this.maxClientConnectionTime ); } @@ -394,19 +397,18 @@ class WebRtcKiosk extends WebRtcPeer { // remove any connections that have been closed let removedAny = false; this.connections.forEach((conn, i) => { - // if (this.connectionIsGood(conn) == false) { - // this.removeConnectionListeners(conn); - // if (conn.call) this.removeCallListeners(conn.call); - // conn.close(); - // removedAny = true; - // } + if (this.connectionIsGood(conn) == false) { + this.removeConnectionListeners(conn); + if (conn.call) this.removeCallListeners(conn.call); + conn.close(); + removedAny = true; + } }); + console.log(removedAny); // filter old connections from array - // if (removedAny) { this.connections = this.connections.filter((conn) => { return this.connectionIsGood(conn); }); - // } this.emit("connections", this.connections); }