socket.io example: not working

I am completely new to the socket.io and trying to get my feet wet by starting with examples on their home page. But all that i get in console after execution is this

debug - served static content /socket.io.js

My Server side code is this:

    var app=require('http').createServer(handler)
    , io = require('socket.io').listen(app)
    , fs = require('fs')
    app.listen(80);

    function handler (req, res)
    {

    fs.readFile(__dirname + '/index.html', function (err, data) 
    {
        if (err)
        {
               res.writeHead(500);
               return res.end('Error loading index.html');
        }
        res.writeHead(200);
        res.end(data);
       });
     }

    io.sockets.on('connection', function (socket) {
    console.log("connected");
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
    console.log(data);
    });
    })

And my index.html goes like this:

var socket = io.connect('document.location.href');

   socket.on('error',function(reason){
  // console.error("Error");


  });
  socket.on('connect', function () {
  console.log('connected');       
          socket.send('hi');

          socket.on('message', function (msg) {
                // my msg
                    });
                     });
             </script>

I googled about it and couldn't resolve the issue. I am on ubuntu with firefox.


If i'm not mistaken your error is here:

'document.location.href'

which shall be

document.location.href

I've just complete a simple example app for which I'll be soon writing a tutorial: https://github.com/dotcloud/socket.io-on-dotcloud

You can grab it (just clone it) and fool around with it to easy how to get started with socket.io with express 3. It is even ready to be push on dotCloud if you whish to share your app.

链接地址: http://www.djcxy.com/p/63176.html

上一篇: Node.js:socket.io关闭客户端连接

下一篇: socket.io示例:不工作