mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-30 13:59:35 +07:00
* Remove docs folder. This was a redirect from a GitHub pages site that does not appear to be in use. * Rename api folder to api-samples. * Move examples to functional-samples folder. * Move cookbook sample to functional-samples. * Move tutorials to functional-samples folder. * Move mv2 and apps folders to _archive. * Rename tools folder to .repo. * Move reference folder to functional-samples. * Update README. Update README with new relative links for reorg. * Update README.md Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com> --------- Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com>
44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
/*
|
|
* Node.js UDP echo server
|
|
*
|
|
* This demonstration shows a basic echo server that has randomly drops responses.
|
|
* The drop factor is `threshold` 0.9 = 90% chance of succes, 10% dropped packets
|
|
*
|
|
* Additionally each response is delayed by 2-3 seconds.
|
|
*
|
|
* Listens on port 3007 by default. Pass in a desired port as cmdline argument.
|
|
*/
|
|
|
|
var dgram = require('dgram');
|
|
var server = dgram.createSocket('udp4');
|
|
|
|
var threshold = 0.99;
|
|
|
|
server.on("listening", function() {
|
|
var address = server.address();
|
|
console.log("Listening on " + address.address);
|
|
});
|
|
|
|
server.on("message", function(message, rinfo) {
|
|
var delay = 2000+Math.random()*1000;
|
|
// Echo the message back to the client.
|
|
var dropped = Math.random();
|
|
if(dropped > threshold) {
|
|
console.log("Recieved message from: " + rinfo.address + ", DROPPED");
|
|
return;
|
|
}
|
|
console.log("Recieved message from: " + rinfo.address);
|
|
setTimeout(function() {
|
|
server.send(message, 0, message.length, rinfo.port, rinfo.address, function(err, bytes) {
|
|
console.log(err, bytes);
|
|
});
|
|
}, delay);
|
|
});
|
|
|
|
server.on("close", function() {
|
|
console.log("Socket closed");
|
|
});
|
|
|
|
var port = process.argv[2];
|
|
server.bind(port ? parseInt(port) : 3007);
|