On Tue, Apr 25, 2017 at 7:04 AM, 黄骐 <[email protected]> wrote:

>   When client make the first connect, I save the responseStreamObserver
> and link it with the client's user ID,
>   so the server can use the StreamObserver when needs to push message to
> client.
>

That sounds good.

  But now I can not find a way to identify the client when connection is
> lost and "CANNCELED" is catched in onError on server side.
>

If the client has already completed its side of the stream, then onError
won't be called, because it can only be called once. Instead, You can call
ServerCallStreamObserver.setOnCancelHandler(Runnable)
<http://www.grpc.io/grpc-java/javadoc/io/grpc/stub/ServerCallStreamObserver.html#setOnCancelHandler-java.lang.Runnable->
and
the Runnable will be executed if the client disconnects. To get the
ServerCallStreamObserver, simply cast the request StreamObserver.

An alternative is to use Context.addListener(CancellationListener, Executor)
<http://www.grpc.io/grpc-java/javadoc/io/grpc/Context.html#addListener-io.grpc.Context.CancellationListener-java.util.concurrent.Executor->
as
the Context is also cancelled when the RPC is cancelled (all I/O failures
on server-side act like cancellation in the API).

-- 
You received this message because you are subscribed to the Google Groups 
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/grpc-io/CA%2B4M1oMhT6Bp992YD8piczTfJrnH7GxSJy98V7R03%2BjGc8e2cQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to