-
使用
kotlin
编写的,可以运行在jvm/android
平台的SignalR
简易客户端 -
A simple SignalR client written in
kotlin
that can run on thejvm/android
platform -
与官方库相比,功能较为简单,仅支持本地与远程服务的相互调用。
-
Compared with the official library, the function is relatively simple, only supports the mutual call of local and remote services.
-
因为仅使用了
kotlin coroutine
、OkHttp
、Gson
等常用库,所以相较于官方库,更加轻量。 -
Because only
kotlin coroutine
,OkHttp
,Gson
libraries are used, it is lighter than the official library.
第1步. 添加JitPack仓库到你的项目中
Step 1. Add the JitPack repository to your build file
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
第2步. 添加以下依赖到你的模块中
Step 2. Add the dependency
dependencies {
...
implementation 'cn.numeron:signalr-lite:latest'
}
-
定义要模拟的远程方法
Define server methods for emulationinterface RealtimeDataService { // Note: The simulated method must be suspend qualified @SignalRInvocation("Subscribe") suspend fun subscribe(tags: String) }
-
在本地定义供服务器远程调用的方法
Define methods locally for the server to call remotely// Note: The 'SignalRInvocation' annotation is required. // If the remote method name is different from the local method name, you can specify it through the parameter @SignalRInvocation("GetRealtime") fun onGotRealtimeData(data: RealtimeData) { // This method is invoked during the remote service invocation }
-
构建
SignalRHub
实例
BuildSignalRHub
instanceprivate val signalRHub = SignalRHub.Builder(serverUrl) // Add object instance that defines local method .invocationOwner(this) .build()
-
创建模拟服务器的实例
Create an instance of the simulated serverprivate val realtimeDataService = signalRHub.simulate(RealtimeDataService::class.java)
-
连接
SignalR
服务器
connectSignalR
server.lifecycleScope.launch { try { signalRHub.connect() } catch (throwable: CancellationException) { Log.d("MainActivity", "signalr disconnected.", throwable) } catch (throwable: Throwable) { Log.d("MainActivity", "connect error.", throwable) } }
-
调用远程方法
Call remote methodslifecycleScope.launch { realtimeDataService.subscribe("data1,data2,data3,data4,data5") }