Last active
May 27, 2016 06:06
-
-
Save shakthimaan/2bbca2e558bad7985c7a7b8625f538ab to your computer and use it in GitHub Desktop.
socket.write with delay
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
const net = require("net"); | |
const MACHINE_PORT = 8081; | |
function* machineDataGenerator() { | |
yield 'ONE\r\n'; | |
yield 'TWO\r\n'; | |
yield 'THREE\r\n'; | |
} | |
var machine = net.createServer(); | |
machine.on('connection', (socket) => { | |
var machineData = machineDataGenerator(); | |
var writeData = function (socket) { | |
data = machineData.next().value; | |
if (data) { | |
setTimeout(function () { | |
socket.write(data); | |
writeData(socket); | |
}, 1000); | |
} | |
else { | |
socket.destroy(); | |
} | |
}; | |
writeData(socket); | |
}); | |
machine.listen(MACHINE_PORT, "localhost"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment