mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-05-02 05:52:14 +00:00
add new openConnection option
This commit is contained in:
parent
7c08e76cec
commit
8911d72648
@ -39,6 +39,15 @@ s.closeListener()
|
||||
s.registerHandler('/name/protocol/you/want/version', function (stream) {})
|
||||
```
|
||||
|
||||
### Open a new connection
|
||||
|
||||
Used when we want to make sure we can connect to a given peer, but do not intend to establish a stream with any of the services offered right away.
|
||||
|
||||
```
|
||||
s.openConnection(peerConnection, function (err) {})
|
||||
```
|
||||
|
||||
|
||||
### Dial a new stream
|
||||
|
||||
```
|
||||
|
15
src/swarm.js
15
src/swarm.js
@ -61,7 +61,7 @@ function Swarm () {
|
||||
// interface
|
||||
|
||||
// open stream account for connection reuse
|
||||
self.openStream = function (peer, protocol, cb) {
|
||||
self.openConnection = function (peer, cb) {
|
||||
// If no connection open yet, open it
|
||||
if (!self.connections[peer.id.toB58String()]) {
|
||||
// Establish a socket with one of the addresses
|
||||
@ -88,7 +88,7 @@ function Swarm () {
|
||||
gotSocket(socket)
|
||||
})
|
||||
} else {
|
||||
createStream(peer, protocol, cb)
|
||||
cb()
|
||||
}
|
||||
|
||||
// do the spdy people dance (multistream-select into spdy)
|
||||
@ -107,12 +107,17 @@ function Swarm () {
|
||||
conn.on('close', function () { delete self.connections[peer.id.toB58String()] })
|
||||
errorUp(self, conn)
|
||||
|
||||
createStream(peer, protocol, cb)
|
||||
cb()
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function createStream (peer, protocol, cb) {
|
||||
self.openStream = function (peer, protocol, cb) {
|
||||
self.openConnection(peer, function (err) {
|
||||
if (err) {
|
||||
return cb(err)
|
||||
}
|
||||
// spawn new muxed stream
|
||||
var conn = self.connections[peer.id.toB58String()].conn
|
||||
conn.dialStream(function (err, stream) {
|
||||
@ -127,7 +132,7 @@ function Swarm () {
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
self.registerHandler = function (protocol, handlerFunc) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user