NodeJS Simple Server
var net = require('net');
var sockets = [];
function cleanInput(data) {
return data.toString().replace(/(\r\n|\n|\r)/gm,"");
}
function receiveData(socket, data) {
var cleanData = cleanInput(data);
console.log("received " + cleanData);
if(cleanData === "@quit") {
socket.end('Goodbye!\n');
}
else {
for(var i = 0; i<sockets.length; i++) {
if (sockets[i] !== socket) {
sockets[i].write(data);
}
}
}
}
function closeSocket(socket) {
var i = sockets.indexOf(socket);
if (i != -1) {
sockets.splice(i, 1);
}
}
function newSocket(socket) {
sockets.push(socket);
socket.write("Hello Hackaday Badge\n");
socket.write('_ _ ____ ____ _ _ ____ ___ ____ _ _\n');
socket.write('|__| |__| | |_/ |__| | \\ |__| \\_/ \n');
socket.write('| | | | |___ | \\_ | | |__/ | | | \n');
socket.on('data', function(data) {
receiveData(socket, data);
})
socket.on('end', function() {
closeSocket(socket);
})
}
var server = net.createServer(newSocket);
server.listen(8888);
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.