简体   繁体   English

CORS、Laravel Valet 和 Socket.io

[英]CORS, Laravel Valet, and Socket.io

This feels like it's all way harder than it should be.这感觉就像它应该比它更难。 I am working on an application for a client and am at the end of my rope trying to get this thing running locally.我正在为一个客户开发一个应用程序,并且在我的绳索尽头试图让这个东西在本地运行。 So, I am using Laravel(served through Valet on my mac) and am writing a Socket.io server for handling different dashboard events.所以,我正在使用 Laravel(通过我的 Mac 上的 Valet 提供服务)并且正在编写一个 Socket.io 服务器来处理不同的仪表板事件。 Socket.io runs in an https configuration as it will in production and I've got it running on port 3001. I've got both serving content over https, resolved all configuration issues there. Socket.io 在 https 配置中运行,就像在生产中一样,我让它在端口 3001 上运行。我已经通过 https 提供内容,解决了那里的所有配置问题。 However, now I can not stop getting CORS errors whenever I try to connect.但是,现在每当我尝试连接时,我都无法停止收到 CORS 错误。

Here is the error I am getting:这是我得到的错误:

Access to XMLHttpRequest at 'https://ags.test:3001/socket.io/?EIO=3&transport=polling&t=1605106550351-47' from origin 'https://ags.test' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.从源“https://ags.test”访问“https://ags.test:3001/socket.io/?EIO=3&transport=polling&t=1605106550351-47”的 XMLHttpRequest 已被 CORS 策略阻止:否请求的资源上存在 Access-Control-Allow-Origin 标头。

I've done a fair bit of testing on this and found that if I do a GET request to the root (/) of the express server, it works fine and without CORS errors.我已经对此进行了大量测试,发现如果我对 express 服务器的根 (/) 执行 GET 请求,它可以正常工作并且没有 CORS 错误。 However, the polling at the url posted in the error above is still providing the same CORS error.但是,在上述错误中发布的 url 上的轮询仍然提供相同的 CORS 错误。

If it isn't abundantly obvious by now, I'm fairly new to the node ecosystem.如果现在还不是很明显,我对节点生态系统还很陌生。 So what I'm caught up on is what's happening with CORS when it goes from calling 'https://ags.test:3001' to 'https://ags.test:3001/socket.io/*'.所以我所关注的是 CORS 从调用“https://ags.test:3001”到“https://ags.test:3001/socket.io/*”时发生了什么。 Or is it possible that express isn't handling the CORS for socket.io at all?或者 express 是否有可能根本不处理 socket.io 的 CORS?

Something I've seen multiple times in other stackoverflow answers is something along the lines of (from the server) io.origins('*:*') to set the origin policy.我在其他 stackoverflow 答案中多次看到的内容是(来自服务器) io.origins('*:*')设置原始策略的内容。 However, I can't seem to find documentation on this and calling it (or any of the variations I've seen of it)但是,我似乎无法找到关于此的文档并调用它(或我见过的任何变体)

So you know what I've already tried, here the server configuration for my Socket.io express server:所以你知道我已经尝试过什么,这里是我的 Socket.io express 服务器的服务器配置:

app.use(cors());
app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');

    // authorized headers for preflight requests
    // https://developer.mozilla.org/en-US/docs/Glossary/preflight_request
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    next();

    app.options('*', (req, res) => {
        // allowed XHR methods  
        res.header('Access-Control-Allow-Methods', 'GET, PATCH, PUT, POST, DELETE, OPTIONS');
        res.send();
    });
});
var options = {
    key: fs.readFileSync('./ags.test.key'),
    cert: fs.readFileSync('./ags.test.crt'),
    origins: '*:*',
    cors: true,
};
const server = https.createServer(options, app);

I've also made adjustments to my laravel site config in valet as seen here https://gist.github.com/poul-kg/b669a76fc27afcc31012aa0b0e34f738 but applied directly to my site-specific config at ~/.config/valet/Nginx/ags.test and restarted.我还在代客服务中对我的 laravel 站点配置进行了调整,如下所示https://gist.github.com/poul-kg/b669a76fc27afcc31012aa0b0e34f738但直接应用于我的站点特定配置 ~/.config/valet/Nginx/ ags.test 并重新启动。

As you can see, I've tried quite a few different things and am at the "throw everything at the wall to see what sticks" stage of trying to get these servers communicating.正如您所看到的,我尝试了很多不同的方法,并且正处于“把所有东西都扔在墙上,看看什么是坚持”的阶段,试图让这些服务器进行通信。 Any recommendations?有什么建议吗?

Okay, took me way too long to figure out what I was missing.好吧,我花了太长时间才弄清楚我错过了什么。 Old stackoverflow answers show configuration methods that are no longer supported in Socket.io v3.旧的 stackoverflow 答案显示了 Socket.io v3 中不再支持的配置方法。

Here's what I was missing to configure this for the most recent version of Socket.io as of the date of posting.这是我在发布之日为最新版本的 Socket.io 配置它所缺少的。

const server = https.createServer(options, app);
const io = require('socket.io')(server, {
    cors: {
        origin: 'https://ags.test'
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM