2018-10-31 14:42:24 +01:00
|
|
|
/* eslint no-console: ["error", { allow: ["log"] }] */
|
|
|
|
/* eslint max-nested-callbacks: ["error", 5] */
|
2017-07-04 11:43:45 +01:00
|
|
|
'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.on('peer:discovery', (peerInfo) => {
|
2019-04-11 15:52:04 +02:00
|
|
|
console.log('Discovered a peer:', peerInfo.id.toB58String())
|
2017-07-04 11:43:45 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
node.on('peer:connect', (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.on('peer:disconnect', (peerInfo) => {
|
|
|
|
const idStr = peerInfo.id.toB58String()
|
2018-11-14 18:50:17 +01:00
|
|
|
const el = document.getElementById(idStr)
|
|
|
|
el && el.remove()
|
2017-07-04 11:43:45 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
node.start((err) => {
|
|
|
|
if (err) {
|
2019-04-11 15:52:04 +02:00
|
|
|
return console.log(err)
|
2017-07-04 11:43:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const idStr = node.peerInfo.id.toB58String()
|
|
|
|
|
|
|
|
const idDiv = document
|
|
|
|
.createTextNode('Node is ready. ID: ' + idStr)
|
|
|
|
|
|
|
|
myPeerDiv.append(idDiv)
|
|
|
|
|
|
|
|
console.log('Node is listening o/')
|
|
|
|
|
|
|
|
// NOTE: to stop the node
|
|
|
|
// node.stop((err) => {})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|