NodeJS Socket.io is not connecting it is not logging out the connected message on the console

pcrecxhr  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(101)

我正在尝试连接到套接字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}`);
});

当我启动服务器“连接!“未登录控制台,请帮助。

bqjvbblv

bqjvbblv1#

io.on("connection", ...)事件仅在socket.io客户端连接到您的服务器时发生。
它不是一个启动消息,与您的其他console.log()语句不同,因此在www.example.com客户端连接之前您不会看到它socket.io。

相关问题