/**
*@作者 caosq
*@创建日期 2020/11/2 15:07
*@说明 GRPC 的请求客户端
*/
object GrpcClient {
val inetSocketAddress by lazy {
InetSocketAddress(BuildConfig.grpcDomain, BuildConfig.grpcPort)
}
// https://grpc.io/docs/platforms/android/java/basics/
val serviceGrpc by lazy {
// 同步请求
// CenterServiceGrpc.newBlockingStub(GrpcClient.grpcBuilder)
// 异步请求
val asyncStub = CenterServiceGrpc.newStub(grpcBuilder)
asyncStub
}
// 使用的新线程 用来避开 StrictMode: StrictMode policy violation: android.os.strictmode.NetworkViolation
val grpcThead by lazy {
newSingleThreadContext("grpcThread")
}
// https://grpc.io/docs/platforms/android/java/basics/
// https://stackoverflow.com/questions/63226377/grpc-okhttp-android-client-channel-with-self-signed-ssl-certificate
// https://developer.android.google.cn/guide/topics/connectivity/grpc
private val grpcBuilder by lazy {
// AndroidChannelBuilder.forAddress(inetSocketAddress.hostName, inetSocketAddress.port)
OkHttpChannelBuilder
.forAddress(inetSocketAddress.hostName, inetSocketAddress.port)
.keepAliveTime(10, TimeUnit.SECONDS)
.keepAliveWithoutCalls(true)
// .useTransportSecurity()
.usePlaintext()
.build()
}
}