我正在研究 WebRTC 并在不同的浏览器页面上连接两个对等点,一个作为发起者的对等点带有这个 url http://localhost:4200/#init
,另一个是视频接收器带有这个 url http://localhost:4200/
。建立成功连接后,第一个对等方与另一个对等方共享其视频,但我面临的问题是其他对等方没有播放或接收视频。
export class SimpPeerComponent implements OnInit {
targetpeer: any;
peer: any;
stream: MediaStream
async ngOnInit() {
try {
// This peer is the initiator and transfering the streaming to the other connected peer
if (location.hash === '#init') {
let stream = await navigator.mediaDevices.getUserMedia({ video: true })
this.peer = new SimplePeer({
initiator: location.hash === '#init',
stream: stream
})
}
else {
this.peer = new SimplePeer()
}
// triggers when signal is sent from remote
this.peer.on('signal', function (data) {
console.log(JSON.stringify(data));
})
this.peer.on('data', (data) => {
console.log('Received Data: ' + data)
})
this.peer.on('stream', (stream) => {
// got remote video stream, now let's show it in a video tag
this.videoElement.srcObject = stream
})
} catch (error) {
console.log(error)
}
}
connect() {
this.peer.signal(this.targetpeer);
}
message() {
this.peer.send('Hello world');
}
@ViewChild('myvideo') videoElementRef: ElementRef;
get videoElement(): HTMLVideoElement {
return this.videoElementRef.nativeElement
}
}
<div class="row">
<div class="col d-flex justify-content-center">
<video #myvideo autoplay controls class="video mb-2"></video>
</div>
</div>
<div class="row">
<div class="col d-flex justify-content-center">
<input type="text" class="form-control-sm mr-1" [(ngModel)]="targetpeer">
<button class="btn btn-success" (click)="connect()">Connect</button>
<button class="btn btn-primary" (click)="message()">Send</button>
</div>
</div>
可能是什么问题?请指导!!!