Angular 5 和 atmosphere.js
Angular 5 and atmosphere.js
我需要在我的 angular 5 项目中使用 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);
}
}
但套接字始终未定义。
我的错误在哪里?
谢谢
您实际上应该以某种方式实例化您的套接字,我的猜测是使用 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);
}
}
}
我需要在我的 angular 5 项目中使用 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);
}
}
但套接字始终未定义。
我的错误在哪里?
谢谢
您实际上应该以某种方式实例化您的套接字,我的猜测是使用 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);
}
}
}