androidsocketsandroid-background

Socket connection in background


I need to make an application where, while the user is authorized, it keeps the socket connection until it is logged out. For this purpose, a foreground service is created, which starts after the authorization of the user, and stops when it is logged out. It implements connection and reconnection on the socket.

All works well until you press the power button and turn off the charging. After this, the user stops receiving pongs from the server and the SocketTimeoutException is received on the OkHttp, and also stops receiving messages on the socket. On JavaWebsocket, The connection was closed because the other endpoint did not respond with a pong in time is received, after which you can successfully create a new socket connection, but it will repeat the same problem in the loop.

In the settings, the optimization of the battery for this application was disabled. What can I do to make a stable connection socket work in the background?

Implementation of activity:

class MainActivity : BaseFragmentPermissionActivity(), MainMvpView {   
    private var mIsSocketBound = false   
    private var mSocketBroadcastReceiver = SocketBroadcastReceiver(this)   
    private var mSocketConnection = SocketConnection(this)
    private var mSocketService: SocketService? = null
    
    override fun onCreate(savedInstanceState: Bundle?) {   
        super.onCreate(savedInstanceState)   
        ...
        doBindService()
    }
    
    private fun doBindService() {
        bindService(Intent(this, SocketService::class.java), mSocketConnection, Context.BIND_AUTO_CREATE)
        mIsSocketBound = true
    }
    
    override fun onStart() {
        super.onStart()
        ...
        mSocketService?.doStopForeground()
    }
    
    override fun onStop() {
        mSocketService?.doStartForeground()
        ...
        super.onStop()
    }
    
    override fun onDestroy() {
        doUnbindService()
        ...
        super.onDestroy()
    }    

    private fun doUnbindService() {
        if (mIsSocketBound) {
            unbindService(mSocketConnection)
            mIsSocketBound = false
            mSocketService = null
        }
    }

    class SocketConnection(mainActivity: MainActivity) : ServiceConnection {
        private val mMainActivity: WeakReference<MainActivity> = WeakReference(mainActivity)

        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            val socketService = (service as SocketService.LocalBinder).getService()
            mMainActivity.get()?.mSocketService = socketService
            if (socketService.isForeground()) {
                socketService.doStopForeground()
            }
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            mMainActivity.get()?.mSocketService = null
        }
    }
}

Implementation of service:

class SocketService : Service(), MvpErrorHandler {   
    private val mConnectingHandler = Handler()
    private val mConnectingTask = ConnectingTask(this)
    private var mIsRunningForeground = false

    override fun onBind(intent: Intent?): IBinder {
        startService(Intent(this, SocketService::class.java))
        return mBinder
    }

    override fun onCreate() {
        super.onCreate()
        DaggerServiceComponent.builder()
                .serviceModule(ServiceModule(this))
                .applicationComponent(PatrolApplication.applicationComponent)
                .build()
                .inject(this)

        startConnecting()
        ...
    }

    override fun onDestroy() {
        ...
        stopConnecting()
        super.onDestroy()
    }

    private fun startConnecting() {
        if (!mIsConnecting) {
            mIsConnecting = true
            mConnectingHandler.post(mConnectingTask)
        }
    }

    private fun stopConnecting() {
        mConnectingHandler.removeCallbacks(mConnectingTask)
        mIsConnecting = false
    }

    private fun openConnection() {
        mCompositeDisposable.add(mDataManager.getSocketToken()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(false, this, {
                stopConnecting()
                mDataManager.openSocketConnection(it.token)
            }, {
                mConnectingHandler.postDelayed(mConnectingTask, RECONNECT_TIME.toLong())
                return@subscribe ErrorHandlerUtil.handleGetSocketError(it, this)
            }))
    }

    class ConnectingTask(socketService: SocketService) : Runnable {
        private val mSocketService: WeakReference<SocketService> = WeakReference(socketService)

        override fun run() {
            mSocketService.get()?.openConnection()
        }
    }
}

Implementation of SocketHelper using JavaWebsocket:

class CustomApiSocketHelper @Inject constructor() : ApiSocketHelper {

    private var mCustomSocketClient: WebSocketClient? = null

    override fun openSocketConnection(token: String) {
        mCustomSocketClient = CustomSocketClient(URI(CONNECTION_URL + token))
        mCustomSocketClient?.connect()
    }

    override fun sendMessage(text: String) {
        if (mCustomSocketClient?.isOpen == true) {
            try {
                mCustomSocketClient?.send(text)
            } catch (t: Throwable) {
                Log.e(TAG, Log.getStackTraceString(t))
                Crashlytics.logException(t)
            }
        }
    }

    override fun closeSocketConnection() {
        mCustomSocketClient?.close(CLOSE_REASON_OK)
    }

    class CustomSocketClient(uri: URI) : WebSocketClient(uri) {
        init {
            connectionLostTimeout = PING_TIMEOUT
        }

        override fun onOpen(handshakedata: ServerHandshake?) {
            sendBroadcast(SocketActionType.OPEN.action)
        }

        override fun onMessage(message: String?) {
            sendBroadcast(SocketActionType.MESSAGE.action, message)
        }

        override fun onClose(code: Int, reason: String?, remote: Boolean) {
            if (code != CLOSE_REASON_OK) {
                //call startConnecting() in service
                sendBroadcast(SocketActionType.CLOSE.action)
            }
        }

        override fun onError(ex: Exception?) {
            sendBroadcast(SocketActionType.FAILURE.action)
        }

        private fun sendBroadcast(type: Int) {
            val intent = Intent().apply { action = SOCKET_BROADCAST_ACTION }
            intent.putExtra(SOCKET_MESSAGE_TYPE, type)
            LocalBroadcastManager.getInstance(CustomApplication.application).sendBroadcast(intent)
        }

        private fun sendBroadcast(type: Int, text: String?) {
            val intent = Intent().apply { action = SOCKET_BROADCAST_ACTION }
            intent.putExtra(SOCKET_MESSAGE_TYPE, type)
            intent.putExtra(SOCKET_MESSAGE, text)
            LocalBroadcastManager.getInstance(CustomApplication.application).sendBroadcast(intent)
        }
    }
}

Implementation of SocketHelper using OkHttp:

class CustomApiSocketHelper @Inject constructor() : ApiSocketHelper {
    
        private var mCustomSocketClient: WebSocket? = null
    
        override fun openSocketConnection(token: String) {
            val request = Request.Builder()
                .url(CONNECTION_URL + token)
                .build()
            mCustomSocketClient = CustomApplication.applicationComponent.authorizedClient().newWebSocket(request, CustomSocketClient())
        }
    
        override fun sendMessage(text: String) {
            mPatrolSocketClient?.send(text)
        }
    
        override fun closeSocketConnection() {
            mCustomSocketClient?.close(CLOSE_REASON_OK, null)
        }
    
        class CustomSocketClient : WebSocketListener() {
    
            override fun onOpen(webSocket: WebSocket, response: Response) {
                super.onOpen(webSocket, response)
                sendBroadcast(SocketActionType.OPEN.action)
            }
    
            override fun onMessage(webSocket: WebSocket, text: String) {
                super.onMessage(webSocket, text)
                sendBroadcast(SocketActionType.MESSAGE.action, text)
            }
    
            override fun onClosed(webSocket: WebSocket?, code: Int, reason: String?) {
                super.onClosed(webSocket, code, reason)
                if (code != CLOSE_REASON_OK) {
                    sendBroadcast(SocketActionType.CLOSE.action)
                }
            }
    
            override fun onFailure(webSocket: WebSocket?, t: Throwable?, response: Response?) {
                super.onFailure(webSocket, t, response)
                sendBroadcast(SocketActionType.FAILURE.action)
            } 
    
            private fun sendBroadcast(type: Int) {
                val intent = Intent().apply { action = SOCKET_BROADCAST_ACTION }
                intent.putExtra(SOCKET_MESSAGE_TYPE, type)
                LocalBroadcastManager.getInstance(CustomApplication.application).sendBroadcast(intent)
            }
    
            private fun sendBroadcast(type: Int, text: String?) {
                val intent = Intent().apply { action = SOCKET_BROADCAST_ACTION }
                intent.putExtra(SOCKET_MESSAGE_TYPE, type)
                intent.putExtra(SOCKET_MESSAGE, text)
                LocalBroadcastManager.getInstance(CustomApplication.application).sendBroadcast(intent)
            }
        }
    }
    ...
    @Provides
    @Singleton
    @Named(AUTHORIZED_CLIENT)
        fun provideAuthorizedClient(builder: OkHttpClient.Builder, interceptor: Interceptor, authenticator: Authenticator): OkHttpClient = builder
            .addInterceptor(interceptor)
            .authenticator(authenticator)
            .pingInterval(PING_TIMEOUT.toLong(), TimeUnit.SECONDS)
            .build()

    @Provides
    @Singleton
        fun provideOkHttpBuilder() = CustomApiHelper.getOkHttpBuilder()

        fun getOkHttpBuilder(): OkHttpClient.Builder {
            val builder = OkHttpClient.Builder()
            builder.readTimeout(NETWORK_CALL_TIMEOUT, TimeUnit.SECONDS)
            builder.writeTimeout(NETWORK_CALL_TIMEOUT, TimeUnit.SECONDS)
            if (BuildConfig.DEBUG) {
                val logger = HttpLoggingInterceptor()
                logger.level = HttpLoggingInterceptor.Level.BASIC
                builder.addInterceptor(logger)
            }
            return builder
        }

Solution

  • After some research and testing on different devices, it was found that for stable operation on the network, it is necessary that the device is charging or has a screen enabled. In the other case, neither PARTIAL_WAKE_LOCK nor the disabling of battery optimization in the settings itself can solve the problem.

    The recommended way to solve this problem is to add this code to your activity:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    }
    

    This prevents the screen from turning off and provides a stable socket connection. But we still have the situation that the user can press the power button. And, if at this moment the device is charging, everything will work as before, but otherwise, we will get the socket disconnect. To solve this problem, you need to periodically wake the device, in order to support the ping-pong process. This is not a recommended solution because it will lead to battery draining, and can not guarantee 100% performance, but if this moment is critical for you, then you can use this solution. You need to add this code, in a suitable place for you, in this example is used at the time of ping.

     @Suppress("DEPRECATION")
     override fun onWebsocketPing(conn: WebSocket?, f: Framedata?) {
         if (mSocketWakeLock == null) {
             mSocketWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK or PowerManager.SCREEN_BRIGHT_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG)
         }
         mSocketWakeLock?.takeIf { !it.isHeld }?.run { acquire(WAKE_TIMEOUT) }
         super.onWebsocketPing(conn, f)
         mSocketWakeLock?.takeIf { it.isHeld }?.run { release() }
    }
    

    Using this solution, on the test devices socket connection, with good Internet, stays stable for 2 hours or more. Without it, it is constantly disconnect.