gpt4 book ai didi

angular - angular5 中 XMPP Web 客户端的起点

转载 作者:行者123 更新时间:2023-12-04 16:44:30 27 4
gpt4 key购买 nike

如果问题可能过于宽泛,我深表歉意,因为我是 XMPP 的新手。

我正在开发 Angular6 应用程序并希望在其中集成 Jabber 聊天。我在谷歌上搜索了很多,但似乎没有找到任何明确的答案。

显然 strophe.js 看起来很有前途,但我找不到任何关于如何将它集成到 Angular5/6 项目中的文档。

任何提示将不胜感激

谢谢

最佳答案

嗯,我找到了xmpp-bosh-clientngx-chat .

第一个至少有记录。第二个不是,但我可以查看来源。

也许这可以帮助其他和我有同样经历的人。

谢谢

附言如果有人知道更好的东西,他/她是最受欢迎的

编辑

我正在发布一些代码片段,说明我如何在 TypeScript/Angular (6.x) 前端中使用 strophe.js 连接到 eJabberd(本地)服务器。

聊天面板.service.ts

import { Injectable } from '@angular/core';

import { Strophe, $pres } from 'strophe.js';

import { EJABBERD } from 'app/api/api.module';

var chatPanelServiceInstance: any = null;

@Injectable()
export class ChatPanelService
{
contacts: any[];
chats: any[];
user: any;
client: any;

// Private
private _xmppConnectionsString: String = "ws://" + EJABBERD.host + ":5280/ws"; // WebSockets
//private _xmppConnectionsString: String = "http://" + EJABBERD.host + ":5280/bosh"; // BOSH
private _xmppConnection: any = null;

/**
* Constructor
*
* @param {HttpClient} _httpClient
*/
constructor(
)
{
chatPanelServiceInstance = this;
Strophe.log = (level: any, msg: string) => { console.log(level + ": " + msg); };
}

/**
* Log into eJabberd
*
* @param {string} jid user name
* @param {string} password password (actually, the user token)
*/
login(jid: string, password: string): void
{
if ( ! this._xmppConnection ) {
this._xmppConnection = new Strophe.Connection( this._xmppConnectionsString , {'keepalive': true});
}

// this._xmppConnection.rawInput = (data: any) => {console.log("RAW IN: " + data)};
// this._xmppConnection.rawOutput = (data: any) => {console.log("RAW OUT: " + data)};

this._xmppConnection.connect(jid+'@'+EJABBERD.host, password, this._onConnect);
}

/**
* Disconnect from eJabberd
*/
logOut(): void
{
if ( this._xmppConnection ) {
this._xmppConnection.options.sync = true;
this._xmppConnection.flush();
this._xmppConnection.disconnect("logout");
this._xmppConnection = null;
}
}

/**
* eJabberd XMPP message Handler
* @param {string} msg Message received
*/
private _onMessage(msg: any): boolean
{
console.log(msg);

return true;
}

/**
* eJabberd connection Handler
* @param {any} status connection result
*/
private _onConnect(status: any): void
{
switch (status) {
case Strophe.Status.CONNECTING:
console.log("Connecting to eJabberd...");
break;
case Strophe.Status.CONNFAIL:
console.log("eJabberd connection failed!");
break;
case Strophe.Status.DISCONNECTING:
console.log("Disconnecting from eJabberd...");
break;
case Strophe.Status.DISCONNECTED:
console.log("Disconnected from eJabberd");
break;
case Strophe.Status.CONNECTED:
// We could have used 'this' instead of an external pointer (chatPanelServiceInstance),
// but the compiler is getting the meaning of 'this' wrong since strophe.js is not a native TypeScript library.
// This means that at run time 'this' doesn't point the service instance, rather to the connection itself.
// In order to avoid confusion I've chosen to use an explicit pointer to the service.
//
chatPanelServiceInstance._xmppConnection.addHandler(chatPanelServiceInstance._onMessage, null, 'message');

//Setting our presence in the server so that everyone can know that we are online
chatPanelServiceInstance._xmppConnection.send($pres().tree());

console.log("eJabberd connected!");
break;
case Strophe.Status.AUTHENTICATING:
console.log("eJabberd authenticating...");
break;
case Strophe.Status.AUTHFAIL:
console.log("eJabberd authentication failed!");
break;
case Strophe.Status.ERROR:
console.log("eJabberd generic connection error!");
break;
case Strophe.Status.ATTACHED:
console.log("eJabberd connection attached!");
break;
case Strophe.Status.REDIRECT:
console.log("eJabberd connection redirected!");
break;
case Strophe.Status.CONNTIMEOUT:
console.log("eJabberd connection timeout!");
break;
default:
console.log("eJabberd: Unknow connection status");
}
}

相关要点:

  1. 注意 _xmppConnectionString 的使用:我正在使用 Web Sockets连接,但 BOSH 版本也工作正常。
  2. 注意使用指向服务的外部指针。 (chatPanelServiceInstance - 请参阅代码中的注释)
  3. 像这样安装 strophe.js:在您的 Angular 项目的根文件夹中,键入 npm install --save strophe.js

关于angular - angular5 中 XMPP Web 客户端的起点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51602357/

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