Skip to content

Commit

Permalink
Merge pull request #220 from wgbartley/master
Browse files Browse the repository at this point in the history
Added UDP listen command
  • Loading branch information
brycekahle committed Feb 27, 2016
2 parents 07ef0b2 + 993e4f4 commit f9ec463
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions commands/UdpCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ UdpCommands.prototype = extend(BaseCommand.prototype, {

init: function () {
this.addOption('send', this.sendUdpPacket.bind(this), 'Sends a UDP packet to the specified host and port');
//this.addOption("listen", this.sendUdpPacket.bind(this), "");
this.addOption("listen", this.listenUdp.bind(this), 'Listens for UDP packets on an optional port (default 5549)');
//this.addOption(null, this.helpCommand.bind(this));
},

Expand All @@ -72,7 +72,24 @@ UdpCommands.prototype = extend(BaseCommand.prototype, {
client.close();
});
});
},

listenUdp: function(port) {
if(port==undefined)
port = 5549;

var udpSocket = dgram.createSocket('udp4');

udpSocket.on('listening', function() {
console.log('Listening for UDP packets on port '+port+' ...');
});

udpSocket.on('message', function(msg, rinfo) {
console.log('['+rinfo.address+'] '+msg.toString());
});

udpSocket.bind(port);
}
});

module.exports = UdpCommands;
module.exports = UdpCommands;

0 comments on commit f9ec463

Please sign in to comment.