0

每当我执行我的代码时,我都会在控制台收到消息说它已连接,然后什么都没有发生。

这是代码:

服务器端

var app = require('express')()
  , server = require('http').createServer(app)
  , io = require('socket.io').listen(server);

server.listen(80);

app.get('/', function (req, res) {
  res.sendfile('index.html');
});

io.sockets.on('connection', function (socket) {

  console.log('m connected');

  socket.emit('news', { hello: 'm joooon ' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

客户端

<!DOCTYPE html>
<html>

<head>
<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>
</head>
<body>

  <div id="lo">magudi</div>
</body></html>
4

1 回答 1

0

On the client side, your not listening to the port.

var socket = io.connect('http://localhost:80');
于 2013-01-05T23:14:43.217 回答