英文:
no socket io connection being made
问题
All versions installed of socket.io and socket.io-client are the same in my mern app.
Here is my server code:
const app = express();
mongoose.connect(MONGOOSE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(() => {
console.log(`MongoDB connected successfully`);
}).catch((error) => console.log(`${error} did not connect`));
const server = http.createServer(app);
const io = new Server(server, console.log('io is being called'));
io.on('connection', (socket) => {
console.log(socket.id)
})
server.listen(3005, () => {
console.log(`Server is listening on PORT 3005`);
});
here is my client code:
import io from 'socket.io-client';
const socket = io('http://localhost:3005');
socket.on('connect', () => {
console.log('you connected!')
})
Why doesn't it show the socket io connection was made when I npm start both my client and server?
On my server side, it console logs that:
- io is being called,
- Server is listening on PORT 3005,
- MongoDB connected successfully
Does anyone know if I'm missing anything?
英文:
All versions installed of socket.io and socket.io-client are the same in my mern app.
Here is my server code:
const app = express();
mongoose.connect(MONGOOSE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(() => {
console.log(`MongoDB connected successfully`);
}).catch((error) => console.log(`${error} did not connect`));
const server = http.createServer(app);
const io = new Server(server, console.log('io is being called'));
io.on('connection', (socket) => {
console.log(socket.id)
})
server.listen(3005, () => {
console.log(`Server is listening on PORT 3005`);
});
here is my client code:
import io from 'socket.io-client';
const socket = io('http://localhost:3005');
socket.on('connect', () => {
console.log('you connected!')
})
Why doesn't it show the socket io connection was made when I npm start both my client and server?
On my server side, it console logs that:
- io is being called,
- Server is listening on PORT 3005,
- MongoDB connected successfully
Does anyone know if I'm missing anything?
答案1
得分: 1
以下是翻译好的部分:
const express = require('express');
const app = express();
const http = require('http');
const socket = require('socket.io');
const cors = require('cors');
const server = http.createServer(app);
app.use(cors({
origin: ['http://localhost:3000'],
credentials: true
}))
const io = socket(server, {
cors: {
origin: '*',
credentials: true
}
})
io.on('connection', (socket) => {
console.log(socket.id)
})
英文:
try it
const express = require('express')
const app = express()
const http = require('http');
const socket = require('socket.io');
const cors = require('cors');
const server = http.createServer(app);
app.use(cors({
origin: ['http://localhost:3000'],
credentials: true
}))
const io = socket(server, {
cors: {
origin: '*',
credentials: true
}
})
io.on('connection', (socket) => {
console.log(socket.id)
})
答案2
得分: 1
-
你的代码没有问题。
console.log('you connected!')
不会出现在服务器端日志中,因为它是客户端代码。 -
你需要检查客户端日志,在你用来打开客户端的浏览器中查看
you connected!
。例如,在Google Chrome中,打开开发者工具的快捷键是F-12。
英文:
-
There is no issue in your code.
console.log('you connected!')
will NOT show up in server side logs because it is client side code. -
You have to check the client side logs in the browser you are using to open the client in order to view
you connected!
. The shortcut to Dev Tools is F-12 in Google Chrome for example.
答案3
得分: 0
这是要翻译的内容:
原因是因为我的socket.io-client代码放在一个独立的js文件中。为了使其运行,它需要放在在App.js中呈现的客户端组件中。一旦我这样做了,服务器和客户端之间的连接就建立了。
英文:
The reason it wasn't being made was because my socket.io-client code was placed in an individual js file.
In order for it to run, it needs to be in a component on the client side rendered in App.js.
Once I did that, the connection was made between server and client.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论