Skip to content

Instantly share code, notes, and snippets.

@Arunshaik2001
Created December 25, 2022 16:39
Show Gist options
  • Save Arunshaik2001/a1c1294d7201b36878a305fa1f22230b to your computer and use it in GitHub Desktop.
Save Arunshaik2001/a1c1294d7201b36878a305fa1f22230b to your computer and use it in GitHub Desktop.
fun sendMessageBiDirectional(channel: ManagedChannel?): Any{
return try {
val stub = GreeterGrpc.newStub(channel)
var failed: Throwable? = null
val finishLatch = CountDownLatch(1)
val responseList = mutableListOf<HelloResponse>()
val requestObserver = stub.bidirectionalHello(object : StreamObserver<HelloResponse> {
override fun onNext(response: HelloResponse) {
responseList.add(response)
}
override fun onError(t: Throwable) {
failed = t
finishLatch.countDown()
}
override fun onCompleted() {
finishLatch.countDown()
}
})
try {
val requests = arrayOf(
newHelloResponse("TOM"),
newHelloResponse("ANDY"),
newHelloResponse("MANDY"),
newHelloResponse("John")
)
for (request in requests) {
requestObserver.onNext(request)
}
} catch (e: java.lang.RuntimeException) {
requestObserver.onError(e)
return e.message?:""
}
requestObserver.onCompleted()
if (!finishLatch.await(1, TimeUnit.MINUTES)) {
return "Timeout error"
}
if (failed != null) {
return failed?.message?:""
}
return responseList.map { it.message }
} catch (e: Exception) {
e
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment