Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在nodejs/javascript中通过websocket建立安全的客户端/服务器连接?_Javascript_Node.js_Http_Ssl_Websocket - Fatal编程技术网

如何在nodejs/javascript中通过websocket建立安全的客户端/服务器连接?

如何在nodejs/javascript中通过websocket建立安全的客户端/服务器连接?,javascript,node.js,http,ssl,websocket,Javascript,Node.js,Http,Ssl,Websocket,你可能认为这个问题可以标记为重复,但我到处搜索,没有找到我要找的 我正在开发一个ionic react应用程序,并使用nodejs服务器作为后端。我正在通过websocket将客户端连接到服务器。我在客户端使用https内置模块,在服务器端使用express框架 当我使用http或ws连接时,一切正常。现在,我的应用程序已经准备好投入生产,我正在切换到https或wss,因为我想使用websocket。我是第一次这样做,所以这让我很困惑 这是我的客户端代码(我将secure改为true,因为我读

你可能认为这个问题可以标记为重复,但我到处搜索,没有找到我要找的

我正在开发一个ionic react应用程序,并使用nodejs服务器作为后端。我正在通过websocket将客户端连接到服务器。我在客户端使用https内置模块,在服务器端使用express框架

当我使用httpws连接时,一切正常。现在,我的应用程序已经准备好投入生产,我正在切换到https或wss,因为我想使用websocket。我是第一次这样做,所以这让我很困惑

这是我的客户端代码(我将secure改为true,因为我读到如果我计划使用https/ssl,我应该这样做):

这是我的服务器代码:

var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
var ws = require('ws').Server;
// This line is from the Node.js HTTPS documentation.
var options = {
  key: fs.readFileSync('key.pem').toString(),
  cert: fs.readFileSync('cert.pem').toString(),

};

var app = express();
var httpsServer = https.createServer(options, app);

var wss = new ws({
    server: httpsServer
});


wss.on("connection", (socket) => {
    console.log("client connected");

    socket.on("message", (msg) => {
        console.log("received: ", msg);
    });

    socket.on("disconnection", () => {
        console.log("client disconnected");
    })
});
httpsServer.listen(8000, () => {
    console.log("server running on 8000");
})
我通过以下方式生成ssl证书。 当我使用http时,我的代码可以完美地工作,但是当我使用https/wss时,突然出现了一个错误,尽管我添加了ssl证书

有趣的是,我在chrome中遇到的错误与firefox中的错误不同。下面是我在chrome中遇到的错误(它甚至看起来像两个独立的错误):

以下是我在firefox中遇到的错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://localhost:8000/socket.io/?EIO=3&transport=polling&t=NFuR-Im. (Reason: CORS request did not succeed).
我尝试并禁用了我所有的borwser插件(例如adblock),但没有成功。我进一步尝试设置标题并配置cors(我在互联网上阅读有关cors错误的信息时发现):


但是,添加此代码并没有改变任何内容,错误仍然出现。我不知道该怎么办。也许这是一个愚蠢的问题,但我是否需要以某种方式在客户端提供证书?这是客户端问题还是服务器端问题?

您是否也信任生成的自签名证书?@AntoineRaoulIscaros我不这么认为。对不起,我对这个很陌生。我甚至不知道如何信任它,也不知道它在现实中意味着什么context@AntoineRaoulIscaros所以我在网上读到了关于信任的事情。事实证明,我只需要在生产环境中使用它,但在我的例子中,我正在本地主机上尝试使用它,但它甚至不起作用。正确吗?这取决于你是如何签署证书的。https网站对你有用吗?你是否也信任你生成的自签名证书?@AntoineRaoulIscaros我不这么认为。对不起,我对这个很陌生。我甚至不知道如何信任它,也不知道它在现实中意味着什么context@AntoineRaoulIscaros所以我在网上读到了关于信任的事情。事实证明,我只需要在生产环境中使用它,但在我的例子中,我正在本地主机上尝试使用它,但它甚至不起作用。正确吗?这取决于您如何签署证书。https网站是否适合您?
    Access to XMLHttpRequest at 'https://localhost:8000/socket.io/?EIO=3&transport=polling&t=NFuRgsF' from origin 'http://localhost:8100' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
    polling-xhr.js:268 

GET https://localhost:8000/socket.io/?EIO=3&transport=polling&t=NFuRgsF net::ERR_FAILED 
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://localhost:8000/socket.io/?EIO=3&transport=polling&t=NFuR-Im. (Reason: CORS request did not succeed).
app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    //res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    
    next();
  });