Created
November 16, 2012 14:48
-
-
Save fideloper/4087879 to your computer and use it in GitHub Desktop.
Echo server (node.js)
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
var net = require("net"), sys = require('sys'); | |
var clients = [], i; | |
var server = net.createServer(function (stream) { | |
stream.setEncoding("utf8"); | |
stream.on("connect", function () { | |
clients.push(stream); | |
}); | |
stream.on("data", function (data) { | |
console.log(data + "sending to " + clients.length + " clients."); | |
for(i = 0; i < clients.length; i++){ | |
if(clients[i] != stream){ | |
clients[i].write(data + "\0"); | |
} | |
} | |
}); | |
stream.on("end", function () { | |
stream.end(); | |
for(i = 0; i < clients.length; i++){ | |
if(clients[i] == stream){ | |
clients.splice(i, 1); | |
} | |
} | |
console.log('removed client'); | |
}); | |
}); | |
process.on('uncaughtException', function(exception){ | |
console.log("uncaught, just ignoring"); | |
}); | |
console.log("Server's running on 192.168.1.198:8080"); | |
server.listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want to create a simple client that will connect to this server and get replies echoed back on command prompt itself. No fancy HTML CSS. Please help me.