Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

topology.add isnt working :/ #3

Open
jackwthake opened this issue May 9, 2018 · 1 comment
Open

topology.add isnt working :/ #3

jackwthake opened this issue May 9, 2018 · 1 comment

Comments

@jackwthake
Copy link

jackwthake commented May 9, 2018

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!

@jackwthake
Copy link
Author

sry about the formatting - couldnt get it to work - new to all of this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant