Javascript 在socket.io中从客户端到服务器的连接失败

Javascript 在socket.io中从客户端到服务器的连接失败,javascript,node.js,socket.io,Javascript,Node.js,Socket.io,我不熟悉NodeJS和socket.io,我正在windows机器上尝试socket.io的基本示例 服务器代码 var io = require('socket.io').listen(8080); io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { c

我不熟悉NodeJS和socket.io,我正在windows机器上尝试socket.io的基本示例

服务器代码

    var io = require('socket.io').listen(8080);

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});
    <script src="http://localhost:8080/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:8080/');

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

    socket.on('error', function (data) {
        console.log(data || 'error');
    });

    socket.on('connect_failed', function (data) {
        console.log(data || 'connect_failed');
    });
</script>
客户端代码

    var io = require('socket.io').listen(8080);

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});
    <script src="http://localhost:8080/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:8080/');

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

    socket.on('error', function (data) {
        console.log(data || 'error');
    });

    socket.on('connect_failed', function (data) {
        console.log(data || 'connect_failed');
    });
</script>

如何修复上述脚本中的错误以成功运行socket.io的基本示例?

使用以下数据创建localhost.js文件:

var port = 80,
http = require('http'),
fs = require('fs'),
socket = require('socket.io'),
app = function (req, res) {
  var url = req.url == '/' ? '/index.html' : req.url;
  fs.readFile(__dirname + url, 'utf8', function (err, data) {
    if (err) return console.log(err);
    res.end(data);
  });
},
httpServer = http.createServer(app).listen(port),
io = socket.listen(httpServer);
io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});
以及包含以下内容的index.html文件:

<html>
<head>
  <script src="socket.min.js"></script>
</head>
<body>
<script>
  var socket = io.connect('ws://localhost:80/');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });

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

  socket.on('error', function (data) {
    console.log(data || 'error');
  });
</script>
</body>
</html>

使用以下数据创建localhost.js文件:

var port = 80,
http = require('http'),
fs = require('fs'),
socket = require('socket.io'),
app = function (req, res) {
  var url = req.url == '/' ? '/index.html' : req.url;
  fs.readFile(__dirname + url, 'utf8', function (err, data) {
    if (err) return console.log(err);
    res.end(data);
  });
},
httpServer = http.createServer(app).listen(port),
io = socket.listen(httpServer);
io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});
以及包含以下内容的index.html文件:

<html>
<head>
  <script src="socket.min.js"></script>
</head>
<body>
<script>
  var socket = io.connect('ws://localhost:80/');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });

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

  socket.on('error', function (data) {
    console.log(data || 'error');
  });
</script>
</body>
</html>

您可以根据需要配置socket.io设置。

我也遇到了同样的问题,发现添加下面的行可以解决问题

io.set('transports',['xhr-polling']);

您可以根据需要配置socket.io设置。

我也遇到了同样的问题,发现添加下面的行可以解决问题

io.set('transports',['xhr-polling']);

它确实对我有用。。。我在Windows7上运行nodejs。也许您应该重新安装/更新nodejs和socket.io。在我将传输类型更改为xhr之后,现在可以在我的chrome和firefox中使用:)。为什么?您是如何更改为xhr的?通过
io.set('transports',['xhr-polling'])使用此设置时,您使用的不是套接字,而是ajax。这表明你的socket.io“安装”存在一些问题,它确实适合我。。。我在Windows7上运行nodejs。也许您应该重新安装/更新nodejs和socket.io。在我将传输类型更改为xhr之后,现在可以在我的chrome和firefox中使用:)。为什么?您是如何更改为xhr的?通过
io.set('transports',['xhr-polling'])使用此设置时,您使用的不是套接字,而是ajax。这表明socket.io“安装”存在一些问题。这行代码救了我的命。我想知道为什么它会起作用。谢谢@ZillGate:很高兴提供帮助:)这是否意味着它将只使用xhr池而不使用Websocket,或者当可以使用Websocket时,它将使用?Akamai不支持Websocket。这是退后。嗨。这行代码救了我的命。我想知道为什么它会起作用。谢谢@ZillGate:很高兴提供帮助:)这是否意味着它将只使用xhr池而不使用Websocket,或者当可以使用Websocket时,它将使用?Akamai不支持Websocket。这是退路。