Description
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 addressesswarm.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!