Socket.io无法连接,控制台上未记录连接消息。

huangapple go评论79阅读模式
英文:

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(&#39;express&#39;);
const http = require(&#39;http&#39;);
const mongoose = require(&#39;mongoose&#39;);

const app = express();
const port = process.env.PORT || 3000;

var server = http.createServer(app);
var io = require(&quot;socket.io&quot;)(server);

app.use(express.json());

const DB = &quot;&quot;;

io.on(&quot;connection&quot;,(socket) =&gt; {
    console.log(&quot;connected!&quot;);   
});

mongoose.connect(DB).then(() =&gt; {
    console.log(&quot;connection successful&quot;);
}).catch((err) =&gt; console.log(&quot;no connection&quot;));    

server.listen(port, &quot;0.0.0.0&quot;, () =&gt; {
    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(&quot;connection&quot;, ...) 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.

huangapple
  • 本文由 发表于 2023年6月22日 00:19:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定