1

我对 socket.io 完全陌生,并试图从他们主页上的示例开始让我的脚湿透。但是执行后我在控制台中得到的只是这个

调试 - 提供静态内容 /socket.io.js

我的服务器端代码是这样的:

    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);
    });
    })

我的 index.html 是这样的:

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>

我用谷歌搜索并无法解决问题。我在 ubuntu 上使用 Firefox。

4

1 回答 1

2

如果我没记错的话,你的错误就在这里:

'document.location.href'

应该是

document.location.href

我刚刚完成了一个简单的示例应用程序,我将很快为其编写教程: https ://github.com/dotcloud/socket.io-on-dotcloud

您可以抓住它(只需克隆它)并使用它来轻松地使用 express 3 开始使用 socket.io。如果您想分享您的应用程序,它甚至可以推送到 dotCloud 。

于 2012-08-18T07:28:39.350 回答