-
-
Save Arunshaik2001/a1c1294d7201b36878a305fa1f22230b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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