docs: update examples for latest libp2p rc (#353)

* docs: update chat example readme

* docs: update discovery test for autodial

* docs: fix delegated routing example

* docs: update echo example readme

* docs: fix libp2p in the browser example

* docs: update examples for peer/content routing

* docs: update the pubsub example
This commit is contained in:
Jacob Heun
2019-04-11 15:52:04 +02:00
committed by GitHub
parent aa1d9b273a
commit 4cb541ddae
16 changed files with 64 additions and 86 deletions

View File

@ -7,6 +7,7 @@ const Mplex = require('libp2p-mplex')
const SPDY = require('libp2p-spdy')
const SECIO = require('libp2p-secio')
const Bootstrap = require('libp2p-bootstrap')
const DHT = require('libp2p-kad-dht')
const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../../../../')
@ -47,10 +48,12 @@ class Node extends libp2p {
wrtcStar.discovery,
wsstar.discovery,
Bootstrap
]
],
dht: DHT
},
config: {
peerDiscovery: {
autoDial: true,
webRTCStar: {
enabled: true
},
@ -58,7 +61,7 @@ class Node extends libp2p {
enabled: true
},
bootstrap: {
interval: 10000,
interval: 20e3,
enabled: true,
list: bootstrapList
}
@ -66,16 +69,19 @@ class Node extends libp2p {
relay: {
enabled: true,
hop: {
enabled: true,
enabled: false,
active: false
}
},
dht: {
enabled: false
},
EXPERIMENTAL: {
dht: false,
pubsub: false
}
},
connectionManager: {
minPeers: 10,
maxPeers: 50
}
}

View File

@ -14,26 +14,8 @@ domReady(() => {
return console.log('Could not create the Node, check if your browser has WebRTC Support', err)
}
let connections = {}
node.on('peer:discovery', (peerInfo) => {
const idStr = peerInfo.id.toB58String()
if (connections[idStr]) {
// If we're already trying to connect to this peer, dont dial again
return
}
console.log('Discovered a peer:', idStr)
connections[idStr] = true
node.dial(peerInfo, (err, conn) => {
if (err) {
// Prevent immediate connection retries from happening
// and include a 10s jitter
const timeToNextDial = 25 * 1000 + (Math.random(0) * 10000).toFixed(0)
console.log('Failed to dial:', idStr)
setTimeout(() => delete connections[idStr], timeToNextDial)
}
})
console.log('Discovered a peer:', peerInfo.id.toB58String())
})
node.on('peer:connect', (peerInfo) => {
@ -47,15 +29,13 @@ domReady(() => {
node.on('peer:disconnect', (peerInfo) => {
const idStr = peerInfo.id.toB58String()
delete connections[idStr]
console.log('Lost connection to: ' + idStr)
const el = document.getElementById(idStr)
el && el.remove()
})
node.start((err) => {
if (err) {
return console.log('WebRTC not supported')
return console.log(err)
}
const idStr = node.peerInfo.id.toB58String()