Created
March 10, 2018 21:27
-
-
Save g4rcez/a242438528c8c302ed2731e323166e49 to your computer and use it in GitHub Desktop.
Minirc with Node JS
This file contains hidden or 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
let net = require('net'); | |
let address, | |
os = require('os'), | |
ifaces = os.networkInterfaces(); | |
for (let dev in ifaces) { | |
let iface = ifaces[dev].filter(function(details) { | |
return details.family === 'IPv4' && details.internal === false; | |
}); | |
if (iface.length > 0) address = iface[0].address; | |
} | |
filterArgument = (argumentArray, search) => { | |
return argumentArray | |
.filter(item => item.startsWith(search)) | |
.toString() | |
.split('=')[1]; | |
}; | |
definePort = port => { | |
return port < 65535 && port > 1024 ? port : 1337; | |
}; | |
const arguments = process.argv.slice(2); | |
let port = !!filterArgument(arguments, '-p') ? filterArgument(arguments, '-p') : filterArgument(arguments, '--port'); | |
let nick = !!filterArgument(arguments, '-n') ? filterArgument(arguments, '-n') : filterArgument(arguments, '--nick'); | |
let host = !!filterArgument(arguments, '-h') ? filterArgument(arguments, '-h') : filterArgument(arguments, '--host'); | |
port = definePort(port); | |
let client = net.connect(port, host); | |
client.on('connect', function() { | |
client.write('New client on chat'); | |
}); | |
client.on('data', function(message) { | |
console.log(message.toString()); | |
}); | |
client.on('end', function() { | |
console.log("The server has been destroy") | |
process.exit(); | |
}); | |
process.stdin.on('readable', function() { | |
let message = process.stdin.read(); | |
if (!message) return; | |
message = message.toString().replace(/\n/, ''); | |
client.write(`${message}`); | |
}); |
Author
g4rcez
commented
Mar 10, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment