const net = require('net');
const stream = require('stream');
const server = net.createServer(socket => {
const clientInfo = `${socket.remoteAddress}:${socket.remotePort}`;
console.log(`+ ${clientInfo} - connected`);
socket.write('Echo server\r\n');
socket
.pipe(new ToUpperCaseTransform())
.pipe(socket);
socket.on('data', message => {
process.stdout.write(`> ${clientInfo} : ${message}`);
})
socket.on('close', () => {
console.log(`- ${clientInfo} - closed`)
})
});
server.listen(1337, '127.0.0.1');
class ToUpperCaseTransform extends stream.Transform {
constructor() {
super()
}
_transform(chunk, enc, done) {
this.push(chunk.toString().toUpperCase())
done();
}
}