2017-12-05 4 views
0

각도 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 –