javascript - Multiple Clients connected to the same Server using UDP in NodeJS - Stack Overflow

Is it possible to have multiple clients to the same UDP server ?I'd like to broadcast the same da

Is it possible to have multiple clients to the same UDP server ? I'd like to broadcast the same data to all connected clients.

Here would be a starting sample, if it helps somehow ...

// Server
var news = [
  "Borussia Dortmund wins German championship",
  "Tornado warning for the Bay Area",
  "More rain for the weekend"
];

var dgram = require('dgram');
var server = dgram.createSocket("udp4");
server.bind(function() {
  server.setBroadcast(true)
  server.setMulticastTTL(128);
  setInterval(broadcastNew, 3000);
});

function broadcastNew() {
  var message = new Buffer(news[Math.floor(Math.random() * news.length)]);
  server.send(message, 0, message.length, 5007, "224.1.1.1");
  console.log("Sent " + message + " to the wire...");
}

// Client 1
var PORT = 5007;
var dgram = require('dgram');
var client = dgram.createSocket('udp4');

client.on('listening', function() {
  var address = client.address();
  console.log('UDP Client listening on ' + address.address + ":" + address.port);
  client.setBroadcast(true)
  client.setMulticastTTL(128);
  client.addMembership('224.1.1.1');
});

client.on('message', function(message, remote) {
  console.log('A: Epic Command Received. Preparing Relay.');
  console.log('B: From: ' + remote.address + ':' + remote.port + ' - ' + message);
});

client.bind(PORT);

// Client 2

// Here would go another client, it is possible ? 

Is it possible to have multiple clients to the same UDP server ? I'd like to broadcast the same data to all connected clients.

Here would be a starting sample, if it helps somehow ...

// Server
var news = [
  "Borussia Dortmund wins German championship",
  "Tornado warning for the Bay Area",
  "More rain for the weekend"
];

var dgram = require('dgram');
var server = dgram.createSocket("udp4");
server.bind(function() {
  server.setBroadcast(true)
  server.setMulticastTTL(128);
  setInterval(broadcastNew, 3000);
});

function broadcastNew() {
  var message = new Buffer(news[Math.floor(Math.random() * news.length)]);
  server.send(message, 0, message.length, 5007, "224.1.1.1");
  console.log("Sent " + message + " to the wire...");
}

// Client 1
var PORT = 5007;
var dgram = require('dgram');
var client = dgram.createSocket('udp4');

client.on('listening', function() {
  var address = client.address();
  console.log('UDP Client listening on ' + address.address + ":" + address.port);
  client.setBroadcast(true)
  client.setMulticastTTL(128);
  client.addMembership('224.1.1.1');
});

client.on('message', function(message, remote) {
  console.log('A: Epic Command Received. Preparing Relay.');
  console.log('B: From: ' + remote.address + ':' + remote.port + ' - ' + message);
});

client.bind(PORT);

// Client 2

// Here would go another client, it is possible ? 
Share Improve this question edited Jun 6, 2016 at 14:27 coyotte508 9,7556 gold badges46 silver badges66 bronze badges asked Jun 6, 2016 at 13:06 Relu MesarosRelu Mesaros 5,0584 gold badges28 silver badges33 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Yes, it is possible.

I won't go on a speech about how you should use TCP before UDP and only use UDP when absolutely necessary.

For your problem, the fact is that UDP doesn't have any "connection". You receive messages, you send messages, but there is no "connection".

So what you should do is:

  • When receiving a message from an ining client, store the IP/Port used by the client
  • When wanting to send messages to clients, send to all the IP/Port binations stored
  • Periodically remove old clients (for example who didn't send a message in the last 5 minutes)

You can detect when a message is received on a bound socket after the "message" event. Your code would look something like that (helped myself):

// Server
var news = [
  "Borussia Dortmund wins German championship",
  "Tornado warning for the Bay Area",
  "More rain for the weekend"
];

var clients = {};

const dgram = require('dgram');
const server = dgram.createSocket('udp4');

server.on('error', (err) => {
  console.log(`server error:\n${err.stack}`);
  server.close();
});

server.on('message', (msg, rinfo) => {
  console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);

  clients[JSON.stringify([rinfo.address, rinfo.port])] = true;
  //use delete clients[client] to remove from the list of clients
});

function broadCastNew() {
    var message = new Buffer(news[Math.floor(Math.random() * news.length)]);

    for (var client in clients) {
        client = JSON.parse(client);
        var port = client[1];
        var address = client[0];
        server.send(message, 0, message.length, port, address);
    }

    console.log("Sent " + message + " to the wire...");
}

server.on('listening', () => {
  var address = server.address();
  console.log(`server listening ${address.address}:${address.port}`);

  setInterval(broadcastNew, 3000);
});

server.bind(5007);

Now whenever your server gets an UDP message on port 5007, it will add the sender to the list of clients, and every 3 seconds it will send a message to all the clients stored. How to make the sender receive that piece of news is another story, but you can use a tool such as WireShark to confirm yourself that it was correctly sent back.

Here I didn't delete old clients but you probably should include a mechanism to store the last time they contacted you (instead of using = true you can for example store current time, then periodically remove old clients)

Broadcast and Multicast are probably different from what you imagine, broadcast is used for example to send a message to everyone on the local network.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745040585a4607783.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信