2017-12-05 14 views
0

Ich brauche atmosphäre.js in meinem eckigen 5 Projekt.Angular 5 und atmosphere.js

Ich habe diese Schritte getan:

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

und ich habe einen Dienst erstellt:

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); 
    } 
    } 

aber die Buchse ist immer undefiniert.

wo ist mein Fehler?

dank

Antwort

0

Sie sollten Ihre Steckdose in someway tatsächlich instanziiert wird meine Vermutung new mit:

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

Der erste Teil nur eine Typdefinition ist, dass Sie mit dem TypeScript Compiler geben. Die Eigenschaft bleibt undefiniert, bis Sie ihr tatsächlich etwas zuweisen.

Obwohl über ihre schauen, scheint dies nicht der richtige Weg zu sein. Versuchen Sie Folgendes:

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

groß es funktioniert. Tnx –