2017-12-05 27 views
0

私のAngular 5プロジェクトではatmosphere.jsを使用する必要があります。Angular 5 and atmosphere.js

は、私はこれらのステップを行ってきた:

npm install --save @types/atmosphere.js 
npm install --save atmosphere.js 

と私はサービスを作成しました:

import { Injectable } from '@angular/core'; 
import * as Rx from 'rxjs'; 
import * as Atmosphere from 'atmosphere.js'; 
import {Subject} from 'rxjs/Subject'; 
@Injectable() 
export class WebsocketService { 
    public eventsSubject: Rx.Subject<any> = new Rx.Subject<any>(); 
    private url:string = "http://localhost:4200/ws/register"; 

    private socket: Atmosphere.Atmosphere; 
    private subSocket: any; 
    private request: Atmosphere.Request; 

    public dataStream: Subject<any> = new Subject<any>(); 

    constructor() { 

    ... 

    try { 
     this.subSocket = this.socket.subscribe(this.request); 
    } catch (e) { 
     console.log("Error: " + e); 
    } 
    } 

が、ソケットは常に定義されていません。

私のエラーはどこですか?あなたが実際に何らかの方法であなたのソケットをインスタンス化する必要があり

おかげ

答えて

0

は、 私の推測は newを使用している:

private socket: Atmosphere.Atmosphere = new Atmosphere.Atmosphere(); 

最初の部分は、あなたがTypeScriptコンパイラに与えるだけで型定義です。実際に何かを割り当てるまでプロパティは未定義のままです。

apiを見ても、これは正しい方法ではありません。試してみてください:

export class WebsocketService { 
    // ... 
    private socket: typeof Atmosphere = Atmosphere; 
    private subSocket: Subscription; 
    private request: Atmosphere.Request = new atmosphere.AtmosphereRequest(); 

    constructor() { 
    try { 
     this.subSocket = this.socket.subscribe(this.request); 
    } catch (e) { 
     console.log("Error: " + e); 
    } 
    } 
} 
+0

素晴らしいです。 tnx –