Node.js UDP广播在环回上工作,而不是在WiFi

时间:2016-10-10 04:36:45

标签: node.js macos udp raspbian

尝试"找到"我的RaspberryPi在本地WiFi上通过数据报广播其IP。新的,但似乎它应该工作:

广播讯息:

var Lighthouse = require('../lib/lighthouse.js');
var lh = new Lighthouse();
lh.startBroadcasting();

收听消息:

lh.listen();

NodeJS 6.7.0

完整代码:

var Netmask = require('netmask').Netmask
var dgram = require('dgram');
var os = require('os');

var lighthouse = function Lighthouse() {
  this.timer = null;
  this.interval = 1;
  this.port = 3001;
  this.server = dgram.createSocket({type: "udp4", reuseAdr: true});
}

lighthouse.prototype.listen = function() {
  var self = this

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

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

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

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

  self.server.bind(self.port);
}

lighthouse.prototype.startBroadcasting = function() {
  var self = this;

  self.address = false;
  self.mask = false;

  var ifaces = os.networkInterfaces();
  Object.keys(ifaces).forEach(function (ifname) {
    ifaces[ifname].forEach(function (iface) {
      if ('IPv4' !== iface.family || iface.internal !== false) {
        return;
      }

      self.address = iface.address
      self.mask = iface.netmask
    });
  });

  if (self.address && self.mask) {
    if (self.timer) {
      clearInterval(self.timer);
    }

    self.message = new Buffer("{server: '" + self.address + "'}");

    block = new Netmask(self.address + '/' + self.mask);
    self.braodcast_address = block.broadcast;

    console.log(`Broadcasting to: ${self.braodcast_address}:${self.port}`);
    console.log(String(self.message));
  }

  self.server.bind(function() {
    self.server.setBroadcast(true);
    self.timer = setInterval(function() {
      self.rebroadcast();
    }, self.interval * 1000);
  });
}

lighthouse.prototype.rebroadcast = function() {
  var self = this;

  if (self.message) {
    self.server.send(self.message, 0, self.message.length, self.port, self.broadcast_address, function() {
      console.log(`Sent message: ${self.message}`);
    });
  }
}

module.exports = lighthouse;

这是客户端和服务器在同一台计算机(任一台计算机)上运行时的输出:

00:20 $ node debug/broadcast.js 
Broadcasting to: 10.0.1.255:3001
{server: '10.0.1.7'}
Sent message: {server: '10.0.1.7'}
Sent message: {server: '10.0.1.7'}
Sent message: {server: '10.0.1.7'}
Sent message: {server: '10.0.1.7'}

00:20 $ node debug/listen.js 
server listening 0.0.0.0:3001
server got: {server: '10.0.1.7'} from 127.0.0.1:50864
server got: {server: '10.0.1.7'} from 127.0.0.1:50864
server got: {server: '10.0.1.7'} from 127.0.0.1:50864
server got: {server: '10.0.1.7'} from 127.0.0.1:50864

似乎要联系,但是当在不同的机器上时,聆听方仅停留在server listening 0.0.0.0:3001

在这里做广播是错误的吗? This answer似乎暗示它应该是非广播多播。也许AirPort路由器只是阻止这样的消息。像sudo tcpdump -n udp port 3001这样的tcpdump似乎没有显示任何内容。

1 个答案:

答案 0 :(得分:0)