diff --git a/src/connection.js b/src/connection.js index cba1538..79e10c6 100644 --- a/src/connection.js +++ b/src/connection.js @@ -1,8 +1,13 @@ 'use strict' +const debug = require('debug') + const connHandler = require('./default-handler') const identify = require('./identify') +const log = debug('libp2p:swarm:connection') +log.error = debug('libp2p:swarm:connection:error') + module.exports = function connection (swarm) { return { addUpgrade () {}, @@ -17,6 +22,10 @@ module.exports = function connection (swarm) { var peerIdForConn + muxedConn.on('end', () => { + conn.end() + }) + muxedConn.on('stream', (conn) => { function gotId () { if (peerIdForConn) { @@ -38,7 +47,7 @@ module.exports = function connection (swarm) { if (swarm.identify) { identify.exec(conn, muxedConn, swarm._peerInfo, (err, pi) => { if (err) { - return console.log('Identify exec failed', err) + return log.error('Identify exec failed', err) } peerIdForConn = pi.id @@ -49,6 +58,7 @@ module.exports = function connection (swarm) { swarm.emit('peer-mux-established', pi) muxedConn.on('close', () => { + log('peer-mux-closed', pi.id.toB58String()) delete swarm.muxedConns[pi.id.toB58String()] swarm.emit('peer-mux-closed', pi) }) diff --git a/src/default-handler.js b/src/default-handler.js index 9b30150..9a08e96 100644 --- a/src/default-handler.js +++ b/src/default-handler.js @@ -20,10 +20,6 @@ module.exports = function connHandler (protocols, conn) { ms.addHandler(protocol, protocols[protocol]) }) - conn.on('error', (err) => { - log.error(err) - }) - ms.handle(conn, (err) => { if (err) { log.error(err) diff --git a/src/dial.js b/src/dial.js index 08512e5..676bb5d 100644 --- a/src/dial.js +++ b/src/dial.js @@ -172,6 +172,7 @@ module.exports = function dial (swarm) { muxedConn.on('close', () => { delete swarm.muxedConns[pi.id.toB58String()] + conn.end() swarm.emit('peer-mux-closed', pi) })