gpt4 book ai didi

javascript - 尝试在 angular2 中使用 stomp.js

转载 作者:太空狗 更新时间:2023-10-29 18:00:01 24 4
gpt4 key购买 nike

我正在尝试使用 stomp.js 实现 Stomp Websocket 客户端。

我正在将 angular2 与 typescript 和 webpack 结合使用,并且对所有这些技术都是新手。我的 angular2 项目是建立在这个种子之上的: https://github.com/angular/angular2-seed

作为实现 stomp.js 客户端的指南,我使用了 https://github.com/sjmf/ng2-stompjs-demo

我目前收到的错误如下:

?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 演示中弄清楚。

在此先感谢您的帮助!

最佳答案

您是否尝试过在界面旁边导出变量?

export var Stomp: Stomp;

关于javascript - 尝试在 angular2 中使用 stomp.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37014407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com