[英]Listening for connections with socket.io not working
I'm using this code for my backend:我正在将此代码用于我的后端:
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');
});
When I run it, it outputs the message confirming it is listening.当我运行它时,它会输出确认它正在收听的消息。 However, on a connection it does not send any messages to the console.
但是,在连接上,它不会向控制台发送任何消息。 I'm trying to listen for connections to a React app.
我正在尝试侦听与 React 应用程序的连接。 I have tried using other code snippets for the connection function that also claim to work as I expected, however none that I have tried have worked, including the code in the official tutorial for socket.io.
我尝试使用其他代码片段来连接 function,它们也声称可以按预期工作,但是我尝试过的都没有工作,包括 socket.io 官方教程中的代码。 Please can anyone help?
请问有人可以帮忙吗?
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}`))
Instead of calling the parameters request
and response
, people use the short form of req
and res
人们没有调用参数
request
和response
,而是使用req
和res
的缩写形式。
Now start this script and go to http://localhost:5000
and you will see "Hello world" in the HTML body.现在启动这个脚本和 go 到
http://localhost:5000
你将在 HTML 正文中看到“Hello world”。 That's express, simple yet powerful:)那是表达,简单而强大:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.