From 993e4f4fa27549a4c6258a0b2a2786c0f01a4ce5 Mon Sep 17 00:00:00 2001 From: Garrett Bartley Date: Wed, 24 Feb 2016 18:13:34 +0000 Subject: [PATCH] Added UDP listen command --- commands/UdpCommands.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/commands/UdpCommands.js b/commands/UdpCommands.js index 39d005bca..17d51dec2 100644 --- a/commands/UdpCommands.js +++ b/commands/UdpCommands.js @@ -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)); }, @@ -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; \ No newline at end of file