Created
January 21, 2021 22:21
-
-
Save FranciscoMarinho/839f17ccd2b81fb34b2af1ad115b5aca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { DOCUMENT } from '@angular/common'; | |
| import { ChangeDetectorRef, Component, ElementRef, HostListener, Inject, OnInit, ViewChild } from '@angular/core'; | |
| @Component({ | |
| selector: 'app-receiver', | |
| templateUrl: './receiver.component.html', | |
| styleUrls: ['./receiver.component.scss'] | |
| }) | |
| export class ReceiverComponent implements OnInit { | |
| @ViewChild('inputValue') inputValue:ElementRef; | |
| navigator: any; | |
| connection; | |
| event: Event; | |
| message; | |
| constructor(@Inject(DOCUMENT) private document: Document, | |
| private changeDetector: ChangeDetectorRef) { | |
| this.navigator = this.document.defaultView.navigator; | |
| } | |
| ngOnInit(): void { | |
| } | |
| @HostListener('window:DOMContentLoaded', ['$event']) | |
| receiveInit() { | |
| if (this.navigator.presentation.receiver) { | |
| this.navigator.presentation.receiver.connectionList.then((connectionList:any) => { | |
| connectionList.connections.map((connection: any) => { | |
| this.connection = connection; | |
| this.connection.onmessage = (event: MessageEvent) => this.handlerOnmessage(event); | |
| }); | |
| }); | |
| } | |
| } | |
| handlerOnmessage(event: MessageEvent) { | |
| this.message = event.data; | |
| this.changeDetector.detectChanges(); | |
| } | |
| sendMessage() { | |
| if (this.connection) { | |
| this.connection.send(this.inputValue.nativeElement.value); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment