英文:
Socket.io is not connecting it is not logging out the connected message on the console
问题
I am trying to connect to socket io but is not connecting
我正在尝试连接到 Socket.IO,但连接失败
const express = require('express');
const http = require('http');
const mongoose = require('mongoose');
const app = express();
const port = process.env.PORT || 3000;
var server = http.createServer(app);
var io = require("socket.io")(server);
app.use(express.json());
const DB = "";
io.on("connection",(socket) => {
console.log("connected!");
});
mongoose.connect(DB).then(() => {
console.log("connection successful");
}).catch((err) => console.log("no connection"));
server.listen(port, "0.0.0.0", () => {
console.log(Server running on port ${port}
);
});
when i start the sever "connected!" is not logging on the console please help.
当我启动服务器时,“connected!” 未在控制台上记录,请帮助。
英文:
I am trying to connect to socket io but is is not connecting
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
const express = require('express');
const http = require('http');
const mongoose = require('mongoose');
const app = express();
const port = process.env.PORT || 3000;
var server = http.createServer(app);
var io = require("socket.io")(server);
app.use(express.json());
const DB = "";
io.on("connection",(socket) => {
console.log("connected!");
});
mongoose.connect(DB).then(() => {
console.log("connection successful");
}).catch((err) => console.log("no connection"));
server.listen(port, "0.0.0.0", () => {
console.log(`Server running on port ${port}`);
});
<!-- end snippet -->
when i start the sever "connected!" is not loging on the console please help.
答案1
得分: 0
"io.on("connection", ...)" 事件仅在socket.io客户端连接到您的服务器时发生。
这不是与您的其他"console.log()"语句相同的启动消息,因此您只会在socket.io客户端连接后才会看到它。
英文:
The io.on("connection", ...)
event only occurs when a socket.io client connects to your server.
It's not a startup message in the same way that your other console.log()
statements are so you won't see it until a socket.io client connects.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论