Notice: Undefined index: in /data/phpspider/phplib/misc.function.php on line 226
Node.js NodeJs-安全Web套接字和客户端连接_Node.js_Websocket - Fatal编程技术网

Node.js NodeJs-安全Web套接字和客户端连接

Node.js NodeJs-安全Web套接字和客户端连接,node.js,websocket,Node.js,Websocket,我需要在安全的websocket中转换带有websocket的应用程序。(在窗口下) 我使用nodeJs作为websocket服务器和一个简单的html页面来连接它 在google和此处搜索时,我发现了以下方法: 为服务器创建证书和密钥。我遵循了本教程: 创建.key和.pem后,我修改了我的nodejs websocket服务器以引入证书: const httpsOptions = { key: fs.readFileSync('./api/security/cert.key'),

我需要在安全的websocket中转换带有websocket的应用程序。(在窗口下) 我使用nodeJs作为websocket服务器和一个简单的html页面来连接它

在google和此处搜索时,我发现了以下方法:

为服务器创建证书和密钥。我遵循了本教程:

创建.key和.pem后,我修改了我的nodejs websocket服务器以引入证书:

const httpsOptions = {
   key: fs.readFileSync('./api/security/cert.key'),
   cert: fs.readFileSync('./api/security/cert.pem')
}

this._http              = require('http');
this._server = this._http.createServer(httpsOptions , function(req, res) { this.closeCurrentConnections(req,res)}.bind(this));

var serverConfig = {
      server: this._server,
      autoAcceptConnections: false
}
this._wsServer = new WebSocketServer(serverConfig);
当我启动nodejs时,Websocket服务器出现

现在,在客户端页面中,我有以下代码:

var websocket_server    = "ws://localhost:8128";
var echo_service        = new WebSocket(websocket_server,"echo-protocol"); 

[...]
我更改了它,使用以下代码通过HTTPS而不是简单的HTTP调用此页面:

var websocket_server    = "wss://localhost:8128";
var echo_service        = new WebSocket(websocket_server,"echo-protocol"); 

[...]
我在客户端页面上出错:

testing_page.html:283 WebSocket connection to 'wss://localhost:8128/' failed: Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR
我想是因为证书不见了。 我已尝试在chrome上导入我以前的创建证书,但无法导入,因为chrome使用的是.crt和/或其他格式。我试过强迫你,但没用

我错过了什么


只需尝试在Chrome中打开一个新选项卡
https://localhost:8128
和,然后按ADVANCED并转到localhost(不安全)。在客户端选项卡上重复连接后。您能确认在没有wss(ssl)的情况下一切正常吗?所以,如果问题出在ssl中,我建议您尝试使用ngrok()在本地计算机上进行需要ssl的测试。搜索解决方案时,我在代码中发现了一个错误。启动wss时,我需要https而不是http。这是。_http=require('https')。然后我必须尝试使用https而不是wss通过浏览器连接,并接受不安全的连接。现在错误不同了:失败:WebSocket握手期间出错:意外响应代码:404