切换到安全协议后无法连接到 websocket

Can't connect to websocket after switching to secured protocol

我已经构建了一个以 NodeJS/Express 作为后端的 Web 应用程序。前端使用 websocket 协议进行连接。一切正常。

现在,我要切换到 wss:// 协议,据我所知,根据 this blog post,该协议应该像

一样简单

生成自签名证书:

openssl req -nodes -new -x509 -keyout certificate.key -out certificate.cert

后端:

// old
// import * as http from 'http';
// new
import * as https from 'https';

// ...

// old
// const server = http.createServer(app);
// new
const server = https.createServer({
  key: fs.readFileSync('certificate.key'),
  cert: fs.readFileSync('certificate.cert'),
}, app);

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

// ...

前端(Angular 使用 RxJS):

// old
// private readonly backend$$ = webSocket<UpdateData<any>>('ws://localhost:8999');
// new
private readonly backend$$ = webSocket<UpdateData<any>>('wss://localhost:8999');

但是,进行这些更改后,我的前端无法再连接到 websocket。

我收到以下错误(由于某种原因我无法从控制台完全复制):

我需要知道这里的问题是什么/可能是什么,或者关于要检查什么来解决问题的任何指示。

问题与自签名证书有关,浏览器(至少是 Firefox、Chrome 和 Edge)在这种情况下默认不允许。

我可以通过直接访问 https://localhost:8999 来规避这个问题,它会显示众所周知的无效证书警告消息。通过点击它,证书被添加为例外(至少是暂时的),并且访问我的实际 Angular 应用程序现在也可以成功连接到 websocket。

Here是关于如何在Firefox中永久添加自签名证书的说明。我希望其他浏览器也能类似地工作。