gpt4 book ai didi

javascript - 无法连接到本地 Node.js 安全 WebSocketServer

转载 作者:太空宇宙 更新时间:2023-11-03 13:31:15 24 4
gpt4 key购买 nike

为了测试 JavaScript/html5 应用程序,我使用 node.js 和 ws package 创建了一个本地 WebSocketServer。 .我想使用带有 SSL/TLS 的安全网络套接字 (wss)。

key 和证书由 OpenSSL 在本地创建(自签名证书)用于测试目的。

客户端只是尝试使用 native WebSocket 对象连接到(本地)https 服务器:

var ws = new Websocket('wss://localhost:8080');

问题是,没有浏览器(Firefox、Chrome、Edge)可以连接到服务器,它们都给我不同的错误消息。

火狐:

Firefox can not connect to the server at wss: // localhost: 8080 /.

Chrome :

ws_client.js:7 WebSocket connection to 'wss://localhost:8080/' failed: Error in connection establishment: net::ERR_CERT_AUTHORITY_INVALID

边缘:

SCRIPT12017: SCRIPT12017: WebSocket Error: SECURITY_ERR, Cross zone connection not allowed

我在 OpenSSL(最新的轻量级版本)中创建了证书和 key ,如下所示:

openssl req -new -x509 -nodes -out server.crt -keyout server.key

(source)

我检查了几乎所有关于这个(和类似)主题的问题,例如this question , 但他们都无法提供解决方案。

请不要将此问题标记为重复,因为所有类似的问题都包含略有不同的问题!

服务器代码:

var fs = require('file-system');

var pkey = fs.readFileSync('server.key', 'utf8');
var crt = fs.readFileSync('server.crt', 'utf8');

var credentials = { key: pkey, cert: crt };
var https = require('https');

var httpsServer = https.createServer(credentials);
httpsServer.listen(8080);

var WebSocketServer = require('ws').Server;

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

wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
ws.send('reply from server : ' + message)
});

});

我尝试了另一个代码作为服务器,但出现了同样的错误:

const WebSocketServer = require('ws').Server; 
var fs = require('file-system');

var ws_cfg = {
ssl: true,
port: 8080,
ssl_key: 'server.key',
ssl_cert: 'server.crt'
};

var processRequest = function(req, res) {
console.log('Request received');
};

var httpServ = require('https');

var app = httpServ.createServer({
key: fs.readFileSync(ws_cfg.ssl_key, 'utf8', (error) => {
console.log('Error reading file');
}),
cert: fs.readFileSync(ws_cfg.ssl_cert, 'utf8', (error) => {
console.log('Error reading file');
})
}, processRequest).listen(ws_cfg.port, function(){
console.log('Server running');
});



var wss = new WebSocketServer( {server: app, port: 8080, host: 'localhost', domain: 'localhost'} );



wss.on('connection', function (ws) {

console.log('Connected to a client');

ws.on('message', function (message) {

console.log('MSG received: ' + message);

});

});

还有一件事。总是,如果我添加 console.log(wss); 到服务器代码,输出看起来像这样:

WebSocketServer {
domain: null,
...some more stuff...
...cert key etc....
host: null,
path: null,
port: null } }

主机、域和端口设置为 null。我尝试了所有方法将其设置为 localhost:8080,但没有成功。我认为这可能是所有问题的根源,但找不到办法。如果有人知道这个问题的答案,我将不胜感激。

(使用不安全的“ws”协议(protocol)(“ws://localhost:8080”)连接到本地 node.js http 服务器是可行的,但我想尽可能真实地测试应用程序,并且使用安全连接。)

最佳答案

-- 这不是答案,只是我的解决方法 --

对于遇到同样问题的人,这是我所做的:

服务器代码应该是:

const fs = require('fs');
const https = require('https');
const WebSocket = require('ws');

const server = new https.createServer({
cert: fs.readFileSync('localcert.cert'), //what ever you're files are called
key: fs.readFileSync('localkey.key')
});

const wss = new WebSocket.Server({ server }); // !

wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('MSG received: %s', message);
});

ws.send('Hi to client');
});


server.listen(8080);

目前只能在 Google Chrome 中使用,在 Firefox 中仍然无法连接。

在 Google Chrome 中输入 chrome://flags/#allow-insecure-localhost 并启用。

关于javascript - 无法连接到本地 Node.js 安全 WebSocketServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54151673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com