Skip to content

Commit

Permalink
URL-encode ampersands from webrtc-peer.js, and cleanup disconnected c…
Browse files Browse the repository at this point in the history
…lients
  • Loading branch information
cacheflowe committed Jan 31, 2024
1 parent 7f453ee commit 1f91177
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions demo/demo--base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions demo/demo-webrtc-group-game.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ class WebRtcGroupGameDemo extends DemoBase {
</div>
`;
});
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() {
Expand Down
22 changes: 12 additions & 10 deletions src/webrtc-peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
);
}

Expand All @@ -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);
}

Expand Down

0 comments on commit 1f91177

Please sign in to comment.