javascript - Node.js chat client on browser -
i have node.js chat server developed ios client, develop client usable on browser. don't want use socket.io or similiar, pure tcp socket. code of server:
// load tcp library net = require('net'); //var sys = require('sys'); // keep track of chat clients var clients = []; // start tcp server net.createserver(function (socket) { // identify client socket.name = socket.remoteaddress + ":" + socket.remoteport // put new client in list clients.push(socket); // send nice welcome message , announce socket.write("welcome " + socket.name + "\n"); broadcast(socket.name + " joined chat\n", socket); socket.write(tools.foo); // handle incoming messages clients. socket.on('data', function (data) { broadcast(socket.name + " >> " + data+"\n", socket); }); // remove client list when leaves socket.on('end', function () { clients.splice(clients.indexof(socket), 1); broadcast(socket.name + " left chat.\n"); }); // send message clients function broadcast(message, sender) { clients.foreach(function (client) { // don't want send sender if (client === sender) return; client.write(message); }); // log server output process.stdout.write(message) } }).listen(5100,"192.168.1.8"); // put friendly message on terminal of server. console.log("chat server running @ port 5100\n");
what can use develop it? hope javascript. excuse me i'm newbie of web programming.
Comments
Post a Comment