I want to have a socket open while my application is in the foreground. I use the JAVA Api WebSocketClient from here:
So i created a manager that implements the WebSocketClient interface with the onOpen, onClose, onMessage and onError callbacks as follow.
I also have a pingPongTimer so i can check my connection every 10sec interval.
The cycle of that proccess is bound in the application lifecycle so when the app comes to the foreground i call the connect and startPingPongTimer
methods and when it goes to the background the kill and stopPingPongTimer
methods.
The WSS url is a webSocket from Amazon Web Sevices.
My problem is that sometimes i call the connect method and i never get an onOpen callback. I also sometimes get the onClose callback after i have disconnected and reconnect so i can not handle the current connection since i get the onClose from a previous one.
class FlowSocketManagerImpl: FlowSocketManager {
private var isFlowing: Boolean = false
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
private var pingPongTimer = object: CountDownTimer(10000, 1000) {
override fun onTick(millisUntilFinished: Long) {}
override fun onFinish() {
println("On Finish")
send("Hello")
this.start()
}
}
private val mWebSocketClient = object : WebSocketClient(URI(Wss)) {
override fun onOpen(serverHandshake: ServerHandshake?) {
println("onOpen")
isFlowing = true
}
override fun onMessage(envelope: String?) {
println("onMessage $envelope")
}
override fun onClose(code: Int, reason: String, remote: Boolean) {
println("onClose $code $reason $remote")
isFlowing = false
socketBackOffStrategy(code)
}
override fun onError(e: Exception) {
e.printStackTrace()
println("onError ${e.message}")
isFlowing = false
}
}
@Throws(Exception::class)
override fun connect() {
mWebSocketClient.close()
mWebSocketClient.connectionLostTimeout = 10
if (mWebSocketClient.isClosed)
mWebSocketClient.connect()
}
override fun send(envelope: String) {
try {
mWebSocketClient.send(envelope)
} catch (e: WebsocketNotConnectedException) {
// ToDo Mark as unread
println("No connection")
}
}
override fun kill() {
if (mWebSocketClient.isOpen) {
mWebSocketClient.close()
}
}
private fun socketBackOffStrategy(code: Int) {
when (code) {
-1 -> {
// NetWork Unavailable
scope.launch {
delay(10000)
withContext(Dispatchers.Main) {
connect()
}
}
}
1000 -> {
// Successfully disconnected
}
1006 -> {
// 1006 The connection was closed because the other endpoint did not respond with a pong in time.
// For more information check: https://github.com/TooTallNate/Java-WebSocket/wiki/Lost-connection-detection
if (mWebSocketClient.isClosed)
connect()
}
}
}
override fun startPingPongCountDown() {
pingPongTimer.start()
}
override fun stopPingPongCountDown() {
pingPongTimer.cancel()
}
}
What is the best way or Library to use for my purpose or how can i make it more reliable and efficient and stable? Thank you