I'm making some tests using WebSocket, and I trying to communicate with various "peers" like a P2P network.
I tried this solution below, it works to open all conections and handle all data, but I can't reply to "peer" that I received the initial message.
Anynone have a better idea or other solution? Thanks!
var WebSocket = require('ws');
var peers = ['ws://127.0.0.1:1001', 'ws://127.0.0.1:1002', 'ws://127.0.0.1:1003'];
var ws = [];
for(var i = 0; i<peers.length; i++){
ws[i] = new WebSocket(peers[i]);
ws[i].on('open', function open() {
ws[i].send('something');
});
ws[i].on('message', function(data, flags) {
console.log(data);
});
ws[i].on('error', function(err){
console.log(err);
});
}
Output:
"Cannot read property 'send' of undefined"