【问题标题】:cannot keep socket connection when screen off in android在android中关闭屏幕时无法保持套接字连接
【发布时间】:2016-03-30 06:28:55
【问题描述】:

我无法在 android 中保持套接字连接。

我在我的应用程序中使用Socket.IO-client java 库。

当屏幕打开时,套接字连接保持不变。

但是,如果屏幕关闭,则由于 ping 超时导致套接字断开。

我该如何解决这个问题?

我这样打开连接。

private static final String EVENT_CONNECT = Socket.EVENT_CONNECT;
private static final String EVENT_MESSAGE = Socket.EVENT_MESSAGE;
private static final String EVENT_DISCONNECT = Socket.EVENT_DISCONNECT;
private static final String EVENT_PING = Socket.EVENT_PING;
private static final String EVENT_CONNECT_TIMEOUT = Socket.EVENT_CONNECT_TIMEOUT;
private static final String EVENT_ERROR = Socket.EVENT_ERROR;

public void connect() {
    if (socket != null && socket.connected() == true) {
        return;
    }
    IO.Options options = new IO.Options();

    options.timeout = 60 * 1000;
    options.reconnection = true;

    Log.d(TAG, "try socket connect");
    socket.on(EVENT_CONNECT, this::onConnected)
            .on(EVENT_MESSAGE, this::onMessage)
            .on(EVENT_DISCONNECT, this::onDisconnected)
            .on(EVENT_PING, this::onPing)
            .on(EVENT_CONNECT_TIMEOUT, this::onConnectTimeout)
            .on(EVENT_ERROR, this::onError);

    socket.connect();
}

这是我的服务器端代码

var Socket = require('socket.io');
var io = Socket(server, { 'pingInterval': 25 * 1000 });
var port = process.env.PORT || 3000;

io.on('connection', function(socket){
    console.log('a user connected');

    ...

    socket.on('disconnect', function(data){
        (typeof socket.member != 'undefined') && disconnect(socket);
        console.log(data);
    });
});

ping 间隔为 25 秒,超时为 60 秒。

当安卓屏幕关闭时,客户端不适用于EVENT_PING。其他事件正常工作。

服务器因日志断开(ping 超时)。

【问题讨论】:

  • 您在哪个 Android 版本中发现此问题?
  • Lollipop(5.0) 和 Kitkat(4.4)
  • 您是否为此使用 Android 服务?
  • 如果你没有使用 Android 服务,操作系统会在后台杀死你的 App/Activity。
  • 是的。套接字连接在服务上。屏幕打开时保持。

标签: android socket.io socket.io-java-client


【解决方案1】:

我解决这个问题如下:

private static final String EVENT_PING = "ping1";
private static final String EVENT_PONG = "pong1";

public void connect() {
    if (socket != null && socket.connected() == true) {
        return;
    }
    IO.Options options = new IO.Options();

    options.timeout = 60 * 1000;
    options.reconnection = true;

    Log.d(TAG, "try socket connect");
    socket.on(EVENT_CONNECT, this::onConnected)
            .on(EVENT_MESSAGE, this::onMessage)
            .on(EVENT_DISCONNECT, this::onDisconnected)
            .on(EVENT_PING, this::onPing)
            .on(EVENT_CONNECT_TIMEOUT, this::onConnectTimeout)
            .on(EVENT_ERROR, this::onError);

    socket.connect();
}

private void onPing(Object... args) {
    Log.d(TAG, "socket ping");
    socket.emit(EVENT_PONG);
}

这是服务器代码。

var pingInterval = 25 * 1000;
var Socket = require('socket.io');
var io = Socket(server, { 'pingInterval': pingInterval });
var port = process.env.PORT || 3000;

io.on('connection', function(socket){
    console.log('a user connected');

    function sendPing() {
        socket.emit('ping1');
    }

    setTimeout(sendPing, pingInterval);

    socket.on('disconnect', function(data){
        (typeof socket.member != 'undefined') && disconnect(socket);
        console.log(data);
    });

    socket.on('pong1', function(data) {
        setTimeout(sendPing, pingInterval);
        console.log('pong');
    });
});

【讨论】:

  • 我和你有同样的问题 - 请告诉我你使用的是哪个版本的 socket.io - 如果我正确理解你的答案,你只是在强制服务器发送数据以固定的时间间隔发送给客户端以在屏幕关闭时保持会话活动?仅供参考,我使用非常旧的 socket.io 0.9,我认为它不会遇到同样的问题,我试图追踪它,因为这已经停止了大升级。
猜你喜欢
  • 2020-03-21
  • 2018-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多