Skip to content

Commit 7fc88b1

Browse files
committed
updated examples to test gunjs
Signed-off-by: Robert Gogete <gogeterobert@yahoo.com>
1 parent 6f322b1 commit 7fc88b1

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

β€Žexamples/gunjs-client.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import https from 'https';
66
import http from 'http';
77

88
// Configuration
9-
const RELAY_URL = 'http://YOUR_SERVER_IP:30876/gun'; // Replace with your server IP
9+
const RELAY_URL = 'http://nostalgiagame.go.ro:30876/gun'; // Replace with your server IP
1010
const CLIENT_ID = `client-${Date.now()}`;
1111

1212
console.log('πŸ” Starting Gun.js Client');

β€Žexamples/gunjs-host.jsβ€Ž

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import path from 'path';
77

88
// Configuration
99
const HOST_PORT = 3000;
10-
const RELAY_URL = 'http://YOUR_SERVER_IP:30876/gun'; // Replace with your server IP
10+
const RELAY_URL = 'http://nostalgiagame.go.ro:30876/gun'; // Replace with your server IP
1111
const HOST_ID = `host-${Date.now()}`;
1212

1313
// Create Express server for file serving
@@ -33,14 +33,45 @@ const gun = Gun({
3333
radisk: false
3434
});
3535

36+
// Connection validation
37+
console.log(`πŸ”— Attempting to connect to relay: ${RELAY_URL}`);
38+
39+
// Listen for Gun.js connection events
40+
gun.on('hi', (peer) => {
41+
console.log(`βœ… Connected to peer: ${peer.url || peer.id || 'unknown'}`);
42+
if (peer.url === RELAY_URL || peer.url?.includes('30876')) {
43+
console.log('🎯 Successfully connected to relay server!');
44+
}
45+
});
46+
47+
gun.on('bye', (peer) => {
48+
console.log(`❌ Disconnected from peer: ${peer.url || peer.id || 'unknown'}`);
49+
});
50+
51+
// Test connection by writing a test value
52+
setTimeout(() => {
53+
console.log('πŸ§ͺ Testing relay connection...');
54+
55+
gun.get('connection-test').get(HOST_ID).put({
56+
test: 'connection-ok',
57+
timestamp: Date.now()
58+
}, (ack) => {
59+
if (ack.err) {
60+
console.log('❌ Failed to write to relay:', ack.err);
61+
} else {
62+
console.log('βœ… Successfully wrote to relay - connection confirmed!');
63+
}
64+
});
65+
}, 2000);
66+
3667
// Host data structure in Gun
3768
const hostNode = gun.get('hosts').get(HOST_ID);
3869

3970
// Register host information
4071
const hostInfo = {
4172
id: HOST_ID,
4273
name: 'Example File Host',
43-
fileServerUrl: `http://YOUR_SERVER_IP:${HOST_PORT}/files`, // Replace with your server IP
74+
fileServerUrl: `http://nostalgiagame.go.ro:${HOST_PORT}/files`, // Replace with your server IP
4475
timestamp: Date.now(),
4576
status: 'online'
4677
};

β€Žexamples/gunjs-relay.jsβ€Ž

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,33 @@ const gun = Gun({
1818

1919
// Log connections
2020
gun.on('hi', (peer) => {
21-
console.log(`🟒 Peer connected: ${peer.id}`);
21+
console.log(`🟒 Peer connected: ${peer.id || peer.url || 'unknown'}`);
22+
if (peer.id?.includes('host-') || peer.url?.includes('localhost')) {
23+
console.log('🏠 Host connected to relay!');
24+
} else if (peer.id?.includes('client-')) {
25+
console.log('πŸ‘€ Client connected to relay!');
26+
}
2227
});
2328

2429
gun.on('bye', (peer) => {
25-
console.log(`πŸ”΄ Peer disconnected: ${peer.id}`);
30+
console.log(`πŸ”΄ Peer disconnected: ${peer.id || peer.url || 'unknown'}`);
31+
});
32+
33+
// Monitor host registrations
34+
gun.get('hosts').on((data, key) => {
35+
if (key !== '_' && data && data.name) {
36+
console.log(`πŸ“ Host registered: ${data.name} (ID: ${data.id})`);
37+
console.log(` πŸ“‚ File server: ${data.fileServerUrl}`);
38+
console.log(` πŸ“Š Status: ${data.status}`);
39+
}
2640
});
2741

2842
// Start the server
29-
server.listen(PORT, () => {
43+
server.listen(PORT, '0.0.0.0', () => {
3044
console.log('πŸš€ Gun.js Relay Server Started');
31-
console.log(`πŸ“‘ Listening on port: ${PORT}`);
32-
console.log(`🌐 Gun endpoint: http://localhost:${PORT}/gun`);
33-
console.log(`πŸ”— Relay URL for peers: http://YOUR_SERVER_IP:${PORT}/gun`);
34-
console.log('πŸ’‘ Replace YOUR_SERVER_IP with your actual server IP address');
45+
console.log(`πŸ“‘ Listening on: 0.0.0.0:${PORT}`);
46+
console.log(`🌐 Gun endpoint: http://0.0.0.0:${PORT}/gun`);
47+
console.log(`πŸ”— Relay URL for peers: http://nostalgiagame.go.ro:${PORT}/gun`);
3548
console.log('⏳ Waiting for connections...\n');
3649
});
3750

0 commit comments

Comments
Β (0)