Skip to content

Instantly share code, notes, and snippets.

@jeiea
Created March 25, 2018 09:54
Show Gist options
  • Save jeiea/726b1dfac6feefa730fba11b8b34fa8b to your computer and use it in GitHub Desktop.
Save jeiea/726b1dfac6feefa730fba11b8b34fa8b to your computer and use it in GitHub Desktop.
node.js echo server
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