大家好,
我是 socket.io 的新手。 我的 NodeJs/Express 服务器中有以下代码
const http = require('http');
const socketIo = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
io.on('connection', socket => {
console.log('New client connected');
socket.on('disconnect', () => console.log('Client disconnected'));
socket.emit('FromAPI', 'hello');
});
//My port here is 8848
app.listen(app.get('port'), () => {
logger.log('info', `Server started at :${app.get('port')}`);
});
和客户端反应应用程序中的以下代码和我的反应应用程序托管在端口 3000 作为http://localhost :3000
import socketIO from 'socket.io-client';
componentDidMount() {
const endPoint = 'http://127.0.0.1:8848';
const socket = socketIO(endPoint);
socket.on('FromAPI', data => console.log(data));
}
使用此代码,我在浏览器控制台上收到以下错误:
polling-xhr.js:263 GET http://127.0.0.1 :8848/socket.io/?EIO=3&transport=polling&t=MMT-4kl 405(不允许的方法)
无法加载http://127.0.0.1 :8848/socket.io/?EIO=3&transport=polling&t=MMT-Aso:响应中“Access-Control-Allow-Origin”标头的值不能是通配符'*' 当请求的凭证模式为 'include' 时。 因此,不允许访问 Origin ' http://localhost :3000'。 XMLHttpRequest 发起的请求的凭证模式由 withCredentials 属性控制。
任何人都请帮助我解决这个问题。
那么,关闭并没有解决方案? :(
遇到同样的问题,找不到解决办法。。
有同样的问题没有解决方案
最有用的评论
那么,关闭并没有解决方案? :(