2016-12-30 118 views
3

新的observables。我正在使用ssh2來獲取服務器上的文件/文件夾列表。我無法弄清楚如何讓我回來的數據作爲觀察到的,因爲所有的在線使用的例子http,而不是一個第三方模塊。如何使異步數據可觀察?

如何設置此服務,以便我可以將列表恢復爲可觀察狀態?

import {Injectable, NgZone} from '@angular/core'; 
import {Observable} from 'rxjs'; 
var Client = require('ssh2').Client; 
var user = require('credentials.json') 
@Injectable() 
export class ConnectionService { 
    public connSettings:any; 

    constructor(private zone: NgZone){ 
     this.connSettings = { 
      host: user.url, 
      username: user.username, 
      password: user.password 
     }; 
     this.openConnection(); 

    } 
    openConnection() { 
     let remotePathToList = '/home/user'; 
     var conn = new Client(); 

     conn.on('ready',()=>{ 
      conn.sftp((err:any, sftp:any)=>{ 
       if (err) throw err; 
       sftp.readdir(remotePathToList, (err:any, list:any)=>{ 
        conn.end(); 

        //return observable data here 

        return list; 

       }) 
      }) 
     }).connect(this.connSettings); 

    } 


} 

回答

1

如果你想有一個通用屬性在任何時間(無關openConnection() - 方法訪問列表,你可以使用一個ReplaySubject

@Injectable() 
export class ConnectionService { 
    public yourList$: ReplaySubject<any> = new ReplaySubject<any>(1); 
    public connSettings:any; 

    constructor(private zone: NgZone){ 
     this.connSettings = { 
      host: user.url, 
      username: user.username, 
      password: user.password 
     }; 
     this.openConnection(); 

    } 
    openConnection() { 
     let remotePathToList = '/home/user'; 
     var conn = new Client(); 

     conn.on('ready',()=>{ 
      conn.sftp((err:any, sftp:any)=>{ 
       if (err) throw err; 
       sftp.readdir(remotePathToList, (err:any, list:any)=>{ 
        conn.end(); 

        //return observable data here 
        yourList$.next(list); 
        return list; 

       }) 
      }) 
     }).connect(this.connSettings); 
    } 
} 

如果你想只將其作爲退回的openConnection的可觀察者您可以創建自定義Observable

@Injectable() 
export class ConnectionService { 
    public yourList$: ReplaySubject<any> = new ReplaySubject<any>(1); 
    public connSettings:any; 

    constructor(private zone: NgZone){ 
     this.connSettings = { 
      host: user.url, 
      username: user.username, 
      password: user.password 
     }; 
     this.openConnection(); 

    } 
    openConnection(): Observable<any> { 
     return Observable.create(obs => { 
      let remotePathToList = '/home/user'; 
      var conn = new Client(); 

      conn.on('ready',()=>{ 
       conn.sftp((err:any, sftp:any)=>{ 
        if (err) throw err; 
        sftp.readdir(remotePathToList, (err:any, list:any)=>{ 
         conn.end(); 

         //return observable data here 
         obs.next(list); 
         obs.complete(); // required, otherwise your subscription would never close 
        }) 
       }) 
      }).connect(this.connSettings); 
     }); 
    } 
} 

請注意,與第二個解決方案,你將不得不調用subscribeopenConnection否則什麼都不會發生,因爲它現在包裹在裏面可觀察到的:

connectionService 
    .openConnection() 
    .subscribe(list => console.log(list)); 
相關問題