node.js - 基于 HTTPs 的 WebRTC 问题

标签 node.js webrtc

我创建了一个简单的 WebRTC 应用程序,可以很好地测试本地主机;然而,除非您有安全连接,否则 WEBRTC 没有多大用处,因为浏览器现在不会运行 GetUserMedia,除非您有 HTTP,所以我“尝试”将其升级为 SSL-TLS。下面是我的两个应用程序并排的屏幕截图,一个安全(不工作)另一个不安全(工作)

enter image description here

正如您在上面看到的,本地主机“连接”,而 HTTPs“无法建立连接”。我是 SSL 新手,所以这可能很简单。只是希望更多人关注这一点。

我确实知道我的 JavaScript HTTPS 服务器正在安全连接,请参见下图 enter image description here

下面是我的代码片段。任何帮助将不胜感激:

SSL 客户端 - Client.JS

var connection = new WebSocket('wss://localhost:8443'),

name = "";

非安全客户端 - Client.JS

var connection = new WebSocket('ws://localhost:8888'),

    name = "";

非安全 JS 服务器 - index.JS

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

wss = new WebSocketServer({ port: 8888 }),

users = {};
wss.on('connection', function (connection) {
connection.on('message', function (message) .....

安全 JS 服务器 - SSLindex.JS

 Var https = require('https'),

fs = require('fs'),

 express = require('express'),

  app = express();


var wss = https.createServer({
key: fs.readFileSync('server.key'),

cert: fs.readFileSync('server.crt'),

ca: fs.readFileSync('ca.crt'),

requestCert: true,

rejectUnauthorized: false
 }, app).listen('8443', function() {
console.log("Secure Express server listening on port 8443");
});

 app.use("/", function(req, res, next) 
{
 res.send("index.js is working!");

  });

最佳答案

为了方便开发人员,chrome 会将 localhost 视为安全源。
如果您使用http://localhost或 ws://localhost 不涉及 SSL,因此它在右侧屏幕截图中工作得很好。
但对于httpswss,浏览器将验证SSL证书颁发机构。

To resolve websocket connection issue with wss://localhost:8443/

临时解决办法:打开 https://localhost:8443/ url 并通过单击 Adavced => 继续
允许浏览器异常(exception) 实际解决方案:为您的 Node 服务器分配域名(node.kali2016.com),并使用授权的 SSL 证书配置 Node 。然后替换
var connection = new WebSocket('wss://localhost:8443')

var connection = new WebSocket('wss://yourdomainname:8443')

关于node.js - 基于 HTTPs 的 WebRTC 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43901648/

相关文章:

node.js - 如何使用 NGINX 访问写入 docker 容器卷上的文件并将 URL 共享到客户端

webrtc - Peerjs 不断失去连接并且用户 ID 丢失

webrtc - Google Meet : WebRTC peer-to-peer and Speech to Text

html - 保存通过 webrtc peerConnection 创建的视频/ Audio Session

c++ - 使用 Qt WebEngine 自动授予对麦克风和摄像头的访问权限

javascript - 如何使用 Promise 来解决这个问题?

javascript - 在 puppeteer/JavaScript 中重试 page.goto、page.waitForNavigation 等的最佳实践

ruby-on-rails - 在 PPC linux (lubuntu 12.04) 上安装 Rails

mysql - NodeJS 无法连接到 Docker 容器内的 MYSQL

javascript - 动态创建的视频元素上的 captureStream()