Skip to content

Commit

Permalink
Merge pull request #17 from magiclabs/ariflo-sc-72401-adds-mgbox-ping
Browse files Browse the repository at this point in the history
Adds 5 second interval "Heartbeat" to RpcProvider
  • Loading branch information
Ariflo authored Mar 30, 2023
2 parents 099cb17 + aca1e9d commit 7265843
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
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 5 seconds to prevent the WebView from garbage collecting it
setInterval(5000) {
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) {
delay(timeMillis)
handler()
}
}
}

0 comments on commit 7265843

Please sign in to comment.