我正在尝试使用stomp.js实现Stomp Websocket客户端。我正在使用angular2、typescript和webpack,对这些技术都很陌生。我的angular2项目是基于此种子构建的:
https://github.com/angular/angular2-seed。
我使用https://github.com/sjmf/ng2-stompjs-demo作为实现stomp.js客户端的指南。
我目前遇到的错误如下:
错误发生在这个方法中:
我的stompjs模块如下所示:
?d41d:73 EXCEPTION: TypeError: Cannot read property 'client' of undefined in [null]
错误发生在这个方法中:
public configure() : void {
// Check for errors
if (this.state.getValue() != STOMPState.CLOSED) {
throw Error("Already running!");
}
let scheme : string = 'ws';
if( AppSettings.IS_SSL ) {
scheme = 'wss';
}
this.client = Stomp.client(
scheme + '://'
+ AppSettings.HOST + ':'
+ AppSettings.WEBSOCK_PORT
+ AppSettings.WEBSOCK_ENDPOINT
);
this.client.heartbeat.incoming = AppSettings.HEARTBEAT;
}
所以,Stomp
似乎未定义。
我正在导入:
import {Stomp} from "stompjs";
我已经按照以下的方式使用npm安装了stomp.js:
npm install --save stompjs
我的stompjs模块如下所示:
declare module "stompjs" {
export interface Client {
heartbeat: any;
debug(...args: string[]);
connect(...args: any[]);
disconnect(disconnectCallback: () => any, headers?: any);
send(destination: string, headers?:any, body?: string);
subscribe(destination: string, callback?: (message: Message) => any, body?: string);
unsubscribe();
begin(transaction: string);
commit(transaction: string);
abort(transaction: string);
ack(messageID: string, subscription: string, headers?: any);
nack(messageID: string, subscription: string, headers?: any);
}
export interface Message {
command: string;
headers: any;
body: string;
ack(headers?: any);
nack(headers?: any);
}
export interface Frame {
constructor(command: string, headers?: any, body?: string);
toString(): string;
sizeOfUTF8(s: string);
unmarshall(datas: any);
marshall(command: string, headers?, body?);
}
export interface Stomp {
client: Client;
Frame: Frame;
over(ws: WebSocket);
}
}
我觉得我的模块和实际的库之间缺少连接,但我不知道如何做到这一点,也无法从github演示中弄清楚。
非常感谢您提前的任何帮助!