onServiceWorkerConnection
fun onServiceWorkerConnection(env: KsrpcEnvironment<String>, onConnection: suspend (Connection<String>) -> Unit)
Listens for "ksrpc-connect" to attach a port and provides callback to initialize a Connection.
This is an experimental API — the service-worker transport has limited test coverage and its behavior may change without notice.
Samples
import com.monkopedia.ksrpc.annotation.ExperimentalKsrpc
import com.monkopedia.ksrpc.channels.registerDefault
import com.monkopedia.ksrpc.ksrpcEnvironment
import com.monkopedia.ksrpc.serialized
import com.monkopedia.ksrpc.toStub
import com.monkopedia.ksrpc.webworker.createServiceWorkerWithConnection
import com.monkopedia.ksrpc.webworker.onServiceWorkerConnection
fun main() {
//sampleStart
val env = ksrpcEnvironment { }
val service = object : GreetingService {
override suspend fun greet(name: String): String = "Hello, $name!"
}
onServiceWorkerConnection(env) { connection ->
connection.registerDefault(service.serialized(env))
}
//sampleEnd
}