简体   繁体   English

在 ejs 和 nodejs 中发布 http://localhost:3000/socket.io/?EIO=3&transport=polling&t=NQUneY3 400(错误请求)

[英]POST http://localhost:3000/socket.io/?EIO=3&transport=polling&t=NQUneY3 400 (Bad Request) in ejs and nodejs

I am very new to this socket.io.我对这个 socket.io 很陌生。

I have the code this is a node server code:我有代码,这是一个节点服务器代码:

var express = require("express");
var app = express();
var http = require("http").createServer(app);
var socketIO = require("socket.io")(http);
var socketID = "";

socketIO.on("connection", function (socket) {
  console.log("User is conneected ", socket.id);
  socketID = socket.id;
});

And here is the code for a ejs file:这是一个ejs文件的代码:

......

<script src="/public/js/socket.io.js"></script>

<script>
......
var socketIO = io("http://localhost:3000");
......
</script>
...... 

And the socket.io.js file is here : socket.io.js 文件在这里

I tried but nothing is working.我试过了,但没有任何效果。 The same error pops whenever I refresh the page.每当我刷新页面时都会弹出相同的错误。 I am very new to this and I really want to get it sorted as soon as possible!!我对此很陌生,我真的很想尽快把它整理好!!

I already have a listen function just after socket.on :我已经在 socket.on 之后socket.on

http.listen(3000, function () {
  console.log("Server has started running!!");
  .........................
.............
})

The below code is working for me.下面的代码对我有用。

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

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

server.listen(3000, function(){
    console.log('listening on *:3000');
});

<script src="/socket.io/socket.io.js" > </script>

<script>
    $(function () {
        var socket = io.connect();
    });
</script>

socket.io.js that you have mentioned is the same as that from the https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js.. .您提到的 socket.io.js 与https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js中的相同。 check the installed version of serail io in package.json and place the same version here in the path socket.io/2.2.0 it will work检查 package.json 中安装的 serail io 版本,并将相同版本放置在路径 socket.io/2.2.0 中

Please Check versions of socket.io in your Frontend and Backend.请在您的前端和后端检查 socket.io 的版本。 It should be compatible.它应该是兼容的。 I had same issue so I solved it with version change.我有同样的问题,所以我通过版本更改解决了它。 Like I had 2.2.0 in my frontend so I install 1.7.2 in backend or same as frontend.就像我的前端有 2.2.0,所以我在后端安装 1.7.2 或与前端相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法连接socket.io节点服务器以响应前端。 “ POST http:// localhost:3000 / socket.io /?EIO = 3&transport = polling&t = MoHNIJT 404(未找到)” - Can't connect socket.io node server to react frontend. “POST http://localhost:3000/socket.io/?EIO=3&transport=polling&t=MoHNIJT 404 (Not Found)” GET http://localhost:8563/socket.io/?EIO=3&transport=polling&t=NEsUPis 404(未找到) - GET http://localhost:8563/socket.io/?EIO=3&transport=polling&t=NEsUPis 404 (Not Found) GET http://localhost:3306/socket.io/?EIO=3&amp;transport=polling&amp;t=NQfLxAy net::ERR_INVALID_HTTP_RESPONSE - GET http://localhost:3306/socket.io/?EIO=3&transport=polling&t=NQfLxAy net::ERR_INVALID_HTTP_RESPONSE GET http://MY_DOMAIN.com/socket.io?EIO=3&amp;transport=polling&amp;t=1449205340962-2 404(未找到) - GET http://MY_DOMAIN.com/socket.io?EIO=3&transport=polling&t=1449205340962-2 404 (Not found) polling-xhr.js:229 GET http://localhost:3000/socket.io/?EIO=4&transport=polling&t=NMtL4rR.net::ERR_CONNECTION_REFUSED - polling-xhr.js:229 GET http://localhost:3000/socket.io/?EIO=4&transport=polling&t=NMtL4rR net::ERR_CONNECTION_REFUSED Phaser.js中的Node.js和Socket.io未连接socket.io/?EIO=3&amp;transport=polling - Node.js and Socket.io in Phaser.js not connecting socket.io/?EIO=3&transport=polling WebSocket 连接到 'ws://localhost:5000/socket.io/?EIO=3&amp;transport=websocket' 失败: - WebSocket connection to 'ws://localhost:5000/socket.io/?EIO=3&transport=websocket' failed: 加载反应应用程序错误时,polling-xhr.js:203 GET https://localhost:8001/socket.io/?EIO=4&amp;transport=polling&amp;t=NZJdzB8 net::ERR_CONNECTION_REFUSED - while loading react app error , polling-xhr.js:203 GET https://localhost:8001/socket.io/?EIO=4&transport=polling&t=NZJdzB8 net::ERR_CONNECTION_REFUSED POST http://localhost:3000/api/signup 400(错误请求) - POST http://localhost:3000/api/signup 400 (Bad Request) POST http://localhost:3000/updateSurvey 400(错误请求) - POST http://localhost:3000/updateSurvey 400 (Bad Request)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM