Public
Edited
Jan 25, 2024
1 fork
Insert cell
Insert cell
id1 = hasUrlParams ? (new URLSearchParams(location.search)).get("id1") : null;
Insert cell
id2 = hasUrlParams ? (new URLSearchParams(location.search)).get("id2") : null;
Insert cell
role = hasUrlParams ? (new URLSearchParams(location.search)).get("role") : null;
Insert cell
peerjs = import("https://esm.sh/peerjs@1.5.2");
Insert cell
{
if(role === "send") {
const peer = new peerjs.Peer(null, { debug: 2 });
peer.on('open', function (id) {
// Workaround for peer.reconnect deleting previous id
if (peer.id === null) {
console.log('Received null id from peer open');
peer.id = lastPeerId;
} else {
lastPeerId = peer.id;
}

console.log('ID: ' + peer.id);
});
peer.on('connection', function (c) {
// Disallow incoming connections
c.on('open', function() {
c.send("Sender does not accept incoming connections");
setTimeout(function() { c.close(); }, 500);
});
});
peer.on('disconnected', function () {
status.innerHTML = "Connection lost. Please reconnect";
console.log('Connection lost. Please reconnect');

// Workaround for peer.reconnect deleting previous id
peer.id = lastPeerId;
peer._lastServerId = lastPeerId;
peer.reconnect();
});
peer.on('close', function() {
conn = null;
status.innerHTML = "Connection destroyed. Please refresh";
console.log('Connection destroyed');
});
peer.on('error', function (err) {
console.log(err);
alert('' + err);
});
} else {
const peer = new peerjs.Peer(null, { debug: 2 });

peer.on('open', function (id) {
// Workaround for peer.reconnect deleting previous id
if (peer.id === null) {
console.log('Received null id from peer open');
peer.id = lastPeerId;
} else {
lastPeerId = peer.id;
}

console.log('ID: ' + peer.id);
recvId.innerHTML = "ID: " + peer.id;
status.innerHTML = "Awaiting connection...";
});
peer.on('connection', function (c) {
// Allow only a single connection
if (conn && conn.open) {
c.on('open', function() {
c.send("Already connected to another client");
setTimeout(function() { c.close(); }, 500);
});
return;
}

conn = c;
console.log("Connected to: " + conn.peer);
status.innerHTML = "Connected";
ready();
});
peer.on('disconnected', function () {
status.innerHTML = "Connection lost. Please reconnect";
console.log('Connection lost. Please reconnect');

// Workaround for peer.reconnect deleting previous id
peer.id = lastPeerId;
peer._lastServerId = lastPeerId;
peer.reconnect();
});
peer.on('close', function() {
conn = null;
status.innerHTML = "Connection destroyed. Please refresh";
console.log('Connection destroyed');
});
peer.on('error', function (err) {
console.log(err);
alert('' + err);
});
}
}
Insert cell
hasUrlParams = {
checkURLParams(1000);
let x = new URLSearchParams(location.search);
return x.has("prev") && x.has("next");
}
Insert cell
import { checkURLParams } from '@mdeagen/wait-for-url-params'
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more