Skip to content

Commit 112c91e

Browse files
committed
Periodic broadcast
Signed-off-by: Robert Gogete <gogeterobert@yahoo.com>
1 parent 2d10ced commit 112c91e

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/host.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class FileHost implements IFileHost {
4242
private upnpClient: ReturnType<typeof natUpnp.createClient> | null = null;
4343
private upnpMapping: { external: number; internal: number } | null = null;
4444
private publicIp: string | null = null;
45+
private registrationInterval: ReturnType<typeof setInterval> | null = null;
4546

4647
constructor(options: HostOptions = {}) {
4748
this.options = options;
@@ -325,6 +326,9 @@ export class FileHost implements IFileHost {
325326
console.log(`🔄 Registering with Gun.js registry...`);
326327
await this.gunRegistry.register(capabilities);
327328
console.log(`✅ Registered capabilities in Gun.js registry with storeId: ${this.storeId}`);
329+
330+
// Start periodic registration to keep data fresh
331+
this.startPeriodicRegistration();
328332
} catch (error) {
329333
console.warn(`⚠️ Failed to register in Gun.js registry:`, error);
330334
}
@@ -336,6 +340,45 @@ export class FileHost implements IFileHost {
336340
return capabilities;
337341
}
338342

343+
/**
344+
* Start periodic registration to keep data fresh in Gun.js registry
345+
*/
346+
private startPeriodicRegistration(): void {
347+
if (!this.gunRegistry || !this.capabilities) {
348+
return;
349+
}
350+
351+
console.log('🔄 Starting periodic registration (every 5 seconds)...');
352+
353+
this.registrationInterval = setInterval(async () => {
354+
try {
355+
// Update the lastSeen timestamp
356+
const updatedCapabilities = {
357+
...this.capabilities!,
358+
lastSeen: Date.now()
359+
};
360+
361+
await this.gunRegistry!.register(updatedCapabilities);
362+
console.log(`🔄 Re-registered capabilities for ${this.storeId}`);
363+
} catch (error) {
364+
console.warn(`⚠️ Failed to re-register capabilities:`, error);
365+
}
366+
}, 5000); // Re-register every 5 seconds
367+
368+
console.log('✅ Periodic registration started');
369+
}
370+
371+
/**
372+
* Stop periodic registration
373+
*/
374+
private stopPeriodicRegistration(): void {
375+
if (this.registrationInterval) {
376+
clearInterval(this.registrationInterval);
377+
this.registrationInterval = null;
378+
console.log('✅ Periodic registration stopped');
379+
}
380+
}
381+
339382
/**
340383
* Wait for WebTorrent client to be ready
341384
*/
@@ -395,7 +438,10 @@ export class FileHost implements IFileHost {
395438
public async stop(): Promise<void> {
396439
console.log('🛑 Stopping FileHost...');
397440

398-
// Remove UPnP port mapping first
441+
// Stop periodic registration first
442+
this.stopPeriodicRegistration();
443+
444+
// Remove UPnP port mapping
399445
await this.removeUpnpPortMapping();
400446

401447
// Stop WebTorrent client

0 commit comments

Comments
 (0)