簡體   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