Last active
October 16, 2018 20:45
-
-
Save getnamo/7350f00823f46d9463240160320d03a3 to your computer and use it in GitHub Desktop.
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
/** from: https://nodejs.org/api/net.html#net_server_address */ | |
const net = require('net'); | |
const util = require('util'); | |
const port = 3000; | |
let server = net.createServer((socket) => { | |
//give it an id | |
socket.id = Math.floor(Math.random() * 1000); | |
//console.log(util.inspect(socket)); | |
console.log('Socket connected as ' + socket.id); | |
socket.write('connected as ' + socket.id); | |
socket.on('data',(data)=>{ | |
console.log('got data: ' + data); | |
socket.write('echo ' + data + '\r\n'); | |
}); | |
socket.on('error',(err)=>{ | |
console.log(socket + 'err ' + err); | |
}); | |
socket.on('close',()=>{ | |
console.log(socket.id + ' disconnected.'); | |
}); | |
}); | |
server.listen(port); | |
console.log('TCP listening at port ' + port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment