Skip to content

topology.add isnt working :/ #3

Open
@jackwthake

Description

@jackwthake

When I run the code below all the initial peers connect (the ones in the peers array) but when a new peer joins that isn't included in that array joins one of the peers I want all of them to connect with that new peer, when I try to do this by broadcasting the new peer's ip and port, the other peers don't connect to the new peer.

const topology = require('fully-connected-topology');
const json = require('duplex-json-stream');

const self = process.argv[2];

var peers = process.argv.slice(3);
var swarm = topology(self, peers);
var connections = []; // all connected sockets
var addresses = []; // all addresses

swarm.on('connection', (socket, connection) => {
console.log('[ New Connection ]');

connections.forEach((peer) => {
peer.write({
type: 0,
src: self,
msg: socket.remoteAddress.toString().split('::ffff:')[1] + ':' + socket.remotePort.toString()
});
});

addresses.push(socket.remoteAddress.toString().split('::ffff:')[1] + ':' + socket.remotePort.toString());

socket = json(socket);
connections.push(socket);

socket.on('data', (data) => {
console.log('msg type: ' + data.type + ': ' + data.src + ' : ' + data.msg);

   if(data.type === 0) {
       console.log('Attempting to add ' + data.msg);

       addresses.push(data.msg);

       swarm.add(data.msg.toString());
   }

});
});

process.stdin.on('data', (data) => {
connections.forEach((peer) => {
peer.write({
type: 1,
src: self,
msg: data.toString().trim()
});
});
});

thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions