javascript 使用www.example.com连接react前端和Express后端socket.io

sdnqo3pr  于 2023-01-16  发布在  Java
关注(0)|答案(2)|浏览(202)

我有一个用React制造的前端,运行在端口3000上,一个express/node服务器运行在端口5000上,它们都在同一台计算机上(没有使用网络)。
我尝试让它们在后端使用www.example.com,socket.io在前端使用socket.io-client进行通信。
连接工作不知何故,但我有一些错误消息在前端控制台,即:

Access to XMLHttpRequest at 'http://localhost:5000/socket.io/?EIO=4&transport=polling&t=OMmsufE' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
polling.js:334          

GET http://localhost:5000/socket.io/?EIO=4&transport=polling&t=OMmsufE net::ERR_FAILED 200

下面是前端代码App.js:

import './App.css';

function App() {
  const socket = require('socket.io-client')('http://localhost:5000/socket.io/');

  socket.on('connect', () => {
    console.log('Connected to server');

    socket.on('message', (message) => {
      console.log('Received message:', message);
    });

    socket.emit('message', 'Hello, server!');
  });

socket.on('disconnect', () => {
  console.log('Disconnected from server');
});

  return (
    <div className="App">App 
    </div>
  );
}

和后端index.js:

const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);

const cors = require("cors")
app.use(cors({ origin: 'http://localhost:3000' }));
io.on('connection', (socket) => {
  console.log('User connected');

  socket.on('disconnect', () => {
    console.log('User disconnected');
  });

  socket.on('message', (message) => {
    console.log('Received message:', message);
    io.emit('message', message);
  });
});
app.use("/socket.io/", (req, res, next) => {
  next()
})

app.get("/", (req, res) => console.log("hello"))

app.use(cors())

server.listen(5000, () => {
  console.log('Server listening on port 5000');
});

有人有主意吗?

xxhby3vn

xxhby3vn1#

我非常确定您的socket.io端点正在阻止真正的端点
委托单位:

// remove `socket.io` here
const socket = require('socket.io-client')('http://localhost:5000/');

服务器:

const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);

const cors = require("cors")
app.use(cors({ origin: 'http://localhost:3000' }));
io.on('connection', (socket) => {
  console.log('User connected');

  socket.on('disconnect', () => {
    console.log('User disconnected');
  });

  socket.on('message', (message) => {
    console.log('Received message:', message);
    io.emit('message', message);
  });
});

app.get("/", (req, res) => console.log("hello"))

app.use(cors())

server.listen(5000, () => {
  console.log('Server listening on port 5000');
});
gv8xihay

gv8xihay2#

所以,确实,正如康拉德所说,问题来自前端,尤其是这条线:

const socket = require('socket.io-client')('http://localhost:5000/socket.io');

这是必要的转变,正如康拉德所说:

const socket = require('socket.io-client')('http://localhost:5000/);

但也有必要补充:

const socket = require('socket.io-client')('http://localhost:5000/', {transports: ['websocket'], upgrade: false});

现在它起作用了

相关问题