简体   繁体   English

要求 function 在 Asp.netcore javascript 中不起作用

[英]require function is not working in Asp.netcore javascript

I tried to implement WebRTC simple-peer video chat application using .net core MVC.我尝试使用 .net 核心 MVC 实现 WebRTC 简单对等视频聊天应用程序。 But when writing javascript implementation that needs to make peer connection, they have used require("xxx") function and I am getting an error that required is not defined.但是在编写需要建立对等连接的 javascript 实现时,他们使用了 require("xxx") function 并且我收到了一个未定义所需的错误。 I know this needs node or requirejs.我知道这需要 node 或 requirejs。 I have tried installing the node also but it is not working.我也尝试过安装节点,但它不工作。

 1)let Peer = require('simple-peer');
   let p = new Peer()

can anyone tell me how to load the required function in .netcore MVC project javascript files?谁能告诉我如何在 .netcore MVC 项目 javascript 文件中加载所需的 function 文件? (I have also use SignalR) (我也使用过 SignalR)

is the module in present in your package.json file?您的 package.json 文件中是否存在该模块?

if not try:如果不尝试:

npm i simple-peer --save-dev

(also check if actually you are getting the name of the node module correctly) (还要检查您是否确实正确获取了节点模块的名称)

Your Code Should Look Like This你的代码应该是这样的

 let Peer = require('simple-peer'); var peer = new Peer({ initiator: true, trickle: false })

 var stream;

 navigator.mediaDevices.getUserMedia({ video: true, audio: false }, function (stream) {
        stream = stream;
 });

function InitPeer(type) {
                var peer = new SimplePeer({
                    initiator: (type == 'init') ? true : false,
                    stream: stream,
                    trickle: false
                });

So I had the issue to create the simple peer object in using standard java script if we use node.js we would be able to create the simple peer object by using reqired() So I had the issue to create the simple peer object in using standard java script if we use node.js we would be able to create the simple peer object by using reqired()

var Peer = require('simple-peer');

but I can't use this in standard java script so if I create like this但我不能在标准 java 脚本中使用它,所以如果我这样创建

var peer = new SimplePeer({
                        initiator: (type == 'init') ? true : false,
                        stream: stream,
                        trickle: false
                    });

stream will give an error telling that stream is not recognize. stream 将给出一个错误,提示 stream 无法识别。 So to avoid that need to create another variable and assign the streaming inside所以为了避免需要创建另一个变量并分配里面的流

 navigator.mediaDevices.getUserMedia({ video: true, audio: false }, function (stream) {
            stream = stream;
     });

So now you can use.所以现在可以使用了。 This part is important if you are going to implement video chat as we need streaming如果您要实现视频聊天,这部分很重要,因为我们需要流式传输

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

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