我正在从事一个项目,在wordpress后端(用于存储消息、帖子)、用于用户交互的角度前端(AnGluar6x)和用于从web应用程序内部进行聊天通信的eJabber服务器之间进行复杂的交互。
为了实现这一点,我尝试将strophe.js集成到角度框架中。
我使用的身份验证方法很复杂。当一个新用户通过适当的(角度)形式注册到wordpress后端时,我已经设置了钩子,它也自动订阅新用户到我的本地eJabberd服务器。为了管理身份验证,为了避免最终用户输入两次他/她的凭据,我在eJabberd中设置了一个自定义extauth脚本(基于这),它与自定义wordpress端点并行工作(为此我使用JWT )。
extauth脚本运行良好,我可以成功地连接到本地eJabberd服务器,并使用各种聊天客户端(Adium、Apple等)。我也可以从我的网络应用程序中连接(就像前面说过的那样)到eJabber。
下面是用于管理连接(ChatPanel.Service.ts)的代码片段:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
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";
private _xmppConnection: any = null;
/**
* Constructor
*/
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.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: string): boolean
{
console.log("eJabber Msg: " + 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:
//handler function [ onMessage() ] will be called when the user recieves a new message
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");
}
}当我尝试在注销后重新连接到服务器时,就会出现问题。
当我从我的web应用程序(即从我的chatpanel.service注销()被调用)登录时,如果我再次尝试登录,我将不会收到来自ejabber服务器的响应。显然,服务器停止侦听来自strophe的任何进一步请求。
我已经试过websockets和BOSH了。最终结果是一样的。
以下是浏览器控制台的快照:
First, successfull, login (WebSocket)
[Log] Connecting to eJabberd... (main.js, line 8666)
[Log] 1: Websocket open (main.js, line 8626)
[Log] 1: _connect_cb was called (main.js, line 8626)
[Log] 1: _dataRecv called (main.js, line 8626)
[Log] 1: SASL authentication succeeded. (main.js, line 8626)
[Log] 1: _dataRecv called (main.js, line 8626, x4)
[Log] eJabberd connected! (main.js, line 8687)
[Log] 1: _dataRecv called (main.js, line 8626)
Logout...
[Log] Disconnecting from eJabberd... (main.js, line 8672)
[Log] 1: Disconnect was called because: logout (main.js, line 8626)
[Log] 1: _doDisconnect was called (main.js, line 8626)
[Log] 1: WebSockets _doDisconnect was called (main.js, line 8626)
[Log] Disconnected from eJabberd (main.js, line 8675)
[Log] 1: Websocket closed (main.js, line 8626)
Second attempt (fails, WebSocket)
[Log] Connecting to eJabberd... (main.js, line 8666)
[Log] 1: Websocket open (main.js, line 8626)
[Log] 1: _connect_cb was called (main.js, line 8626)
[Log] 1: _dataRecv called (main.js, line 8626)
[Log] eJabberd authentication failed! (main.js, line 8693)
[Log] 3: WebSocket stream error: connection-timeout - Idle connection (main.js, line 8626)
[Log] eJabberd generic connection error! (main.js, line 8696)
[Log] 1: _doDisconnect was called (main.js, line 8626)
[Log] 1: WebSockets _doDisconnect was called (main.js, line 8626)
[Log] Disconnected from eJabberd (main.js, line 8675)
[Log] 1: Websocket closed (main.js, line 8626)
Successful first login (BOSH)
[Log] Connecting to eJabberd... (main.js, line 8666)
[Log] 0: _throttledRequestHandler called with 1 requests (main.js, line 8626)
[Log] 0: request id 1.0 posting (main.js, line 8626)
[Log] 0: request id 1.0 state changed to 1 (main.js, line 8626)
[Log] 0: request id 1.1 state changed to 2 (main.js, line 8626)
[Log] 0: request id 1.1 state changed to 3 (main.js, line 8626)
[Log] 0: request id 1.1 state changed to 4 (main.js, line 8626)
[Log] 0: removing request (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 0 requests (main.js, line 8626)
[Log] 0: request id 1 should now be removed (main.js, line 8626)
[Log] 0: request id 1.1 got 200 (main.js, line 8626)
[Log] 1: _connect_cb was called (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 0 requests (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 1 requests (main.js, line 8626)
[Log] 0: request id 2.0 posting (main.js, line 8626)
[Log] 0: request id 2.0 state changed to 1 (main.js, line 8626)
[Log] 0: request id 2.1 state changed to 2 (main.js, line 8626)
[Log] 0: request id 2.1 state changed to 3 (main.js, line 8626)
[Log] 0: request id 2.1 state changed to 4 (main.js, line 8626)
[Log] 0: removing request (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 0 requests (main.js, line 8626)
[Log] 0: request id 2 should now be removed (main.js, line 8626)
[Log] 0: request id 2.1 got 200 (main.js, line 8626)
[Log] 1: _dataRecv called (main.js, line 8626)
[Log] 1: SASL authentication succeeded. (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 0 requests (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 1 requests (main.js, line 8626)
[Log] 0: request id 3.0 posting (main.js, line 8626)
[Log] 0: request id 3.0 state changed to 1 (main.js, line 8626)
[Log] 0: request id 3.1 state changed to 2 (main.js, line 8626)
[Log] 0: request id 3.1 state changed to 3 (main.js, line 8626)
[Log] 0: request id 3.1 state changed to 4 (main.js, line 8626)
[Log] 0: removing request (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 0 requests (main.js, line 8626)
[Log] 0: request id 3 should now be removed (main.js, line 8626)
[Log] 0: request id 3.1 got 200 (main.js, line 8626)
[Log] 1: _dataRecv called (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 0 requests (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 1 requests (main.js, line 8626)
[Log] 0: request id 4.0 posting (main.js, line 8626)
[Log] 0: request id 4.0 state changed to 1 (main.js, line 8626)
[Log] 0: request id 4.1 state changed to 2 (main.js, line 8626)
[Log] 0: request id 4.1 state changed to 3 (main.js, line 8626)
[Log] 0: request id 4.1 state changed to 4 (main.js, line 8626)
[Log] 0: removing request (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 0 requests (main.js, line 8626)
[Log] 0: request id 4 should now be removed (main.js, line 8626)
[Log] 0: request id 4.1 got 200 (main.js, line 8626)
[Log] 1: _dataRecv called (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 0 requests (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 1 requests (main.js, line 8626)
[Log] 0: request id 5.0 posting (main.js, line 8626)
[Log] 0: request id 5.0 state changed to 1 (main.js, line 8626)
[Log] 0: request id 5.1 state changed to 2 (main.js, line 8626)
[Log] 0: request id 5.1 state changed to 3 (main.js, line 8626)
[Log] 0: request id 5.1 state changed to 4 (main.js, line 8626)
[Log] 0: removing request (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 0 requests (main.js, line 8626)
[Log] 0: request id 5 should now be removed (main.js, line 8626)
[Log] 0: request id 5.1 got 200 (main.js, line 8626)
[Log] 1: _dataRecv called (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 0 requests (main.js, line 8626)
[Log] eJabberd connected! (main.js, line 8687)
[Log] 0: _throttledRequestHandler called with 1 requests (main.js, line 8626)
[Log] 0: request id 6.0 posting (main.js, line 8626)
[Log] 0: request id 6.0 state changed to 1 (main.js, line 8626)
[Log] 0: request id 6.1 state changed to 2 (main.js, line 8626)
[Log] 0: request id 6.1 state changed to 3 (main.js, line 8626)
[Log] 0: request id 6.1 state changed to 4 (main.js, line 8626)
[Log] 0: removing request (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 0 requests (main.js, line 8626)
[Log] 0: request id 6 should now be removed (main.js, line 8626)
[Log] 0: request id 6.1 got 200 (main.js, line 8626)
[Log] 1: _dataRecv called (main.js, line 8626)
[Log] 1: no requests during idle cycle, sending blank request (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 1 requests (main.js, line 8626)
[Log] 0: request id 7.0 posting (main.js, line 8626)
[Log] 0: request id 7.0 state changed to 1 (main.js, line 8626)
Logout...
[Log] Disconnecting from eJabberd... (main.js, line 8672)
[Log] 1: Disconnect was called because: logout (main.js, line 8626)
[Log] 1: _sendTerminate was called (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 2 requests (main.js, line 8626)
[Log] 0: _processRequest: first request has readyState of 1 (main.js, line 8626)
[Log] 0: request id 17.0 posting (main.js, line 8626)
[Log] 0: request id 17.0 state changed to 1 (main.js, line 8626)
[Log] 0: request id 17.0 state changed to 4 (main.js, line 8626)
[Log] 0: removing request (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 1 requests (main.js, line 8626)
[Log] 0: _processRequest: first request has readyState of 1 (main.js, line 8626)
[Log] 0: request id 17 should now be removed (main.js, line 8626)
[Log] 0: request id 17.0 got 200 (main.js, line 8626)
[Log] 1: _dataRecv called (main.js, line 8626)
[Log] 0: request id 16.1 state changed to 2 (main.js, line 8626)
[Log] 0: request id 16.1 state changed to 3 (main.js, line 8626)
[Log] 0: request id 16.1 state changed to 4 (main.js, line 8626)
[Log] 0: removing request (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 0 requests (main.js, line 8626)
[Log] 0: request id 16 should now be removed (main.js, line 8626)
[Log] 0: request id 16.1 got 200 (main.js, line 8626)
[Log] 1: _dataRecv called (main.js, line 8626)
[Log] 1: _doDisconnect was called (main.js, line 8626)
[Log] Disconnected from eJabberd (main.js, line 8675)
Second attempt (after logout, unsuccessful, BOSH)
[Log] Connecting to eJabberd... (main.js, line 8666)
[Log] 0: _throttledRequestHandler called with 1 requests (main.js, line 8626)
[Log] 0: request id 18.0 posting (main.js, line 8626)
[Log] 0: request id 18.0 state changed to 1 (main.js, line 8626)
[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (b7e98bdb6711295ac7cd2c2d7a139415, line 0)
[Log] 0: request id 18.1 state changed to 2 (main.js, line 8626)
[Log] 0: request id 18.1 state changed to 3 (main.js, line 8626)
[Log] 0: request id 18.1 state changed to 4 (main.js, line 8626)
[Log] 0: removing request (main.js, line 8626)
[Log] 0: _throttledRequestHandler called with 0 requests (main.js, line 8626)
[Log] 0: request id 18 should now be removed (main.js, line 8626)
[Log] 0: request id 18.1 got 200 (main.js, line 8626)
[Log] 1: _connect_cb was called (main.js, line 8626)
[Log] 3: Server did not offer a supported authentication mechanism (main.js, line 8626)
[Log] eJabberd connection failed! (main.js, line 8669)
[Log] 1: _doDisconnect was called (main.js, line 8626)
[Log] Disconnected from eJabberd (main.js, line 8675) 如您所见,在Bosh中,调试器告诉我服务器没有提供任何合适的身份验证方法。
显然,服务器不再监听传入的连接请求。但是,奇怪的是,如果我重新启动服务器,重新连接就会成功。看起来,即使在断开连接过程完成之后,strophe.js仍然保持着打开状态,而关闭它的唯一方法就是停止服务器。
我不确定这是strophe.js问题还是ejabberd配置问题,但我更倾向于考虑客户端问题(或代码中缺少的内容),因为外部客户端可以完美地连接到服务器。
任何提示都将不胜感激。
发布于 2018-09-18 17:28:40
明白了。
作为对这些问题的回答,我在这里发布了一个类似的问题。
问题不是Strophe.js,而是jabber服务器的配置。问题是缓存。
来自eJabber docs (这里)
auth_use_cache: false\true:从ejabberd17.06开始的,缓存得到了彻底的改进。与extauth_cache不同,一组新变量描述缓存行为,默认值现在为真。请注意,缓存会影响每个帐户维护多个密码的能力。因此,如果您的身份验证机制支持特定于应用程序的密码,则必须禁用缓存。
在适用的情况下,eJabberd被配置为在默认情况下使用缓存。
由于当我从web应用程序中退出时,我会使用于身份验证的旧JSON令牌失效,因此ejabber将根据缓存的值检查新凭据,从而发现不匹配。
解决方案是禁用像这样的缓存(在ejabberd.yml中) auth_use_cache: false
https://stackoverflow.com/questions/52385906
复制相似问题