Strophe.js发送($ pres())无法在Typescript angular(版本5)中工作

时间:2018-02-07 09:30:10

标签: angular typescript xmpp strophe.js

我正在使用 Strophe.js 连接 Openfire XMPP 服务器。已建立连接状态已连接,但我无法向服务器发送状态,即用户未在Openfire控制台上显示在线

以下是我的plunker链接:Plunker

在代码中请参考src / app.ts第47行

this.connection.send($pres());

问题出在上述方法中。

请告诉我遗漏的内容或不正确的内容?

由于

1 个答案:

答案 0 :(得分:0)

这就是我们解决问题的方法。当Strophe为事件调用句柄时,前面定义的所有变量都是未定义的。在这种情况下" this.connection"未定义。

我们首先定义了一个全局变量并在构造函数中设置它。

var Strophethis;

export class StropheAccess {

private connection: any;

constructor () {
   Strophethis = this;
}

执行回叫时,更新所需的变量。

onConnection (status): boolean {
   this.connection = StropheAccess.connection;

然后发送将起作用。

 this.connection.send($pres());

除了此更新,我们还需要导入$ pres和其他。

import { Strophe} from 'strophe';
import { $pres} from 'strophe';
import { $iq } from 'strophe';
import { $msg} from 'strophe';
import { $build } from 'strophe';

祝你好运。