Last active
July 9, 2016 16:55
-
-
Save vicatcu/3d5e8925e605ff64b4b572edcb87eb33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Promise = require('bluebird'); | |
var promiseDoWhilst = require('promise-do-whilst'); | |
var dgram = require('dgram'); | |
var ANNOUNCEMENT_PORT = 43662; | |
var SUBNET_BROADCAST_ADDRESS = '192.168.1.255'; | |
function sendDatagram(){ | |
let message = new Buffer(JSON.stringify(messageJson)); | |
var client; | |
return Promise.try(() => { | |
return new Promise((resolve, reject) => { | |
client = dgram.createSocket("udp4", (error) => { | |
if (error) { | |
console.log(`Warn: Create Socket Failed: ${error.message}`); | |
reject(error); | |
return; | |
} | |
resolve(); | |
}); | |
}); | |
}).then(() => { | |
console.log(`Info: Socket created`); | |
client.setBroadcast(true); | |
}).then(() => { | |
console.log(`Info: Broadcast set to true`); | |
return new Promise((resolve, reject) => { | |
client.send(message, 0, message.length, ANNOUNCEMENT_PORT, SUBNET_BROADCAST_ADDRESS, (error) => { | |
if(error){ | |
reject(error); | |
return; | |
} | |
resolve(); | |
}); | |
}); | |
}).then(() => { | |
console.log(`Info: Message Sent: `, message.toString()); | |
return new Promise((resolve, reject) => { | |
client.close(() => { | |
resolve(); | |
}); | |
}) | |
}).catch((error) => { | |
if(client){ | |
client.close(); | |
} | |
console.log(`Warn: Announce failed: ${error.message}, ${error.stack}`); | |
}) | |
} | |
promiseDoWhilst(() => { | |
return sendDatagram().delay(10000); | |
}, () => {return true;}).catch((error) => { | |
console.log(error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment