POST socket.io/?EIO=3&transport=polling&t=NQUneY3 400(Bad Request)in ejs and nodejs

noj0wjuj  于 11个月前  发布在  Node.js
关注(0)|答案(4)|浏览(121)

我对这个socket.io非常陌生。
我有代码这是一个节点服务器代码:

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;
});

字符串
下面是一个ejs文件的代码:

......

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

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


socket.io.js文件是here
我试过了,但没有任何工作。同样的错误弹出每当我刷新页面。我是非常新的这一点,我真的想得到它尽快排序!!
我已经在socket.on后面有一个listen函数:

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

wrrgggsh

wrrgggsh1#

下面的代码是为我工作。

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');
});

个字符

vxf3dgd4

vxf3dgd42#

你提到的socket.io.js与https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js..中的socket.io.js相同。检查package.json中serail io的安装版本,并将相同的版本放置在路径socket.io/2.2.0中,它将工作

gwbalxhn

gwbalxhn3#

请检查socket.io在你的前端和后端的版本。它应该是兼容的。我有同样的问题,所以我解决了它与版本更改。像我有2.2.0在我的前端,所以我安装1.7.2在后端或前端相同。

vom3gejh

vom3gejh4#

以下是我使用Typescript的最新(2024年1月)解决方案:
server.ts

import express, { Express, Request, Response } from 'express';
import { Server, Socket } from 'socket.io';
import { createServer } from 'http';
import path from 'path';

const app: Express = express();
app.set("port", process.env.PORT || 3000);

const httpServer = createServer(app);
const io = new Server(httpServer, {/* options */});

app.get("/", (req: any, res: any) => {
  res.sendFile(path.resolve("./client/index.html"));
});

io.on("connection", function(socket: Socket) {
  console.log("a user connected");
  socket.on("message", function(message: any) {
  console.log(message);
  });
});

const server = httpServer.listen(3000, function() {
  console.log("listening on *:3000");
});

字符串
./client/index.html

<!-- ./client/index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body>
    <button onClick="sendMsg()">Emit Msg</button>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.7.3/socket.io.js"></script>
    <script>
      const socket = io("http://localhost:3000");
      function sendMsg() {
        socket.emit("message", "HELLO WORLD");
      }
    </script>
  </body>
</html>


如前所述,服务器端的socket.io版本与服务页面上的脚本src匹配非常重要4.7.3

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.7.3/socket.io.js">

相关问题