Skip to content

Instantly share code, notes, and snippets.

View FranciscoMarinho's full-sized avatar

Francisco Marinho FranciscoMarinho

View GitHub Profile
sendMessage() {
if (this.connection) {
this.connection.send(this.inputValue.nativeElement.value);
}
}
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 {
import { Component, OnInit, Inject, ViewChild, ElementRef, ChangeDetectorRef } from '@angular/core';
import { DOCUMENT } from '@angular/common';
declare const PresentationRequest: any;
@Component({
selector: 'app-emitter',
templateUrl: './emitter.component.html',
styleUrls: ['./emitter.component.scss']
})
@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);
});
});
}
this.connection.onmessage = (event) => this.handlerOnmessage(event);
handlerOnmessage(event) {
this.message = event.data;
this.changeDetector.detectChanges();
}
close() {
if (this.connection) {
this.connection.close();
}
}
terminate() {
if (this.connection) {
this.connection.terminate();
}
reconnect() {
if (!!this.connectionID) {
this.navigator.defaultRequest.reconnect(this.connectionID).then((connection: any) => {
this.connectionID = connection.id;
this.connection = connection;
}).catch( error => {
console.log('error ' + error.name + ': ' + error.message);
});
}
}
start() {
if (this.navigator.defaultRequest) {
this.navigator.defaultRequest.start().then((connection: any) => {
this.connectionID = connection.id;
this.connection = connection;
}).catch(error => {
console.log('error ' + error.name + ': ' + error.message);
});
}
if (this.navigator.defaultRequest) {
this.navigator.defaultRequest.getAvailability().then((availability: any) => {
if(availability){
this.buttonDisabled = availability.value
}
}).catch(error => {
console.log('error ' + error.name + ': ' + error.message);
this.buttonDisabled = true;
});
}
readonly receiveUrl = 'http://localhost:4200/receiver';
readonly presentationRequest;
navigator: any;
constructor(@Inject(DOCUMENT) private document: Document) {
this.navigator = this.document.defaultView.navigator;
try {
this.presentationRequest = new PresentationRequest(this.receiveUrl);
this.navigator.defaultRequest = this.presentationRequest;
} catch {