Socket.io Letsencrypt和CORS上的安全连接失败

问题描述 投票:0回答:1

我有一个简单的node.js设置运行socket.io> 1.0.0;

fs = require('/home/node/bin/node_modules/file-system');

        // Options for socket.io > 1.0.0
        var options = {
                allowUpgrades: true,
                transports: [ 'polling', 'websocket' ],
                pingTimeout: 6000,
                pingInterval: 3000,
                cookie: 'nom-nom',
                httpCompression: true,
                key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
                cert: fs.readFileSync('/etc/letsencrypt/live/example.com/fullchain.pem'),
                origins: '*:*'
        };

        io = require('/home/node/bin/node_modules/socket.io')(8000, options);

当客户端从通过http提供的页面连接时,它们能够按预期连接并允许套接字连接。如果客户端尝试从通过https提供的页面进行连接,则会出现以下错误;

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://example.com:8000/socket.io/?EIO=3&transport=polling&t=MfVDWxD. (Reason: CORS request did not succeed).

鉴于我的CORS政策已经为*:*设定,应该允许任何我不明白为什么它失败。

我已经尝试将{secure: true}添加到客户端连接,我也尝试强制连接字符串的wss://https://网址 - 所有这些都导致相同的CORS错误。

这是一个裸套接字实现,不使用任何框架,如express或甚至http。

有人可以指出我正确的方向,以便我可以让我的客户通过https或理想的httphttps连接。

node.js sockets socket.io cors socket.io-1.0
1个回答
0
投票

你需要安装cors,你可以用npm或任何其他包经理来做

然后在你的nodejs app中:

// require cors
const cors = require("cors");
// Use Cors As Middleware
app.use(cors());
© www.soinside.com 2019 - 2024. All rights reserved.