【问题标题】:Strophe.js send($pres()) not working in Typescript angular (version 5)Strophe.js send($pres()) 在 Typescript angular 中不起作用(版本 5)
【发布时间】:2018-05-05 02:57:57
【问题描述】:
我正在使用 Strophe.js 连接 Openfire XMPP 服务器。连接已建立,状态为 CONNECTED,但我无法将状态发送到服务器,即用户未在 Openfire 控制台上显示 在线。
这是我的 plunker 链接:Plunker
代码中请参考src/app.ts line no.47
this.connection.send($pres());
问题出在上述方法中。
请告诉我遗漏了什么或不正确的地方?
谢谢
【问题讨论】:
标签:
angular
typescript
xmpp
strophe.js
【解决方案1】:
这就是我们解决问题的方法。当 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';
祝你好运。