【问题标题】:Android-XMPP disconnectAndroid-XMPP 断开连接
【发布时间】:2015-04-10 05:28:05
【问题描述】:

我正在为我的应用程序使用 aSmack 和 Openfire。和 Spark 进行测试。

我正在编写我的连接代码和广播接收器,它们在 STICKY 服务中监听网络状态。

当我关闭 wifi 时,互联网会关闭,但 Spark 仍将我显示为“可用”,3-4 分钟后它会将我的状态变为“不可用”。

我尝试了 xmppConnection.disconnect() 和 Presence.Type.unavailable。但没有一个为我工作。

如何在断开客户端的互联网连接后立即断开 XMPP 服务器/发送状态“不可用”?

这是我的代码:

// Connect XMPP Server
public void connectXMPPServer() {

    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {

            // Create a connection
            ConnectionConfiguration connConfig = new ConnectionConfiguration(
                    HOST, PORT, SERVICE);
            connConfig.setReconnectionAllowed(true);

            xmppConnection = new XMPPConnection(connConfig);

            try {

                xmppConnection.connect();
                Log.i("XMPPChatDemoActivity", "Connected to "
                        + xmppConnection.getHost());
            } catch (XMPPException ex) {

                Log.e("XMPPChatDemoActivity", "Failed to connect to "
                        + xmppConnection.getHost());
                Log.e("XMPPChatDemoActivity", ex.toString());
            }

            try {

                if (xmppConnection.isConnected()) {

                    xmppConnection.login(USERNAME, PASSWORD);
                    Log.i("XMPPChatDemoActivity", "Logged in as "
                            + xmppConnection.getUser());

                    Presence presence = new Presence(
                            Presence.Type.available);
                    xmppConnection.sendPacket(presence);
                }
            } catch (XMPPException ex) {

                Log.e("XMPPChatDemoActivity", "Failed to log in as "
                        + USERNAME);
                Log.e("XMPPChatDemoActivity", ex.toString());
            }
        }
    });
    t.start();
}

// Broadcast receiver; listens to network state
public BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        try {

            if (isNetworkOn()) {

                Log.i("BackgroundService-BroadcastReceiver",
                        "Network is on");
                connectXMPPServer();
            } else if (!isNetworkOn()) {

                Log.i("BackgroundService-BroadcastReceiver",
                        "Network is off");
                Presence presence = new Presence(Presence.Type.unavailable);
                xmppConnection.sendPacket(presence);
                xmppConnection.disconnect();
            }
        } catch (Exception e) {

            Log.e("BackgroundService-networkStateReceiver", e.toString());
        }
    }
};

// Returns network state
public boolean isNetworkOn() {

    ConnectivityManager connMngr = (ConnectivityManager) getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connMngr.getActiveNetworkInfo();
    return (netInfo != null && netInfo.isConnected());
}

【问题讨论】:

  • 互联网连接断开后,您的数据包不会传送/到达服务器,因此它会持续显示在服务器上。使用 ping-pong 进行端到端连接,30 秒后从服务器连续发送 ping 到客户端。如果 ping 未从服务器接收,则设置该用户不可用

标签: android xmpp asmack disconnect


【解决方案1】:

您必须添加一个 ConnectionListener,它会在检测到连接断开后立即被调用。然后,您可以在 UI 中更新您的状态。

发送出席信息是为了让其他人知道您的状态,如果您没有联系,您显然无法做到这一点。服务器会在检测到断开连接时为您执行此操作。

【讨论】:

  • 感谢您的回答。我想知道我可以对服务器做什么,以便在客户端的 Internet 连接断开时它会立即显示为“不可用”。
  • @Sushant 你得到问题的答案或解决方案了吗?
猜你喜欢
  • 2015-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-25
  • 2016-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多