@yz-social/kdht 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/spec/node.html +18 -13
- package/transports/webrtc.js +1 -0
package/package.json
CHANGED
package/spec/node.html
CHANGED
|
@@ -19,29 +19,34 @@
|
|
|
19
19
|
import { v4 as uuidv4 } from 'uuid';
|
|
20
20
|
import { WebContact } from '@yz-social/kdht';
|
|
21
21
|
Object.assign(globalThis, {uuidv4, WebContact });
|
|
22
|
+
const bootstrapBase = new URL('/kdht', origin).href; // Must be a string
|
|
23
|
+
let contact;
|
|
22
24
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
async function connect() { // Connect or reconnect to the network.
|
|
26
|
+
const name = uuidv4();
|
|
27
|
+
contact = globalThis.contact = await WebContact.create({name});
|
|
28
|
+
const bootstrapName = await contact.fetchBootstrap(bootstrapBase);
|
|
29
|
+
console.log('connecting to', bootstrapName);
|
|
30
|
+
const bootstrapContact = globalThis.bootstrapContact = await contact.ensureRemoteContact(bootstrapName, bootstrapBase);
|
|
31
|
+
await contact.join(bootstrapContact);
|
|
32
|
+
update.onclick();
|
|
27
33
|
}
|
|
28
|
-
const contact = globalThis.contact = await WebContact.create({name});
|
|
29
|
-
const bootstrapBase = new URL('/kdht', origin).href; // Must be a string
|
|
30
|
-
const bootstrapName = await contact.fetchBootstrap(bootstrapBase);
|
|
31
|
-
console.log('connecting to', bootstrapName);
|
|
32
|
-
const bootstrapContact = globalThis.bootstrapContact = await contact.ensureRemoteContact(bootstrapName, bootstrapBase);
|
|
33
|
-
await contact.join(bootstrapContact);
|
|
34
|
-
console.log('connected');
|
|
35
34
|
|
|
36
35
|
update.onclick = () => {
|
|
37
36
|
const report = contact.node.report(null);
|
|
38
|
-
console.log({report, display});
|
|
39
37
|
display.textContent = report;
|
|
40
38
|
};
|
|
41
|
-
|
|
39
|
+
await connect();
|
|
42
40
|
|
|
43
41
|
write.onclick = () => contact.storeValue(key.value, writeValue.value);
|
|
44
42
|
read.onclick = () => contact.node.locateValue(key.value).then(value => readValue.value = value);
|
|
43
|
+
document.addEventListener("visibilitychange", () => {
|
|
44
|
+
if (document.hidden) {
|
|
45
|
+
contact.disconnect();
|
|
46
|
+
} else {
|
|
47
|
+
connect();
|
|
48
|
+
}
|
|
49
|
+
});
|
|
45
50
|
</script>
|
|
46
51
|
</body>
|
|
47
52
|
</html>
|
package/transports/webrtc.js
CHANGED
|
@@ -209,6 +209,7 @@ export class WebContact extends Contact { // Our wrapper for the means of contac
|
|
|
209
209
|
// If we find that in our inFlight tags, then the message is a response.
|
|
210
210
|
if (dataString === '"bye"') { // Special messsage that the other side is disconnecting, so we can clean up early.
|
|
211
211
|
this.webrtc.close();
|
|
212
|
+
this.host.xlog('removing disconnected contact');
|
|
212
213
|
await this.host.removeContact(this); // TODO: Make sure we're not invoking this in maxTransports cases.
|
|
213
214
|
return;
|
|
214
215
|
}
|