Created
March 25, 2018 09:54
-
-
Save jeiea/726b1dfac6feefa730fba11b8b34fa8b to your computer and use it in GitHub Desktop.
node.js echo server
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'); | |
let clients = []; | |
let server = net.createServer(stream => { | |
stream.setEncoding('utf8'); | |
clients.push(stream); | |
let idx = clients.indexOf(stream); | |
console.log(`client ${idx} connected.`); | |
stream.on('data', data => { | |
console.log(`client ${idx} sended: ${data}`); | |
stream.write(data); | |
}); | |
stream.on('end', () => { | |
stream.end(); | |
console.log(`removed client ${idx}`); | |
clients[idx] = null; | |
}); | |
}); | |
process.on('uncaughtException', exception => { | |
console.log(exception); | |
}); | |
console.log('Server is running on ::8080'); | |
server.listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment