我正在使用 ionic 5 构建一个视频聊天应用程序,我不知道如何正确使用它,我尝试过,但我不断在控制台中收到此错误
Uncaught ReferenceError: parcelRequire is not defined
at push../node_modules/peerjs/dist/peerjs.min.js.parcelRequire.EgBh (peerjs.min.js:1)
at Object../node_modules/peerjs/dist/peerjs.min.js
这是我的代码
import { Injectable } from '@angular/core';
import Peer from 'peerjs';
@Injectable({
providedIn: 'root'
})
export class WebrtcService {
peer: Peer;
myStream: MediaStream;
myEl: HTMLMediaElement;
partnerEl: HTMLMediaElement;
options:any= {
host: 'localhost',
port: 9000,
path: '/peerjs',
debug: 3,
config: {
'iceServers': [{
url: 'stun:stun1.l.google.com:19302'
},{
url: 'turn:numb.viagenie.ca',
credential: 'muazkh',
username: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7403111606001734181d02115a171b19" rel="noreferrer noopener nofollow">[email protected]</a>'
}
]
}
}
constructor() {}
getMedia() {
navigator.getUserMedia({ audio: true, video: true },stream=>this.handleSuccess(stream),
(error) =>this.handleError(error));
}
async init(userId: string, myEl: HTMLMediaElement, partnerEl: HTMLMediaElement) {
this.myEl = myEl;
this.partnerEl = partnerEl;
try {
this.getMedia();
} catch (e) {
this.handleError(e);
}
await this.createPeer(userId);
}
async createPeer(userId: string) {
this.peer = new Peer(userId, this.options);
this.peer.on('open', ()=>this.wait())
}
call(partnerId: string) {
const call = this.peer.call(partnerId, this.myStream);
call.on('stream', (stream)=>this.partnerEl.srcObject = stream);
}
wait() {
this.peer.on('call', (call) => {
call.answer(this.myStream);
call.on('stream', (stream) =>this.partnerEl.srcObject = stream);
});
}
注释掉大部分代码以找出错误的来源,我注意到在运行 this.peer = new Peer(userId, this.options);
时会发生错误。
我还注意到,如果我应该在其他页面中运行它,它会起作用,但是如果它在 app.component.ts 中运行,它会给我这个错误,我真的需要上面的代码在应用程序中.component.ts
最佳答案
关于angular - 如何删除在ionic中peerjs没有定义parcelRequire,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62956200/