File tree 3 files changed +31
-8
lines changed
examples/libp2p-in-the-browser/1
3 files changed +31
-8
lines changed Original file line number Diff line number Diff line change 17
17
},
18
18
"dependencies" : {
19
19
"detect-dom-ready" : " ^1.0.2" ,
20
+ "libp2p-bootstrap" : " ~0.9.3" ,
20
21
"libp2p-mplex" : " ~0.8.0" ,
21
- "libp2p-railing" : " ~0.9.1" ,
22
22
"libp2p-secio" : " ~0.10.0" ,
23
23
"libp2p-spdy" : " ~0.12.1" ,
24
24
"libp2p-webrtc-star" : " ~0.15.3" ,
25
+ "libp2p-websocket-star" : " ~0.8.1" ,
25
26
"libp2p-websockets" : " ~0.12.0" ,
26
27
"peer-info" : " ~0.14.1"
27
28
}
Original file line number Diff line number Diff line change 2
2
3
3
const WebRTCStar = require ( 'libp2p-webrtc-star' )
4
4
const WebSockets = require ( 'libp2p-websockets' )
5
+ const WebSocketStar = require ( 'libp2p-websocket-star' )
5
6
const Mplex = require ( 'libp2p-mplex' )
6
7
const SPDY = require ( 'libp2p-spdy' )
7
8
const SECIO = require ( 'libp2p-secio' )
8
- const Bootstrap = require ( 'libp2p-railing ' )
9
+ const Bootstrap = require ( 'libp2p-bootstrap ' )
9
10
const defaultsDeep = require ( '@nodeutils/defaults-deep' )
10
11
const libp2p = require ( '../../../../' )
11
12
@@ -26,12 +27,14 @@ const bootstrapers = [
26
27
class Node extends libp2p {
27
28
constructor ( _options ) {
28
29
const wrtcStar = new WebRTCStar ( { id : _options . peerInfo . id } )
30
+ const wsstar = new WebSocketStar ( { id : _options . peerInfo . id } )
29
31
30
32
const defaults = {
31
33
modules : {
32
34
transport : [
33
35
wrtcStar ,
34
- new WebSockets ( )
36
+ WebSockets ,
37
+ wsstar
35
38
] ,
36
39
streamMuxer : [
37
40
Mplex ,
@@ -42,6 +45,7 @@ class Node extends libp2p {
42
45
] ,
43
46
peerDiscovery : [
44
47
wrtcStar . discovery ,
48
+ wsstar . discovery ,
45
49
Bootstrap
46
50
]
47
51
} ,
@@ -55,21 +59,24 @@ class Node extends libp2p {
55
59
} ,
56
60
bootstrap : {
57
61
interval : 10000 ,
58
- enabled : false ,
62
+ enabled : true ,
59
63
list : bootstrapers
60
64
}
61
65
} ,
62
66
relay : {
63
- enabled : false ,
67
+ enabled : true ,
64
68
hop : {
65
- enabled : false ,
69
+ enabled : true ,
66
70
active : false
67
71
}
68
72
} ,
69
73
EXPERIMENTAL : {
70
74
dht : false ,
71
75
pubsub : false
72
76
}
77
+ } ,
78
+ connectionManager : {
79
+ maxPeers : 50
73
80
}
74
81
}
75
82
Original file line number Diff line number Diff line change 1
- /* eslint-disable no-console */
1
+ /* eslint no-console: ["error", { allow: ["log"] }] */
2
+ /* eslint max-nested-callbacks: ["error", 5] */
2
3
'use strict'
3
4
4
5
const domReady = require ( 'detect-dom-ready' )
@@ -13,13 +14,27 @@ domReady(() => {
13
14
return console . log ( 'Could not create the Node, check if your browser has WebRTC Support' , err )
14
15
}
15
16
17
+ let connections = { }
18
+
16
19
node . on ( 'peer:discovery' , ( peerInfo ) => {
17
20
console . log ( 'Discovered a peer' )
18
21
const idStr = peerInfo . id . toB58String ( )
19
22
console . log ( 'Discovered: ' + idStr )
23
+ if ( connections [ idStr ] ) {
24
+ // If we're already trying to connect to this peer, dont dial again
25
+ return
26
+ }
20
27
28
+ connections [ idStr ] = true
21
29
node . dial ( peerInfo , ( err , conn ) => {
22
- if ( err ) { return console . log ( 'Failed to dial:' , idStr ) }
30
+ let timeToNextDial = 0
31
+ if ( err ) {
32
+ // Prevent immediate connection retries from happening
33
+ timeToNextDial = 30 * 1000
34
+ console . log ( 'Failed to dial:' , idStr )
35
+ }
36
+
37
+ setTimeout ( ( ) => delete connections [ idStr ] , timeToNextDial )
23
38
} )
24
39
} )
25
40
You can’t perform that action at this time.
0 commit comments