From aab15ab218ff975ba263245b92719352fdf470cd Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Mon, 20 Aug 2018 16:58:54 +0200 Subject: [PATCH] docs: improve browser example connectability --- examples/libp2p-in-the-browser/1/package.json | 3 ++- .../libp2p-in-the-browser/1/public/index.html | 14 -------------- .../1/src/browser-bundle.js | 17 ++++++++++++----- examples/libp2p-in-the-browser/1/src/index.js | 19 ++++++++++++++++++- 4 files changed, 32 insertions(+), 21 deletions(-) delete mode 100644 examples/libp2p-in-the-browser/1/public/index.html diff --git a/examples/libp2p-in-the-browser/1/package.json b/examples/libp2p-in-the-browser/1/package.json index c4651ec5c8..c6d64a249d 100644 --- a/examples/libp2p-in-the-browser/1/package.json +++ b/examples/libp2p-in-the-browser/1/package.json @@ -17,11 +17,12 @@ }, "dependencies": { "detect-dom-ready": "^1.0.2", + "libp2p-bootstrap": "~0.9.3", "libp2p-mplex": "~0.8.0", - "libp2p-railing": "~0.9.1", "libp2p-secio": "~0.10.0", "libp2p-spdy": "~0.12.1", "libp2p-webrtc-star": "~0.15.3", + "libp2p-websocket-star": "~0.8.1", "libp2p-websockets": "~0.12.0", "peer-info": "~0.14.1" } diff --git a/examples/libp2p-in-the-browser/1/public/index.html b/examples/libp2p-in-the-browser/1/public/index.html deleted file mode 100644 index c13adfe989..0000000000 --- a/examples/libp2p-in-the-browser/1/public/index.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - - libp2p in the browser - - -

libp2p node running \o/

-
-
- - - - diff --git a/examples/libp2p-in-the-browser/1/src/browser-bundle.js b/examples/libp2p-in-the-browser/1/src/browser-bundle.js index 80017c175b..c59474b26e 100644 --- a/examples/libp2p-in-the-browser/1/src/browser-bundle.js +++ b/examples/libp2p-in-the-browser/1/src/browser-bundle.js @@ -2,10 +2,11 @@ const WebRTCStar = require('libp2p-webrtc-star') const WebSockets = require('libp2p-websockets') +const WebSocketStar = require('libp2p-websocket-star') const Mplex = require('libp2p-mplex') const SPDY = require('libp2p-spdy') const SECIO = require('libp2p-secio') -const Bootstrap = require('libp2p-railing') +const Bootstrap = require('libp2p-bootstrap') const defaultsDeep = require('@nodeutils/defaults-deep') const libp2p = require('../../../../') @@ -26,12 +27,14 @@ const bootstrapers = [ class Node extends libp2p { constructor (_options) { const wrtcStar = new WebRTCStar({ id: _options.peerInfo.id }) + const wsstar = new WebSocketStar({ id: _options.peerInfo.id }) const defaults = { modules: { transport: [ wrtcStar, - new WebSockets() + WebSockets, + wsstar ], streamMuxer: [ Mplex, @@ -42,6 +45,7 @@ class Node extends libp2p { ], peerDiscovery: [ wrtcStar.discovery, + wsstar.discovery, Bootstrap ] }, @@ -55,14 +59,14 @@ class Node extends libp2p { }, bootstrap: { interval: 10000, - enabled: false, + enabled: true, list: bootstrapers } }, relay: { - enabled: false, + enabled: true, hop: { - enabled: false, + enabled: true, active: false } }, @@ -70,6 +74,9 @@ class Node extends libp2p { dht: false, pubsub: false } + }, + connectionManager: { + maxPeers: 50 } } diff --git a/examples/libp2p-in-the-browser/1/src/index.js b/examples/libp2p-in-the-browser/1/src/index.js index 7100454b8a..5bc618b3b8 100644 --- a/examples/libp2p-in-the-browser/1/src/index.js +++ b/examples/libp2p-in-the-browser/1/src/index.js @@ -1,3 +1,6 @@ +/* eslint no-console: ["error", { allow: ["log"] }] */ +/* eslint max-nested-callbacks: ["error", 5] */ + 'use strict' const domReady = require('detect-dom-ready') @@ -12,13 +15,27 @@ domReady(() => { return console.log('Could not create the Node, check if your browser has WebRTC Support', err) } + let connections = {} + node.on('peer:discovery', (peerInfo) => { console.log('Discovered a peer') const idStr = peerInfo.id.toB58String() console.log('Discovered: ' + idStr) + if (connections[idStr]) { + // If we're already trying to connect to this peer, dont dial again + return + } + connections[idStr] = true node.dial(peerInfo, (err, conn) => { - if (err) { return console.log('Failed to dial:', idStr) } + let timeToNextDial = 0 + if (err) { + // Prevent immediate connection retries from happening + timeToNextDial = 30 * 1000 + console.log('Failed to dial:', idStr) + } + + setTimeout(() => delete connections[idStr], timeToNextDial) }) })