javascript - socket.io web client not working -
i met socket world socket.io , node.js found server example online: https://gist.github.com/creationix/707146 created first client on ios , receive , dispatch messages, same telnet client, next create second client socket.io receive messages on browser too.
i tried example in howto: http://socket.io/#how-to-use these examples client not recognized!
where wrong? start?
the server code:
// load tcp library net = require('net'); // 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); // handle incoming messages clients. socket.on('data', function (data) { broadcast(socket.name + "> " + data, 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(5000); // put friendly message on terminal of server. console.log("chat server running @ port 5000\n");
the client code:
<script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); </script>
thanks lot.
i know late, , solved issue server listening on port 5000. client connecting port 80.
change clinet code to:
<script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost:5000'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); </script>
Comments
Post a Comment