建立WebSocket连接时出现错误

8
我在服务器端使用ws npm,在客户端使用 websocket。

当在 node-js 上运行此代码时,它可以正常工作,但从浏览器运行时会出现以下错误:

failed: Error in connection establishment: net::ERR_CERT_COMMON_NAME_INVALID
const ws = new WebSocket('wss://domain:port', null, { 
  rejectUnauthorized: false 
});

ws.onerror = function (e) {
  console.log(e)
}

ws.onclose = function (e) {
  console.log(e)
}

ws.onopen = function () {
  console.log('connected ')
  ws.send(JSON.stringify({ msg: 'msg' }));
}

1
尝试使用 'ws://domain:port' - evgeni fotia
我的域名正在使用https运行。 当使用ws时,错误提示应该使用wss。 - HaYa Abu Al Halawa
1个回答

1
net::ERR_CERT_COMMON_NAME_INVALID 看起来是证书问题。浏览器使用各种证书,例如您可以在Mozilla Firefox的首选项>隐私与安全>证书中查看证书,并探索哪个证书导致了问题。
确保您的API指向正确的证书。

非常感谢,我使用了错误的证书。 - HaYa Abu Al Halawa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接