add new openConnection option

This commit is contained in:
David Dias 2015-07-25 18:42:36 -07:00
parent 7c08e76cec
commit 8911d72648
2 changed files with 19 additions and 5 deletions

View File

@ -39,6 +39,15 @@ s.closeListener()
s.registerHandler('/name/protocol/you/want/version', function (stream) {}) 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 ### Dial a new stream
``` ```

View File

@ -61,7 +61,7 @@ function Swarm () {
// interface // interface
// open stream account for connection reuse // 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 no connection open yet, open it
if (!self.connections[peer.id.toB58String()]) { if (!self.connections[peer.id.toB58String()]) {
// Establish a socket with one of the addresses // Establish a socket with one of the addresses
@ -88,7 +88,7 @@ function Swarm () {
gotSocket(socket) gotSocket(socket)
}) })
} else { } else {
createStream(peer, protocol, cb) cb()
} }
// do the spdy people dance (multistream-select into spdy) // 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()] }) conn.on('close', function () { delete self.connections[peer.id.toB58String()] })
errorUp(self, conn) 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 // spawn new muxed stream
var conn = self.connections[peer.id.toB58String()].conn var conn = self.connections[peer.id.toB58String()].conn
conn.dialStream(function (err, stream) { conn.dialStream(function (err, stream) {
@ -127,7 +132,7 @@ function Swarm () {
}) })
}) })
}) })
} })
} }
self.registerHandler = function (protocol, handlerFunc) { self.registerHandler = function (protocol, handlerFunc) {