简体   繁体   English

部署站点时,我无权访问 navigator.mediaDevices。 我该如何解决?

[英]I do not have have access to navigator.mediaDevices when the site is deployed. How do I fix this?

I am currently trying to use the WebRTC api and have everything working locally.我目前正在尝试使用 WebRTC api 并使所有内容都在本地工作。 When I deploy to surge.sh I lose access to the navigator.mediaDevices object.当我部署到surge.sh我无法访问navigator.mediaDevices对象。 How can I resolve this?我该如何解决这个问题?

The following line of code is where I am having trouble with.以下代码行是我遇到问题的地方。

const stream = await navigator.mediaDevices.getDisplayMedia({video: {mediaSource: 'screen'}});

I receive the following error message:我收到以下错误消息:

TypeError: Cannot read property 'getDisplayMedia' of undefined

You need https.你需要https。

navigator.mediaDevices is only available in SecureContext now in Chrome 74 , Firefox 68 , and in the spec , which means the object will be missing in insecure contexts (http). navigator.mediaDevices现在仅在 Chrome 74 、Firefox 68spec中的 SecureContext 中可用,这意味着该对象将在不安全的上下文 (http) 中丢失。

As jib answered, navigator.mediaDevices is not available when webserver uses HTTP scheme while not hosted on localhost , but with an exception in chrome/chromium:正如jib回答的那样,当网络服务器使用 HTTP 方案而不托管在localhost上时, navigator.mediaDevices不可用,但在 chrome/chromium 中除外:

$ chromium --unsafely-treat-insecure-origin-as-secure=http://WEBSERVER_FQDN:PORT_NUMBER

--unsafely-treat-insecure-origin-as-secure command line option allows to loosen the restriction. --unsafely-treat-insecure-origin-as-secure命令行选项允许放宽限制。 It's useful for developing/debugging when HTTPS is not set up yet or you need to analyze raw unencrypted HTTP messages (ie wireshark).当尚未设置 HTTPS 或您需要分析原始未加密的 HTTP 消息(即wireshark)时,它对于开发/调试很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM