Rpc
@Target(allowedTargets = [AnnotationTarget.CLASS, AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.TYPE_PARAMETER] )
Every Rpc annotated interface will have a code generation process run on it, making the interface effectively usable for RPC calls.
Example usage.
Define an interface and mark it with @Rpc
. Compiler plugin will ensure that all declarations inside the interface are valid.
@Rpc
interface MyService {
suspend fun sayHello(firstName: String, lastName: String, age: Int): String
}
Content copied to clipboard
On the client side use kotlinx.rpc.RpcClient to get a generated instance of the service, and use it to make calls:
val rpcClient: RpcClient
val myService = rpcClient.withService<MyService>()
val greetingFromServer = myService.sayHello("Alex", "Smith", 35)
Content copied to clipboard
On the server side, define an implementation of this interface and register it on an kotlinx.rpc.RpcServer:
class MyServiceImpl : MyService {
override suspend fun sayHello(firstName: String, lastName: String, age: Int): String {
return "Hello, $firstName $lastName, of age $age. I am your server!"
}
}
val server: RpcServer
server.registerService<MyService> { MyServiceImpl() }
Content copied to clipboard