X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fangular%2Fvideos%2Fcomponents%2Fwatch%2Fvideos-watch.component.ts;h=6e212e8bc8afa72ec69fa9ba977d434c4ec3a6fd;hb=8140a704bbbecd149c68267545e4215579c9785c;hp=e3a9738203e7451c682abde49effd585af5165a2;hpb=dc8bc31be517a53e8fbe7100cfe45cd73f596de0;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/angular/videos/components/watch/videos-watch.component.ts b/client/angular/videos/components/watch/videos-watch.component.ts index e3a973820..6e212e8bc 100644 --- a/client/angular/videos/components/watch/videos-watch.component.ts +++ b/client/angular/videos/components/watch/videos-watch.component.ts @@ -1,22 +1,29 @@ -/// +import { Component, OnInit, ElementRef } from '@angular/core'; +import { RouteParams, CanDeactivate, ComponentInstruction } from '@angular/router-deprecated'; -import { Component, OnInit, ElementRef } from 'angular2/core'; -import { RouteParams } from 'angular2/router'; +import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe'; +// TODO import it with systemjs declare var WebTorrent: any; -import { Video } from '../../models/video'; -import { VideosService } from '../../services/videos.service'; +import { Video } from '../../video'; +import { VideosService } from '../../videos.service'; @Component({ selector: 'my-video-watch', templateUrl: 'app/angular/videos/components/watch/videos-watch.component.html', - styleUrls: [ 'app/angular/videos/components/watch/videos-watch.component.css' ] + styleUrls: [ 'app/angular/videos/components/watch/videos-watch.component.css' ], + pipes: [ BytesPipe ] }) -export class VideosWatchComponent { +export class VideosWatchComponent implements OnInit, CanDeactivate { video: Video; + downloadSpeed: number; + uploadSpeed: number; + numPeers: number; + loading: boolean = false; + private _interval: NodeJS.Timer; private client: any; constructor( @@ -24,6 +31,7 @@ export class VideosWatchComponent { private _routeParams: RouteParams, private _elementRef: ElementRef ) { + // TODO: use a service this.client = new WebTorrent({ dht: false }); } @@ -36,15 +44,32 @@ export class VideosWatchComponent { } loadVideo(video: Video) { + this.loading = true; this.video = video; - + console.log('Adding ' + this.video.magnetUri + '.'); this.client.add(this.video.magnetUri, (torrent) => { - torrent.files[0].appendTo(this._elementRef.nativeElement, (err) => { + this.loading = false; + console.log('Added ' + this.video.magnetUri + '.'); + torrent.files[0].appendTo(this._elementRef.nativeElement.querySelector('.embed-responsive'), (err) => { if (err) { alert('Cannot append the file.'); console.error(err); } - }) - }) + }); + + // Refresh each second + this._interval = setInterval(() => { + this.downloadSpeed = torrent.downloadSpeed; + this.uploadSpeed = torrent.uploadSpeed; + this.numPeers = torrent.numPeers; + }, 1000); + }); + } + + routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any { + console.log('Removing video from webtorrent.'); + clearInterval(this._interval); + this.client.remove(this.video.magnetUri); + return true; } }