Skip to content

Commit

Permalink
2
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvaro Ramirez Castillo authored and Alvaro Ramirez Castillo committed Feb 10, 2024
1 parent 91c5bfd commit 0ab0c3d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/app/components/host/host.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ <h1>host</h1>
<button (click)="syncWithRemote()">sync</button>
</div>
<div>
<p>{{message()}}</p>
<input type="text" [formControl]="messageControl" />
<button (click)="sendDataToRemote()">send</button>
</div>
11 changes: 9 additions & 2 deletions src/app/components/host/host.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, signal, ChangeDetectorRef, inject } from '@angular/core';
import { ReactiveFormsModule, FormControl } from '@angular/forms';
import { Host } from './host';

Expand All @@ -9,15 +9,17 @@ import { Host } from './host';
templateUrl: './host.component.html'
})
export class HostComponent implements OnInit {
private changeDetector = inject(ChangeDetectorRef);
private host = new Host();
offer = '';
candidate = '';
message = signal('');
messageControl = new FormControl('');
remoteOfferControl = new FormControl('');
remoteCandidateControl = new FormControl('');

async ngOnInit() {
const { offer, candidate } = await this.host.createConnection();
const { offer, candidate } = await this.host.createConnection({ onReceiveMessageCallback: event => this.onReceiveMessageCallback(event) });
this.offer = JSON.stringify(offer);
this.candidate = JSON.stringify(candidate);
}
Expand All @@ -32,4 +34,9 @@ export class HostComponent implements OnInit {
const message = this.messageControl.value || '';
this.host.sendDataToRemote(message);
}

private onReceiveMessageCallback(event: MessageEvent) {
this.message.set(event.data);
this.changeDetector.detectChanges();
}
}
13 changes: 9 additions & 4 deletions src/app/components/host/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ export class Host {
private readonly CHANNEL = 'SendDataChannel';
private peerConnection: RTCPeerConnection;
private offer!: RTCSessionDescriptionInit;
private sendChannel!: RTCDataChannel;
private channel!: RTCDataChannel;

constructor() {
this.peerConnection = new RTCPeerConnection();
}

createConnection() {
createConnection({ onReceiveMessageCallback }: { onReceiveMessageCallback: (event: MessageEvent) => void }) {
return new Promise<{ offer: RTCSessionDescriptionInit, candidate: RTCIceCandidateInit }>(async (resolve) => {
this.createChannel();
this.receiveChannel({ onReceiveMessageCallback });
this.peerConnection.onicecandidate = ({ candidate }) => {
if (candidate) {
resolve({
Expand All @@ -23,13 +24,17 @@ export class Host {
});
}

private receiveChannel({ onReceiveMessageCallback }: { onReceiveMessageCallback: (event: MessageEvent) => void }) {
this.channel.onmessage = event => { onReceiveMessageCallback(event) };
}

private async createOffer() {
this.offer = await this.peerConnection.createOffer();
this.peerConnection.setLocalDescription(this.offer);
}

private createChannel() {
this.sendChannel = this.peerConnection.createDataChannel(this.CHANNEL);
this.channel = this.peerConnection.createDataChannel(this.CHANNEL);
}

syncWithRemote({ offer, candidate }: { offer: RTCSessionDescriptionInit; candidate: RTCIceCandidateInit }) {
Expand All @@ -38,6 +43,6 @@ export class Host {
}

sendDataToRemote(data: string) {
this.sendChannel.send(data);
this.channel.send(data);
}
}
9 changes: 5 additions & 4 deletions src/app/components/remote/remote.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Remote } from './remote';
})
export class RemoteComponent {
private changeDetector = inject(ChangeDetectorRef);
private remote = new Remote();
message = signal('');
messageControl = new FormControl('');
hostOfferControl = new FormControl('');
Expand All @@ -20,19 +21,19 @@ export class RemoteComponent {
async syncWithHost() {
const hostOffer = JSON.parse(this.hostOfferControl.value || '') as RTCSessionDescriptionInit;
const hostCandidate = JSON.parse(this.hostCandidateControl.value || '') as RTCIceCandidateInit;
const remote = new Remote();
const { candidate, offer } = await remote.createConnection({ offer: hostOffer, onReceiveMessageCallback: event => this.onReceiveMessageCallback(event) })
const { candidate, offer } = await this.remote.createConnection({ offer: hostOffer, onReceiveMessageCallback: event => this.onReceiveMessageCallback(event) })

this.offer = JSON.stringify(offer);
this.candidate = JSON.stringify(candidate);
remote.syncWithHost({ candidate: hostCandidate });
this.remote.syncWithHost({ candidate: hostCandidate });
}

sendDataToRemote() {
const message = this.messageControl.value || '';
this.remote.sendDataToRemote(message);
}

private onReceiveMessageCallback(event: MessageEvent) {
console.log('Received Message 11111', event.data);
this.message.set(event.data);
this.changeDetector.detectChanges();
}
Expand Down
6 changes: 6 additions & 0 deletions src/app/components/remote/remote.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export class Remote {
private peerConnection: RTCPeerConnection;
private offer!: RTCSessionDescriptionInit;
private channel!: RTCDataChannel;

constructor() {
this.peerConnection = new RTCPeerConnection();
Expand Down Expand Up @@ -30,10 +31,15 @@ export class Remote {
private receiveChannel({ onReceiveMessageCallback }: { onReceiveMessageCallback: (event: MessageEvent) => void }) {
this.peerConnection.ondatachannel = event => {
const { channel } = event;
this.channel = channel;
channel.onmessage = event => { onReceiveMessageCallback(event) };
};
}

sendDataToRemote(data: string) {
this.channel.send(data);
}

syncWithHost({ candidate }: { candidate: RTCIceCandidateInit }) {
this.peerConnection.addIceCandidate(candidate);
}
Expand Down

0 comments on commit 0ab0c3d

Please sign in to comment.