没有建立Socket.io连接。

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

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:

  1. io is being called,
  2. Server is listening on PORT 3005,
  3. 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:

  1. io is being called,
  2. Server is listening on PORT 3005,
  3. 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

  1. 你的代码没有问题。console.log('you connected!') 不会出现在服务器端日志中,因为它是客户端代码

  2. 你需要检查客户端日志,在你用来打开客户端的浏览器中查看you connected!。例如,在Google Chrome中,打开开发者工具的快捷键是F-12。

英文:
  1. 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.

  2. 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.

huangapple
  • 本文由 发表于 2023年4月17日 10:03:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031281.html
匿名

发表评论

匿名网友

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

确定