繁体   English   中英

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

[英]Socket.io Letsencrypt and a secure connection failing on CORS

我有一个简单的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提供的页面进行连接, 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:// url - 所有这些都导致相同的CORS错误。

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

有人可以指出我正确的方向,以便我可以允许我的客户通过https或理想的httphttps

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

然后在你的nodejs app中:

// require cors
const cors = require("cors");
// Use Cors As Middleware
app.use(cors());

暂无
暂无

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

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