侦听与 socket.io 的连接无效

Listening for connections with socket.io not working

我正在为我的后端使用此代码:

const express = require('express');
const app = express();
const socketIo = require("socket.io");
const io = socketIo(http);

io.on("connection", socket => {
  console.log("New client connected");
  socket.on("disconnect", () => console.log("Client disconnected"));
});

http.listen(3000, () => {
  console.log('listening on *:3000');
});

当我运行它时,它输出消息确认它正在收听。但是,在连接上它不会向控制台发送任何消息。我正在尝试监听与 React 应用程序的连接。我尝试过使用其他代码片段作为连接功能,它们也声称可以按我的预期工作,但是我尝试过的 none 已经起作用,包括 socket.io 的官方教程中的代码。 请问有人能帮忙吗?

const express = require('express')
const app = express()

const PORT = 5000

// Get to http://localhost:5000
app.get("/", (request, response) => {
  // Send back some data to the client
  response.send("Hello world")
})

// Post to http://localhost:5000/getRandom
app.post("/getRandom", (req, res) => {
  res.send(Math.random())
})

app.listen(PORT, () => console.log(`Server running on PORT ${PORT}`))

人们不调用参数requestresponse,而是使用reqres

的缩写形式

现在启动此脚本并转到 http://localhost:5000,您将在 HTML 正文中看到“Hello world”。这是表达,简单而强大:)