javascript Socket.io 400(错误请求)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24560480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:02:07  来源:igfitidea点击:

Socket.io 400 (Bad Request)

javascriptiosnode.jssocketsexpress

提问by Markuz Shultz

I have this piece of code on my server

我的服务器上有这段代码

    var express = require('express');
    var routes = require('./routes');
    var user = require('./routes/user');
    var http = require('http');
    var path = require('path');

var app = express();
    var server = require('http').Server(app);
    var io = require('socket.io')(server);

    server.listen(3000);

    io.sockets.on('connection', function (socket) {
      console.log("Socket connected");
    });

I just want to create a connection

我只想建立一个连接

and on the client -

在客户端 -

<script src="public/javascripts/socket.io.js"></script>
     <script>
        var socket = io.connect('http://127.0.0.1:3000');
     </script>

And when I open my browser I get this error in console:

当我打开浏览器时,我在控制台中收到此错误:

GET http://127.0.0.1:3000/socket.io/1/?t=1404410309733 400 (Bad Request) socket.io.js:1659
XHR finished loading: GET "http://127.0.0.1:3000/socket.io/1/?t=1404410309733". 

I've already done this like 10 times and I never get this. Does anyone know how to fix it?

我已经这样做了 10 次,但我从来没有得到过这个。有谁知道如何修理它?

回答by Mritunjay

I think an older version of socket-iocould be the reason.

我认为旧版本的socket-io可能是原因。

I was getting this problem when I was using 0.9.16version. I upgraded to following and it worked.

我在使用0.9.16版本时遇到了这个问题。我升级到跟随并且它起作用了。

<script src="https://cdn.socket.io/socket.io-1.0.6.js"></script>