[英]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.