diff --git a/examples/.DS_Store b/examples/.DS_Store new file mode 100644 index 0000000..dece892 Binary files /dev/null and b/examples/.DS_Store differ diff --git a/examples/mapper/.gitignore b/examples/mapper/.gitignore new file mode 100644 index 0000000..4187d67 --- /dev/null +++ b/examples/mapper/.gitignore @@ -0,0 +1 @@ +bundle.js \ No newline at end of file diff --git a/examples/mapper/package.json b/examples/mapper/package.json new file mode 100644 index 0000000..b9c2724 --- /dev/null +++ b/examples/mapper/package.json @@ -0,0 +1,23 @@ +{ + "name": "mapper", + "version": "0.0.0", + "description": "", + "main": "src/index.js", + "scripts": { + "bundle": "browserify src/index.js --require browserify-zlib-next:zlib > public/bundle.js", + "serve": "static public -p 9090 -H '{\"Cache-Control\": \"no-cache, must-revalidate\"}'", + "mon": "nodemon --exec \"npm run start\" --ignore public/bundle.js", + "start": "npm run bundle && npm run serve" + }, + "license": "MIT", + "devDependencies": { + "browserify": "^14.0.0", + "browserify-optional": "^1.0.0", + "browserify-zlib-next": "^1.0.1", + "concat-stream": "^1.6.0", + "detect-dom-ready": "^1.0.2", + "node-static": "^0.7.9", + "nodemon": "^1.11.0" + }, + "dependencies": {} +} diff --git a/examples/mapper/public/index.html b/examples/mapper/public/index.html new file mode 100644 index 0000000..7d77a39 --- /dev/null +++ b/examples/mapper/public/index.html @@ -0,0 +1,14 @@ + + + + + p2p mapper + + +

p2p mapper

+
+
+ + + + diff --git a/examples/mapper/src/create-node.js b/examples/mapper/src/create-node.js new file mode 100644 index 0000000..f318876 --- /dev/null +++ b/examples/mapper/src/create-node.js @@ -0,0 +1,41 @@ +'use strict' + +const PeerInfo = require('peer-info') +const Node = require('../../../src') +const multiaddr = require('multiaddr') + +function createNode (callback) { + PeerInfo.create((err, peerInfo) => { + if (err) { + return callback(err) + } + + const peerIdStr = peerInfo.id.toB58String() + const ma = `/libp2p-webrtc-star/dns4/star-signal.cloud.ipfs.team/wss/ipfs/${peerIdStr}` + + peerInfo.multiaddr.add(multiaddr(ma)) + + const node = new Node(peerInfo, undefined, { webRTCStar: true }) + + node.idStr = peerIdStr + callback(null, node) + + /* + node.discovery.on('peer', (peerInfo) => { + console.log('Discovered peer', peerInfo.id.toB58String()) + }) + + node.start((err) => { + if (err) { + return console.log('WebRTC not supported') + } + + console.log('Node is listening') + + // node.stop((err) => {}) + }) + */ + }) +} + +module.exports = createNode diff --git a/examples/mapper/src/index.js b/examples/mapper/src/index.js new file mode 100644 index 0000000..41d5555 --- /dev/null +++ b/examples/mapper/src/index.js @@ -0,0 +1,58 @@ +'use strict' + +const domReady = require('detect-dom-ready') +const createNode = require('./create-node') + +domReady(() => { + const myPeerDiv = document.getElementById('my-peer') + const swarmDiv = document.getElementById('swarm') + + createNode((err, node) => { + if (err) { + return console.log('Could not create the Node, check if your browser has WebRTC Support', err) + } + + node.discovery.on('peer', (peerInfo) => { + const idStr = peerInfo.id.toB58String() + console.log('Discovered: ' + idStr) + + node.dialByPeerInfo(peerInfo, (err, conn) => { + if (err) { return console.log('Failed to dial:', idStr) } + }) + }) + + node.swarm.on('peer-mux-established', (peerInfo) => { + const idStr = peerInfo.id.toB58String() + console.log('Got connection to: ' + idStr) + const connDiv = document.createElement('div') + connDiv.innerHTML = 'Connected to: ' + idStr + connDiv.id = idStr + swarmDiv.append(connDiv) + }) + + node.swarm.on('peer-mux-closed', (peerInfo) => { + const idStr = peerInfo.id.toB58String() + console.log('Lost connection to: ' + idStr) + document.getElementById(idStr).remove() + }) + + node.start((err) => { + if (err) { + return console.log('WebRTC not supported') + } + + const idStr = node.peerInfo.id.toB58String() + + const idDiv = document + .createTextNode('Node is ready. ID: ' + idStr) + + myPeerDiv.append(idDiv) + + console.log('Node is listening o/') + + /* to stop the node + * node.stop((err) => {}) + */ + }) + }) +}) diff --git a/package.json b/package.json index e1e4be8..3394b34 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "async": "^2.1.4", "chai": "^3.5.0", "gulp": "^3.9.1", - "libp2p-ipfs-nodejs": "~0.18.0", + "libp2p-ipfs-nodejs": "~0.18.1", "peer-id": "~0.8.2", "pre-commit": "^1.2.2", "pull-goodbye": "0.0.1", @@ -43,12 +43,12 @@ "webrtcsupport": "^2.2.0" }, "dependencies": { - "libp2p": "~0.5.3", + "libp2p": "~0.5.4", "libp2p-railing": "~0.4.1", "libp2p-secio": "~0.6.7", "libp2p-spdy": "~0.10.4", - "libp2p-swarm": "~0.26.16", - "libp2p-webrtc-star": "~0.8.6", + "libp2p-swarm": "~0.26.17", + "libp2p-webrtc-star": "~0.8.7", "libp2p-websockets": "~0.9.2", "mafmt": "^2.1.6", "multiaddr": "^2.2.1",