Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds 5 second interval "Heartbeat" to RpcProvider #17

Merged
merged 3 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion magic/core/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=8.0.4
VERSION_NAME=8.0.5
POM_DESCRIPTION=Magic Android SDK
POM_NAME=magic-android
POM_ARTIFACT_ID=magic-android
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package link.magic.android.core.provider

import androidx.annotation.Keep
import org.web3j.protocol.core.Response

@Keep
class HeartbeatResponse : Response<String>()
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package link.magic.android.core.provider

enum class Method {
MAGIC_BOX_HEART_BEAT;

override fun toString(): String {
return name.lowercase()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import android.util.Log
import android.util.Log.DEBUG
import com.google.gson.Gson
import io.reactivex.Flowable
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import link.magic.android.Magic
import link.magic.android.core.relayer.WebViewWrapper
import link.magic.android.core.relayer.message.OutboundMessageType
Expand All @@ -29,6 +32,13 @@ import java.util.concurrent.CompletableFuture
*/
class RpcProvider internal constructor(initialContext: Context, val urlBuilder: URLBuilder) : Web3jService {

init {
// Ping the iFrame every 2min to prevent the WebView from garbage collecting it
setInterval(120000) {
val request = Request(Method.MAGIC_BOX_HEART_BEAT.toString(), emptyList<String>(), this, HeartbeatResponse::class.java)
sendAsync(request, HeartbeatResponse::class.java)
}
}
/**
* Construct Relayer to send payloads to WebView
*/
Expand Down Expand Up @@ -133,4 +143,11 @@ class RpcProvider internal constructor(initialContext: Context, val urlBuilder:
"Magic-SDK: Service %s does not support Close function",
this.javaClass.simpleName))
}

private fun setInterval(timeMillis: Long, handler: () -> Unit) = GlobalScope.launch {
while (true) {
Ariflo marked this conversation as resolved.
Show resolved Hide resolved
delay(timeMillis)
handler()
}
}
}