Skip to content
This repository was archived by the owner on Apr 29, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added examples/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions examples/mapper/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bundle.js
23 changes: 23 additions & 0 deletions examples/mapper/package.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
14 changes: 14 additions & 0 deletions examples/mapper/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>p2p mapper</title>
</head>
<body>
<h1>p2p mapper</h1>
<div id="my-peer"></div>
<div id="swarm"></div>

<script src="bundle.js"></script>
</body>
</html>
41 changes: 41 additions & 0 deletions examples/mapper/src/create-node.js
Original file line number Diff line number Diff line change
@@ -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
58 changes: 58 additions & 0 deletions examples/mapper/src/index.js
Original file line number Diff line number Diff line change
@@ -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) => {})
*/
})
})
})
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down