简体   繁体   English

smack使用上一个流ID连接到xmpp服务器

[英]smack connect to xmpp server with previous stream id

I'm building a chat application using xmpp over Ejabbered for server and smack for android client 我正在构建一个聊天应用程序,使用xmpp over Ejabbered for server和smack for android client

I've established connecting , login , send and receiving messages ,then I've faced a problem with user network disconnecting and reconnecting , which has been solved by Reconnecting Manger in smack and xmpp-0198, however there is a case where i need to create a new connection in smack but use the previous session (stream) to get all the messages stored in that session (they don't get stored to offline messages) ,and if i create a new connection with new stream id , user messages get lost . 我已经建立了连接,登录,发送和接收消息,然后我遇到了用户网络断开连接和重新连接的问题,这已经通过在smack和xmpp-0198中重新连接Manger解决了,但是有一种情况我需要在smack中创建一个新连接,但使用前一个会话(流)获取存储在该会话中的所有消息(它们不会存储到离线消息),如果我创建一个具有新流ID的新连接,则用户消息获取丢失 。

so is there a connection constructor to implement this solution . 那么是否有一个连接构造函数来实现这个解决方案。 or server side configuration to store thous messages to offline messages 或服务器端配置,以将消息存储到脱机消息

I think one of the following will solve your issue- 我认为以下其中一项将解决您的问题 -

  • First check is mod_offline enabled in the server side. 首先检查是否在服务器端启用了mod_offline。
  • If mod_offline enabled then check offline message limit in the server side. 如果启用了mod_offline,则检查服务器端的脱机消息限制。 It should be greater than 0. 它应该大于0。
  • Use PingManager to stable your connection. 使用PingManager稳定连接。 I am here putting sample code to use PingManager in android- 我在这里使用示例代码在android-中使用PingManager

During XMPPTcpConnection initiation- 在XMPPTcpConnection启动期间 -

pingManager = PingManager.getInstanceFor(this.connection);
pingManager.registerPingFailedListener(new PingFailedListener() {
    @Override
    public void pingFailed() {
        // session dropped, request for reconnection
    }
});

When XMPPTcpCOnnection authenticated- 当XMPPTcpCOnnection验证时 -

@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
    configurePingManager();
}

private void configurePingManager() {
    pingManager.setPingInterval(ACCORDING_SERVER_PING_INTERVAL);
    pingManager.pingServerIfNecessary();
}
  • Make sure stream_management enabled both in server side and client side. 确保在服务器端和客户端都启用了stream_management。 I am here putting a sample code to enable stream_management for android client side- 我在这里放一个示例代码来启用android客户端的stream_management-

xmppTcpConnection.setUseStreamManagement(true); xmppTcpConnection.setUseStreamManagement(真); xmppTcpConnection.setUseStreamManagementResumption(true); xmppTcpConnection.setUseStreamManagementResumption(真);

When XMPPTcpCOnnection authenticated checking session status send and request all pending streams using the code below- 当XMPPTcpCOnnection通过身份验证检查会话状态时,使用以下代码发送并请求所有挂起的流 -

@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
    configurePingManager();
    if (!resumed) {
        try {
            xmppTcpConnection.sendSmAcknowledgement();
            xmppTcpConnection.requestSmAcknowledgement();
        } catch (SmackException.NotConnectedException | StreamManagementException.StreamManagementNotEnabledException e) {
            e.printStackTrace();
        } 
    }
}  

Hope following all those steps your problem will be solved. 希望遵循所有这些步骤,您的问题将得到解决。

after lots of searching ,finally i upgraded Ejabberd server to the latest version 17.03 经过大量搜索,最后我将Ejabberd服务器升级到最新版本17.03

where they've added the new module mod_stream_mgmt ,and changed the behavior of stream management , so when i create a new connection it get rebind to the old one and receive the unsent and un-handled messages 他们已经添加了新模块mod_stream_mgmt,并改变了流管理的行为,所以当我创建一个新连接时,它会重新绑定到旧连接并接收未发送和未处理的消息

to activated the mod_stream_mgmt i used the following configurations : 激活mod_stream_mgmt我使用了以下配置:

mod_stream_mgmt : 
  resume_timeout :60
  resend_on_timeout: true

Note : I have also activated mod_ping on server side ,I don't know if that has a direct effect on this process and case but right now my clients are not missing any messages . 注意:我也在服务器端激活了mod_ping,我不知道这是否对这个过程和案例有直接影响但是现在我的客户端没有丢失任何消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM