1

所以我试图用nodejs和socket.io设置一个新项目

起初我在这个站点的家用电脑上尝试这些代码:http: //socket.io/#how-to-use

我尝试了第一个示例,它运行良好,所以我继续在我的家用电脑上工作。

但是,当我在我的工作电脑上尝试它时,我的工作无法正常工作。
即使是网络上的第一个示例也不起作用。
客户端没有从服务器接收到发出的“新闻”事件,
并且客户端没有向服务器发出“我的其他事件”。
客户端浏览器没有错误,我已经尝试过我使用的所有浏览器(firefox,ie和chrome都是最新版本)

为什么客户端不向服务器发送事件,客户端无法接收服务器发送的数据?

问题是什么?是我工作的电脑吗?因为我已经在 3 台不同的计算机上尝试过,结果还是一样。它只适用于我的家用电脑。在我的工作计算机(win7 x86)上,我已经禁用了我的 UAC 和防火墙。

我使用 nodejs v0.10.1,socket.io v0.9.13

这是我从 socket.io 的示例中复制的代码:
client.html

<script src="./node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>

<script>
  var socket = io.connect('http://localhost:8999');

  setTimeout(function(){
    socket.emit('my other event', { my: 'emitted by timeout' }); // nothing happened at server
  },3000);

  socket.on('news', function (data) {
    console.log(data); // this won't fire
    socket.emit('my other event', { my: 'data' }); // this won't fire
  });
</script>

服务器.js

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

app.listen(8999);

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) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

节点控制台的输出:

   info  - socket.io started
   debug - client authorized
   info  - handshake authorized c3Y6q0EnyWtldKBKnUHp
   debug - setting request GET /socket.io/1/websocket/c3Y6q0EnyWtldKBKnUHp
   debug - set heartbeat interval for client c3Y6q0EnyWtldKBKnUHp
   debug - client authorized for
   debug - websocket writing 1::
   debug - websocket writing 5:::{"name":"news","args":[{"hello":"world"}]}
4

0 回答 0