为什么 AndroidAsync 断开连接时间这么长? [英] Why is AndroidAsync disconnect time so long?

查看:42
本文介绍了为什么 AndroidAsync 断开连接时间这么长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AndroidAsync koush 低级网络协议库.我正在使用 WebSocket 连接到服务器.我能够连接、发送/接收消息和断开连接.我的断开连接时间很长.服务器平均有 59 秒未检测到断开连接.

I'm using the AndroidAsync koush low level network protocol library. I am connecting to a server with WebSocket. I am able to connect, send/receive messages, and disconnect. My disconnect time is very long. Server does not detect the disconnect for on average 59 seconds.

为了重现问题,我连接WebSocket,授权,每10秒开始ping.然后我打开飞行模式,我的网络连接中断了.这时候调用了 setClosedCallback 方法.这是我记录此消息的方式:

In order to reproduce the problem, I connect WebSocket, authorize, and start pinging every 10 seconds. Then I turn on Airplane Mode and my network connection drops. At this time the setClosedCallback method is called. Here is how I am logging this message:

private WebSocket mConnection;

private WebSocketConnectCallback mSocketCallback = new WebSocketConnectCallback() {
    @Override
    public void onCompleted(Exception ex, WebSocket webSocket) {
    ...
            webSocket.setClosedCallback(new CompletedCallback() {
            @Override
            public void onCompleted(Exception ex) {
                Log.d("websocket", "WebSocket closed");
                if (ex != null && ex.getMessage() != null) {
                    Log.e("websocket", "WebSocket closed exception: " + ex.getMessage());
                }
                disconnect();
            }
        });
    }
};

private void disconnect() {
    mPingHandler.removeCallbacks(pingRunnable);
    if (mConnection != null) {
        mConnection.close();
        mConnection = null;
    }
}

这是让我知道 WebSocket 已关闭的日志消息:

This is the log message letting me know that WebSocket has closed:

WebSocket 关闭异常:recvfrom failed: ETIMEDOUT(连接超时)

然而,我们的服务器在大约 59 秒内没有收到断开连接消息.我们的服务器正在使用这些库:

However our server does not receive the disconnect message for about 59 seconds. Our server is using these libraries:

  • gevent==1.0
  • gevent-websocket==0.9.2
  • greenlet==0.4.2

有没有办法加快我的速度?某处是否有套接字级别超时,我可以将其设置为较低的值,以便我的服务器人员更快地获得断开连接消息?

Is there a way to speed this up on my end? Is there a socket level timeout somewhere that I can set to a lower value so that my server guys get the disconnect message faster?

推荐答案

因为 AndroidAsync 没有实现结束握手,这是RFC 6455(参见这个有关详细信息),您的 WebSocket 服务器无法检测在 WebSocket 上下文中的断开连接.因此,服务器必须等待 ETIMEDOUT 在 TCP/IP 上下文中 来检测断开连接.

Because AndroidAsync does not implement the closing handshake which is required by RFC 6455 (see this for details), your WebSocket server cannot detect disconnection in the context of WebSocket. So, the server has to wait for ETIMEDOUT in the context of TCP/IP to detect disconnection.

使用符合 RFC 6455 并正确实现关闭握手的 WebSocket 库.

Use a WebSocket library that complies with RFC 6455 and implements the closing handshake correctly.

这篇关于为什么 AndroidAsync 断开连接时间这么长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆