Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: adding relay #59

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"dependencies": {
"libp2p-ping": "~0.3.1",
"libp2p-swarm": "~0.26.17",
"libp2p-circuit": "~0.0.1",
"mafmt": "^2.1.6",
"multiaddr": "^2.2.1",
"peer-book": "~0.3.1",
Expand All @@ -54,4 +55,4 @@
"greenkeeperio-bot <support@greenkeeper.io>",
"mayerwin <mayerwin@users.noreply.github.com>"
]
}
}
16 changes: 12 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const EE = require('events').EventEmitter
const assert = require('assert')
const Ping = require('libp2p-ping')
const setImmediate = require('async/setImmediate')
const series = require('async/series')

exports = module.exports

Expand Down Expand Up @@ -41,6 +42,9 @@ class Node {
// If muxer exists, we can use Identify
this.swarm.connection.reuse()

// If muxer exists, we can use Relay
this.swarm.connection.relay()

// Received incommind dial and muxer upgrade happened, reuse this
// muxed connection
this.swarm.on('peer-mux-established', (peerInfo) => {
Expand Down Expand Up @@ -100,7 +104,7 @@ class Node {
this.swarm.transport.add(
transport.tag || transport.constructor.name, transport)
} else if (transport.constructor &&
transport.constructor.name === 'WebSockets') {
transport.constructor.name === 'WebSockets') {
// TODO find a cleaner way to signal that a transport is always
// used for dialing, even if no listener
ws = transport
Expand All @@ -119,7 +123,8 @@ class Node {

if (this.modules.discovery) {
this.modules.discovery.forEach((discovery) => {
setImmediate(() => discovery.start(() => {}))
setImmediate(() => discovery.start(() => {
}))
})
}

Expand All @@ -135,11 +140,14 @@ class Node {

if (this.modules.discovery) {
this.modules.discovery.forEach((discovery) => {
setImmediate(() => discovery.stop(() => {}))
setImmediate(() => discovery.stop(() => {
}))
})
}

this.swarm.close(callback)
series([
(cb) => this.swarm.close(cb)
], callback)
}

//
Expand Down