beNerd
beNerd

Reputation: 3374

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.

Upvotes: 1

Views: 595

Answers (1)

3on
3on

Reputation: 6339

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.

Upvotes: 2

Related Questions