在节点中运行时如何阻止未定义的导航器(HTML5)

时间:2019-06-11 02:08:44

标签: html node.js webrtc

运行webrtc节点服务时出现错误。尝试使用导航器时出现错误。这是运行时的错误 node client.js

navigator.mediaDevices.getUserMedia = navigator.mediaDevices.getUserMedia || navigator.mediaDevices.webkitGetUserMedia
   ^

ReferenceError: navigator is not defined

我正在使用angular和node运行其中包含webrtc的文件。这是我的设置:

Angular CLI: 8.0.1
Node: 10.16.0
Angular: 8.0.0

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.800.1
@angular-devkit/build-angular     0.800.1
@angular-devkit/build-optimizer   0.800.1
@angular-devkit/build-webpack     0.800.1
@angular-devkit/core              8.0.1
@angular-devkit/schematics        8.0.1
@angular/cdk                      7.0.4
@angular/cli                      8.0.1
@angular/http                     6.0.9
@angular/material                 6.4.7
@ngtools/webpack                  8.0.1
@schematics/angular               8.0.1
@schematics/update                0.800.1
rxjs                              6.5.2
typescript                        3.4.5
webpack                           4.30.0

这是我的client.js代码:

//检查浏览器是否支持WebRTC

function hasUserMedia() { 
   navigator.mediaDevices.getUserMedia = navigator.mediaDevices.getUserMedia || navigator.mediaDevices.webkitGetUserMedia 
      || navigator.mediaDevices.mozGetUserMedia || navigator.mediaDevices.msGetUserMedia; 
   return !!navigator.mediaDevices.getUserMedia; 
}

if (true) { 
   navigator.mediaDevices.getUserMedia = navigator.mediaDevices.getUserMedia || navigator.mediaDevices.webkitGetUserMedia
      || navigator.mediaDevices.mozGetUserMedia || navigator.mediaDevices.msGetUserMedia;

   //get both video and audio streams from user's camera 
   navigator.mediaDevices.getUserMedia({ video: true, audio: true }, function (stream) { 
      var video = document.querySelector('video'); 

      //insert stream into the video tag 
      video.src = window.URL.createObjectURL(stream); 
   }, function (err) {}); 

}else {
   alert("Error. WebRTC is not supported!"); 
}

由于开始看到有关WebRTC新版本的帖子,我开始添加navigator.mediaDevice。我不确定是否每种方法都需要它。

如果社区认为WebRTC不是首选方式,请告诉我。我仍在尝试为直播应用程序找到最佳选择。但是我认为WebRTC是最好的。

0 个答案:

没有答案